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·Jan 23, 2026·3 min read
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

  • Install dependencies. pnpm add openai ai @ai-sdk/openai
  • Configure client.

import OpenAI from 'openai';

const ai = new OpenAI({

baseURL: 'https://assisters.dev/api/v1',

apiKey: process.env.ASSISTERS_API_KEY!,

});

  • Create streaming endpoint. In app/api/chat/route.ts:

const stream = await ai.chat.completions.create({

model: 'assisters-chat-v1',

messages,

stream: true,

});

return new Response(stream.toReadableStream());

  • Build the UI. Use Vercel AI SDK's useChat hook.
  • Persist messages. On each exchange, insert into messages table with conversation_id.
  • Add function calling. Define tools (search DB, call API). AI decides when to invoke.
  • Moderate input and output. Call /moderate endpoint before responding.
  • 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

Tool

Use

Vercel AI SDK

Chat UI primitives

Assisters

OpenAI-compatible gateway

Supabase

History + auth

Langfuse

Observability

Upstash / Redis

Rate limiting

FAQs

Which model should I use? Start with assisters-chat-v1 — cheaper than GPT, comparable quality.

How much does it cost? $5-50/mo for a low-volume chatbot. Scales linearly with usage.

Can I fine-tune? Yes — see our next article on fine-tuning.

Does it work on mobile? Next.js PWA or React Native with EventSource polyfill.

How do I handle long conversations? Summarize the first half every 20 turns.

What about function calling safety? Always confirm destructive actions with the user before executing.

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

How to Train an AI Chatbot on Website Content Safely

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: Use Cases That Actually Drive Revenue

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

11 min read
Guide

What a Healthcare AI Assistant Needs Before Launch

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

12 min read
Guide

Website AI Chat Widgets: What Converts Better Than Generic Bots

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.

Get Updates