AI Chatbot
Build a production AI chatbot with RAG, function calling, and edge deployment.
Design & Architecture
Choose model, design RAG retrieval, function-calling schema, and conversation state.
Why
Chatbot architecture decisions are sticky: your retrieval index, tool schema, and state shape all couple to the model's context window and API. A wrong retrieval strategy (semantic-only vs keyword+semantic) caps answer accuracy at ~60% no matter how good the prompt. Design the data flow before the prompt.
When
Before writing the chat endpoint. 1-2 weeks to validate retrieval quality on real questions before building the UI.
How
Example
Common Problems
- ⚠Semantic-only retrieval — misses exact matches (error codes, product IDs); add BM25
- ⚠No reranking — top-k by cosine includes near-duplicates; a cross-encoder rerank lifts accuracy 10-20%
- ⚠Vibes-based eval — looks fine in demos, regresses silently when the index or prompt changes
Best Practices
- ✓Use hybrid retrieval (BM25 + semantic) and rerank with a cross-encoder — it's the single biggest accuracy lever
- ✓Define tool schemas as precisely as a typed function — vague descriptions cause wrong tool calls
- ✓Build a golden eval set before the UI; run RAGAS in CI so retrieval regressions fail the build
Resources for this stage
Tools
Templates
Implementation & Streaming
Build the chat endpoint with SSE streaming, tool execution loop, and guardrails.
Why
Streaming tokens via SSE cuts perceived latency from 5s to <500ms — the difference between 'feels smart' and 'feels broken'. The tool-call loop (model calls tool -> you execute -> feed result back) is where most chatbot bugs live: infinite loops, unvalidated tool args, and context overflow.
When
After retrieval quality is validated on the golden set. Build the streaming endpoint + tool loop, then the UI.
How
Example
Common Problems
- ⚠No iteration cap on the tool loop — a model that re-calls the same tool loops until timeout
- ⚠Buffering the full response before sending — 5s blank screen, users assume it's broken
- ⚠No grounding check — the model answers confidently from training data, not your retrieved docs
Best Practices
- ✓Stream tokens via SSE from the first token — perceived latency is the #1 chatbot UX metric
- ✓Cap the tool-call loop (max 5 iterations) and validate every tool arg with a schema before executing
- ✓Check the answer is grounded in retrieved context; flag ungrounded claims rather than showing them
Deployment & Monitoring
Deploy to Vercel/Cloudflare with streaming, cost tracking, and quality alerts.
Why
Chatbot costs are unbounded — one user in a loop can burn $100 of tokens in an hour. Without per-user rate limits, cost dashboards, and quality alerts (hallucination rate, tool failure rate), a chatbot degrades silently and expensively. Edge deployment (Cloudflare Workers / Vercel) keeps streaming latency low globally.
When
When the endpoint passes the golden eval set. Deploy behind a feature flag, ramp to 10% -> 50% -> 100% of users.
How
Example
Common Problems
- ⚠No per-user cost cap — one user in a tool loop burns the month's API budget in an afternoon
- ⚠Deploying without re-running the golden eval — a prompt tweak regresses retrieval and ships to all
- ⚠Blocking on the full response instead of streaming — high latency, users leave before the answer
Best Practices
- ✓Set a per-user daily token budget and downgrade (not block) on exceed — protect margin without ruining UX
- ✓Run the golden eval set as a CI gate on every prompt/index change; block deploys on regression
- ✓Deploy to edge (Vercel/Cloudflare) and stream from token one — latency is the #1 chatbot metric
Resources for this stage
Tools
Templates
Full Developer Lifecycle
A 14-stage developer roadmap from idea to maintenance — visualize dependencies and track progress.
Next Step