Table of Contents
Quick Answer
Building a production chatbot in 2026 is a weekend project. Use any OpenAI-compatible API, such as OpenAI, Claude via a gateway, or Assisters, with the standard openai npm package. Stream responses over Server-Sent Events, persist conversation history in Postgres, and add function calling for tool use.
- Streaming makes the chatbot feel several times faster even at identical latency.
- Storing every message gives you the data you need for debugging and future fine-tuning.
- Rate-limiting per user is the simplest, most effective defense against abuse and runaway cost.
This tutorial walks through the full build, from dependencies to moderation and rate limiting, plus the mistakes that most often sink chatbot projects.
What You'll Need
The stack is deliberately minimal. Each piece does one job well.
- A Next.js 15+ app or any Node backend
- An OpenAI-compatible API key (a self-hosted-friendly gateway like Assisters is ideal for cost and data control)
- Postgres or Supabase for conversation history and authentication
- The Vercel AI SDK or the raw
openaiclient
The reason to use an OpenAI-compatible API rather than a single vendor's proprietary SDK is portability. Your code stays the same whether you point it at one provider or another, which protects you from lock-in and lets you switch models as pricing and capabilities change.
Step-by-Step Build
Follow these steps in order. Each builds on the last, and the whole sequence is achievable in a focused weekend.
1. Install dependencies
pnpm add openai ai @ai-sdk/openai
2. Configure the client
Point the standard OpenAI client at your chosen gateway. Because the API is OpenAI-compatible, only the base URL and key change.
import OpenAI from 'openai';
const ai = new OpenAI({
baseURL: 'https://assisters.dev/api/v1',
apiKey: process.env.ASSISTERS_API_KEY!,
});
3. Create the streaming endpoint
In app/api/chat/route.ts, stream the completion back to the client. Streaming is the single biggest perceived-performance win.
const stream = await ai.chat.completions.create({
model: 'assisters-chat-v1',
messages,
stream: true,
});
return new Response(stream.toReadableStream());
4. Build the UI
Use the Vercel AI SDK's useChat hook to handle the streaming connection, message state, and input handling. This saves you from writing the SSE plumbing by hand.
5. Persist messages
On each exchange, insert both the user message and the assistant response into a messages table keyed by a conversation_id. This history is invaluable for debugging, analytics, and any future fine-tuning.
6. Add function calling
Define tools, such as searching your database or calling an external API, with JSON schemas, and let the model decide when to invoke them. Function calling is what turns a chatbot from a conversationalist into something that can take action.
7. Moderate input and output
Call a moderation endpoint before responding to filter harmful or policy-violating content. A single jailbreak screenshot circulating online can destroy user trust, so this step is not optional for any public-facing bot.
8. Rate limit
Use a tool like @upstash/ratelimit or a self-hosted Redis to cap usage, for example 20 messages per minute per user. This protects both your costs and your service from abuse.
Common Mistakes to Avoid
Most chatbot projects fail not on the happy path but on the edges. These are the recurring pitfalls.
- Skipping moderation. Public chatbots will be probed. Without moderation, one viral jailbreak can permanently damage trust in your product.
- Infinite context. Sending the entire conversation history with every request inflates cost and latency, and eventually exceeds the context window. Truncate to the last 20 or so messages plus a running summary of older ones.
- No retry logic. Network blips are inevitable. Without exponential backoff and retries, transient failures become broken conversations.
- Exposing the API key in the client. Never put your key in client-side code. Always proxy requests through your server so the key stays secret.
Hardening for Production
A weekend prototype and a production service differ in a handful of important ways. Once the basic loop works, address these before launch.
Manage context deliberately
As conversations grow, you need a strategy for what to send the model. The standard approach is a sliding window of recent messages combined with a periodically updated summary of earlier turns. This keeps cost and latency bounded while preserving the gist of long conversations. Our guide to handling token limits covers practical strategies, and the context window explainer gives the underlying mechanics.
Add observability
Log every step, prompt, response, tool calls, latency, and errors, to a tool like Langfuse. When something goes wrong in production, structured logs are the difference between a five-minute fix and a day of guessing. Review failure cases weekly to find patterns.
Control cost with the right gateway
Cost is the variable that most often surprises teams after launch. Routing through an OpenAI-compatible gateway you control, such as Assisters, lets you manage spend, switch models, and keep data handling in your control without rewriting application code. For a wider look at keeping AI spending predictable, see our guide on reducing AI API costs.
Secure the function-calling layer
If your tools can take consequential actions, sending emails, modifying data, making payments, add human confirmation steps before those actions execute. An agent that can act without oversight is one wrong tool call away from a serious problem. The same principle applies when you graduate from a chatbot to a full agent, covered in our guide to building AI agents.
Top Tools
A focused toolkit covers the entire build.
| Tool | Use |
|---|---|
| Vercel AI SDK | Chat UI primitives and streaming |
| Assisters | OpenAI-compatible gateway |
| Supabase | History + auth |
| Langfuse | Observability |
| Upstash / Redis | Rate limiting |
Choosing the Right Model and Gateway
One decision shapes the cost, quality, and flexibility of everything else: which model you route to and through what gateway. Because you are building on an OpenAI-compatible API, this choice is a configuration value rather than an architectural commitment, but it still deserves thought.
Match the model to the job. A fast, inexpensive model is ideal for routine conversational turns, while a more capable model is worth the cost for complex reasoning, tool selection, or summarization. Many production chatbots route by intent: simple replies go to a cheap model, hard requests escalate to a stronger one. Because the OpenAI-compatible interface keeps the call signature identical, switching models is a one-line change rather than a refactor.
The gateway is equally consequential. Routing through an OpenAI-compatible gateway you control gives you a single place to manage cost, switch providers, enforce rate limits, and keep data handling in your control. This is the difference between a chatbot whose economics you understand and one whose bill surprises you each month. For teams that want to keep data and spend within their own infrastructure, a self-hosted-friendly gateway such as Assisters is a natural fit, and our guide to reducing AI API costs explores the trade-offs in depth.
The broader lesson is to keep the model and gateway behind a thin abstraction in your own code. When pricing shifts or a better model appears, you want to adopt it by changing configuration, not by rewriting your application. That portability is the entire point of building on an OpenAI-compatible standard.
Frequently Asked Questions
Why use an OpenAI-compatible API instead of a single vendor's SDK? Portability. With an OpenAI-compatible API, your code uses the same standard client regardless of which provider or model sits behind it. You can switch models as pricing and capabilities change without rewriting your application, and you avoid lock-in to any one vendor. A gateway like Assisters gives you that flexibility plus cost and data control.
Do I really need streaming for a chatbot? Strongly recommended. Streaming sends the response token by token as it is generated, so users see output begin almost immediately rather than waiting for the full reply. This makes the chatbot feel several times faster even when total latency is unchanged, and it is one of the highest-impact, lowest-effort improvements you can make.
How do I keep my chatbot from getting too expensive? Three measures cover most of it: manage context with a sliding window plus summary rather than sending full history every time, rate-limit usage per user, and route requests through a cost-controlled gateway you manage. Together these keep per-message cost bounded and protect against runaway usage.
Is moderation really necessary? For any public-facing chatbot, yes. Users will attempt to jailbreak or misuse it, and a single viral example of your bot producing harmful content can permanently damage trust. Calling a moderation endpoint before responding is a small amount of work that protects your product's reputation.
How should I store conversation history? Persist each message, both user and assistant, to a database table keyed by a conversation ID, using Postgres or Supabase. This history powers debugging, analytics, and any future fine-tuning. Avoid keeping history only in memory, which is lost on restart and does not scale.
Can I build this without writing all the streaming code myself?
Yes. The Vercel AI SDK's useChat hook handles the streaming connection, message state, and input on the client side, so you do not have to implement Server-Sent Events plumbing by hand. Combined with the standard OpenAI client on the server, it removes most of the boilerplate.
Conclusion
A production chatbot is genuinely a weekend project in 2026, thanks to OpenAI-compatible APIs and mature UI tooling like the Vercel AI SDK. Stream responses for perceived speed, persist every message for debugging and future improvement, add function calling for real capability, and protect the whole thing with moderation and rate limiting.
The key strategic choice is to self-host or route through a gateway you control, such as Assisters, so you manage cost and data rather than being locked to a single vendor. And if you would rather generate the entire scaffold from a prompt instead of wiring it up by hand, Misar.Dev can build the foundation for you. Explore the full toolkit in the Misar AI suite.
Frequently Asked Questions
Quick answers to common questions about this topic.