Skip to content
Misar.io

How to Use AI to Build a Chatbot in 2026 (OpenAI-Compatible API Tutorial)

All articles
Guide

How to Use AI to Build a Chatbot in 2026 (OpenAI-Compatible API Tutorial)

Ship a production chatbot in a weekend using OpenAI-compatible APIs, streaming, and function calling. Complete Next.js tutorial with code.

Misar Team·Dec 3, 2025·3 min read
How to Use AI to Build a Chatbot in 2026 (OpenAI-Compatible API Tutorial)
Photo by Mohamed Nohassi on unsplash
Table of Contents

Quick Answer

Use any OpenAI-compatible API (OpenAI, Claude, Assisters) with the openai npm package. Stream responses via Server-Sent Events, store conversation history in Postgres, and add function calling for tool use.

  • Streaming feels 5x faster even at the same latency
  • Store every message for debugging and fine-tuning
  • Rate-limit per user to prevent abuse

What You'll Need

  • Next.js 15+ app or any Node backend
  • OpenAI-compatible API key (Assisters recommended for self-hosted)
  • Postgres or Supabase for history
  • Vercel AI SDK or raw openai client

Steps

  1. Install dependencies. pnpm add openai ai @ai-sdk/openai
  2. Configure client.
ts
   import OpenAI from 'openai';
   const ai = new OpenAI({
     baseURL: 'https://assisters.dev/api/v1',
     apiKey: process.env.ASSISTERS_API_KEY!,
   });
  1. Create streaming endpoint. In app/api/chat/route.ts:
ts
   const stream = await ai.chat.completions.create({
     model: 'assisters-chat-v1',
     messages,
     stream: true,
   });
   return new Response(stream.toReadableStream());
  1. Build the UI. Use Vercel AI SDK's useChat hook.
  2. Persist messages. On each exchange, insert into messages table with conversation_id.
  3. Add function calling. Define tools (search DB, call API). AI decides when to invoke.
  4. Moderate input and output. Call /moderate endpoint before responding.
  5. Rate limit. @upstash/ratelimit or self-hosted Redis: 20 msg/min per user.

Common Mistakes

  • Skipping moderation. A single jailbreak screenshot destroys trust.
  • Infinite context. Truncate history to last 20 messages + summary of older.
  • No retry logic. Network blips kill UX. Use exponential backoff.
  • Exposing API key in client. Always proxy through your server.

Top Tools

ToolUse
Vercel AI SDKChat UI primitives
AssistersOpenAI-compatible gateway
SupabaseHistory + auth
LangfuseObservability
Upstash / RedisRate limiting

Conclusion

A production chatbot is a weekend project in 2026 with OpenAI-compatible APIs and the Vercel AI SDK. Self-host the model gateway (Assisters) to control costs and data. Try Misar Dev to generate the entire scaffold from a prompt.

aichatbotnextjsopenaihow-to
Enjoyed this article? Share it with others.

More to Read

View all posts
Guide

Safely Train AI Chatbots on Website Content in 2026

Website content is one of the richest sources of information your business has. Every help article, FAQ, service description, and policy page is a direct line to your customers’ most pressing questions—yet most of this d

9 min read
Guide

E-commerce AI Assistants 2026: How to Drive Revenue with AI

E-commerce is no longer just about transactions—it’s about personalized experiences, instant support, and frictionless journeys. Today’s shoppers expect more than just a website; they want a concierge that understands th

10 min read
Guide

5 Must-Have Features for a Healthcare AI Assistant in 2026

Healthcare AI isn’t just about algorithms—it’s about trust. Patients, clinicians, and regulators all need to believe that your AI assistant will do more than talk; it will listen, remember, and act responsibly when it ma

11 min read
Guide

Best AI Chat Widgets for SaaS Conversions in 2026: Boost Leads Now

Website AI chat widgets have become a staple for SaaS companies looking to engage visitors, answer questions, and drive conversions. Yet, most chat widgets still rely on generic, rule-based bots that frustrate users with

11 min read

Explore Misar AI Products

From AI-powered blogging to privacy-first email and developer tools — see how Misar AI can power your next project.

Stay in the loop

Follow our latest insights on AI, development, and product updates.