← Blog

Claude API Knowledge Base Chatbot Tutorial

2026-11-17 · 5 min read · SubToAPI Team

What you're building

A knowledge base chatbot answers questions using your own content — docs, FAQs, product manuals, internal wikis — instead of relying on Claude's general training data. This tutorial shows the fastest way to build one with the Claude API: inject your knowledge base into the system prompt, manage multi-turn conversation history correctly, and stream responses so the chat feels responsive.

There are two common architectures. For knowledge bases under roughly 50-100 pages, you can paste the whole thing (or a tightly curated subset) directly into the system prompt — no vector database, no retrieval pipeline, just a well-structured prompt. For larger knowledge bases, you'll eventually need chunking and retrieval. This tutorial focuses on the direct-injection approach because it covers 80% of real chatbot use cases (support bots, internal tools, product assistants) and is the right starting point before you add retrieval complexity.

Step 1: Prepare your knowledge base content

Format matters more than volume. Claude performs better with clearly delimited, well-labeled sections than with a wall of unstructured text.

# Refund Policy
Customers can request a refund within 30 days of purchase...

# Shipping Times
Standard shipping takes 5-7 business days...

# Account Deletion
To delete an account, users must...

Keep each section short and factual. Remove marketing language, redundant intros, and anything that isn't a direct answer to a likely question. If your knowledge base lives in Notion, Confluence, or a CMS, export it to plain markdown and strip the navigation chrome before you touch the API.

Step 2: Write the system prompt

The system prompt is where the knowledge base lives, plus the rules for how Claude should behave.

You are a support assistant for Acme Inc. Answer questions using ONLY the
knowledge base below. If the answer isn't in the knowledge base, say you
don't know and suggest contacting support@acme.com. Keep answers under
4 sentences unless the user asks for detail.

KNOWLEDGE BASE:
<<insert your markdown content here>>

Three things make or break this step:

Step 3: Send the request with conversation history

A chatbot isn't a single API call — it's a growing list of messages. Each turn, you send the full history back so Claude has context.

curl https://api.subtoapi.app/v1/messages \
  -H "Authorization: Bearer $SUBTOAPI_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "claude-sonnet-4-5",
    "max_tokens": 500,
    "system": "You are a support assistant... KNOWLEDGE BASE: ...",
    "messages": [
      {"role": "user", "content": "How long do refunds take?"},
      {"role": "assistant", "content": "Refunds are processed within 5-7 business days after approval."},
      {"role": "user", "content": "And what about international orders?"}
    ]
  }'

On the server side, store the messages array per conversation (session ID, user ID, or a simple cookie-backed key) and append each new user turn plus Claude's reply before sending the next request. The system prompt stays constant across the whole conversation — you don't repeat the knowledge base in every message, just in system.

Step 4: Stream the response

Chat UIs feel sluggish if the user waits for the full answer. Streaming sends tokens as they're generated:

const res = await fetch("https://api.subtoapi.app/v1/messages", {
  method: "POST",
  headers: {
    "Authorization": `Bearer ${process.env.SUBTOAPI_KEY}`,
    "Content-Type": "application/json"
  },
  body: JSON.stringify({
    model: "claude-sonnet-4-5",
    max_tokens: 500,
    system: systemPrompt,
    messages: conversationHistory,
    stream: true
  })
});

const reader = res.body.getReader();
const decoder = new TextDecoder();
while (true) {
  const { done, value } = await reader.read();
  if (done) break;
  renderChunk(decoder.decode(value));
}

Details on event types and parsing SSE chunks are in the streaming docs.

Step 5: Handle a growing knowledge base

Direct injection works until your content outgrows the context window or your token costs climb per request. Two practical signals it's time to move beyond this approach:

At that point, the standard fix is retrieval: split content into chunks, embed them, and fetch only the relevant chunks per query instead of sending everything. That's a separate build (and a bigger one) — but you don't need it to ship a working chatbot today.

Step 6: Wire it into your product

Once the chat logic works, you need a stable API layer in front of it — something your frontend or support tool can call without you managing Claude account credentials directly in client code. This is where SubToAPI fits: it turns your existing Claude access into application API keys (sub_live_...) you can issue per project or per environment, with streaming, tool use, and usage metadata built in so you can track how much each chatbot deployment is actually costing. If you're running the chatbot for multiple internal teams or customer-facing products, team seats let everyone share billing without sharing raw credentials.

Get a key from signup, follow the quickstart to confirm it works, then swap the endpoint and auth header in the code above.

questions

Do I need a vector database to build a Claude API knowledge base chatbot? No, not for small to medium knowledge bases. Pasting well-structured content directly into the system prompt works fine up to roughly 15-20k tokens. Vector retrieval is only needed when your content is too large to fit, or when queries need to pull from many unrelated sections.

How do I stop the chatbot from making up answers not in my knowledge base? Add an explicit instruction in the system prompt telling Claude to answer only from the provided content and to say "I don't know" with a fallback contact when the answer isn't there. This single instruction eliminates most hallucination issues.

How do I manage conversation history across multiple user sessions? Store the messages array per session (keyed by session ID or user ID) in your backend or a lightweight store like Redis, and append each new turn before sending the next request. The system prompt with your knowledge base stays fixed and doesn't need to be duplicated per message.

Turn your Claude access into an HTTPS API

SubToAPI gives you application API keys, streaming, tool use and usage insights on top of your existing Claude access — set up in minutes.

Start free  Read the quickstart →