All guides
AI Products·6 min read

How do you add an AI chatbot to an existing web app?

Hrishikesh Patel·Full-Stack Engineer·Updated

To add an AI chatbot to an existing web app, expose a single backend endpoint that calls the language model behind a clean service layer, ground it in your own content with retrieval (RAG) only if it must answer from your data, stream the response to a chat UI, and wrap the whole thing in timeouts and fallbacks so the rest of the app is never blocked by a slow or failed model call.

The mistake teams make is treating this as a rewrite. It is not. A good AI feature is a contained addition with clear inputs and outputs that you can remove or swap without touching the rest of the product.

.../ tl;dr

  • Add the chatbot as a bolt-on service, not a rewrite of your app.
  • Put the LLM call behind one backend endpoint with validation and fallbacks.
  • Use RAG if the bot must answer from your own content; skip it if it only needs general knowledge.
  • Stream responses to the UI and handle the failure states explicitly.

What does the architecture look like?

Keep it simple: your frontend sends a message to one backend endpoint; that endpoint owns the LLM call and any retrieval; the response streams back to a chat component. The model provider stays behind your own service so you can add validation, caching, retries, and swap providers without changing the UI.

  • Frontend chat component — sends messages, renders a streamed reply.
  • Backend chat service — the one place that talks to the LLM.
  • Optional retrieval layer — a vector store for answering from your content.
  • Provider behind the service — never call the model directly from the browser.
Never put your model API key in the frontend or call the provider directly from the browser. The key belongs on the server, behind your own endpoint.

Do you need RAG?

Ask one question: does the bot need to answer from your specific content — docs, policies, product data — or only from general knowledge the model already has? If it needs your content, add retrieval-augmented generation so answers are grounded and can cite sources. If it only needs general capabilities (drafting, explaining, formatting), skip retrieval and save the complexity.

When in doubt, ship the simplest version first without RAG, see where it gives vague or wrong answers about your domain, and add retrieval to close that gap.

Building the chat endpoint

The endpoint validates the request, optionally retrieves context, builds the prompt, calls the model, and streams the result. Putting this behind a service layer means the rest of your app depends on a stable interface, not on a specific provider.

@app.post("/api/chat")
async def chat(req: ChatRequest):
    # 1. validate input
    # 2. (optional) retrieve relevant passages for RAG
    context = await retrieve(req.message) if USE_RAG else None
    prompt = build_prompt(req.message, context)
    # 3. call the model behind a service, with a timeout
    return StreamingResponse(stream_completion(prompt), media_type="text/event-stream")

Streaming to the frontend

Stream tokens as they arrive rather than waiting for the full answer. Streaming makes the bot feel fast and lets users start reading immediately, which matters because model responses can take several seconds. On the client, append chunks to the message as they come in and show a typing indicator until the stream closes.

Reliability and cost

The model call is the easy 10%; the reliability around it is the rest. Set a timeout on every call, retry transient failures, and define what happens when the model is unavailable — a fallback message beats a spinner that never resolves. Control cost by caching repeated questions, keeping prompts as small as the task allows, and routing simple requests to cheaper models.

  • Timeouts and retries on every model call.
  • A graceful fallback when the provider is down or slow.
  • Caching for repeated or near-identical questions.
  • Prompt-size discipline and model routing to manage token spend.

Common pitfalls

  1. 01Rewriting the app around AI instead of adding one contained feature.
  2. 02Calling the provider from the browser and leaking the API key.
  3. 03Shipping without fallbacks, so a model outage breaks the page.
  4. 04Skipping evaluation — you cannot improve answer quality you never measure.
  5. 05Ignoring cost until the first large bill arrives.

FAQ

How long does it take to add a basic AI chatbot?

A contained chatbot with a single backend endpoint and a streamed UI is a small, well-scoped piece of work. Most of the effort goes into grounding it in your data (if it needs RAG) and into the reliability and cost engineering, not the initial model call.

Do I need a vector database for an AI chatbot?

Only if the bot must answer from your own content. A vector database powers retrieval (RAG) so the model can ground answers in your documents. If the chatbot only needs general capabilities, you can skip it.

Can I add AI without replacing my current stack?

Yes. The recommended approach is a bolt-on: one backend service that calls the model, wired into your existing app at a single point. It works with whatever frontend and backend you already run.

Need this built?

I add reliable AI features to real products — chatbots, assistants, and LLM workflows — engineered for cost, latency, and graceful failure, not just demos.

See AI Product Development