Claude API Knowledge Base Search Integration Guide
Claude API Knowledge Base Search Integration
If you're searching for "claude api knowledge base search integration," you're almost certainly trying to get Claude to answer questions using your company's documentation, support articles, or internal wiki instead of relying on its general training data. The short answer: you don't fine-tune Claude on your docs — you build a retrieval layer that fetches relevant content and feeds it to Claude as context, either through a direct prompt injection or through Claude's native tool-use feature.
This pattern is usually called RAG (retrieval-augmented generation), and it's the standard way to connect any LLM API — Claude included — to a private knowledge base. Below is a practical walkthrough of how to wire it up, the two main architectures you'll choose between, and the pitfalls that cause "it works in testing but gives wrong answers in production."
Two architectures for knowledge base integration
1. Pre-retrieval (search first, then ask Claude)
You run the search yourself before calling the Claude API. This is simpler and cheaper:
- User asks a question.
- Your backend embeds the question and queries a vector database (or does keyword search) against your knowledge base.
- The top matching chunks are inserted into the prompt as context.
- Claude answers using only that context.
2. Tool-use retrieval (Claude decides when to search)
You expose a search_knowledge_base function as a tool. Claude decides whether it needs to call it, can call it multiple times, and can refine the query based on earlier results. This is more flexible for multi-step questions ("find the pricing doc, then check if it mentions annual discounts") but costs more tokens and latency per turn.
Most production systems start with pre-retrieval and move to tool-use once questions get more complex.
Setting up pre-retrieval search
Your knowledge base needs to be chunked and embedded first. A typical pipeline:
- Split documents into 300–800 token chunks with some overlap
- Generate embeddings for each chunk (OpenAI, Voyage, or any embedding model)
- Store vectors in Pinecone, Weaviate, pgvector, or similar
- At query time, embed the user's question and run a similarity search
Once you have the top N chunks, build the prompt:
const chunks = await searchKnowledgeBase(userQuestion); // returns top 5 matches
const context = chunks
.map((c, i) => `[${i + 1}] ${c.text}`)
.join("\n\n");
const response = 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: 1024,
system: "Answer only using the provided context. If the answer isn't in the context, say you don't know.",
messages: [
{
role: "user",
content: `Context:\n${context}\n\nQuestion: ${userQuestion}`
}
]
})
});
This works against the standard /v1/messages endpoint regardless of which Claude access layer you're using — see /docs/messages for the full request shape. The key to accuracy is the system prompt constraint: without an explicit instruction to stick to the provided context, Claude will blend in general knowledge, which is where "hallucinated" answers in knowledge base bots usually come from.
Setting up tool-use retrieval
If you want Claude to actively decide when and how to query your knowledge base, define a tool and let the model call it:
{
"name": "search_knowledge_base",
"description": "Search the internal knowledge base for relevant documents",
"input_schema": {
"type": "object",
"properties": {
"query": { "type": "string", "description": "Search query" }
},
"required": ["query"]
}
}
Claude will respond with a tool_use block containing a query instead of a final answer. Your backend executes the search, returns the results in a tool_result block, and sends the conversation back for Claude to continue. The full request/response cycle for this is documented at /docs/tools — the important part is that you loop until Claude stops requesting tool calls and returns a final text answer.
This pattern is worth the extra complexity when:
- Questions often require multiple searches to answer fully
- You want Claude to rephrase or narrow a query based on weak initial results
- You're building an agent that also needs other tools (ticket lookup, calendar, etc.) alongside search
Common mistakes
Dumping too much context. Sending 20 chunks "just in case" increases cost and often decreases accuracy — Claude has to sift through noise. Five well-ranked chunks almost always beat fifteen mediocre ones.
No citation requirement. Ask Claude to reference which chunk it used ([1], [2]) so you can trace answers back to source documents and catch retrieval errors quickly.
Stale embeddings. If your knowledge base updates daily but embeddings are regenerated weekly, users get outdated answers with high confidence. Re-embed on document change, not on a fixed schedule.
Ignoring streaming for long answers. If your knowledge base bot writes multi-paragraph answers, stream the response instead of waiting for the full completion — see /docs/streaming for how server-sent events work on the messages endpoint.
Where SubToAPI fits
If you're integrating Claude into an internal tool or customer-facing product, you still need API key management, usage tracking per team or per customer, and a billing-friendly way to meter requests — none of which the retrieval logic above touches. SubToAPI sits between your app and Claude: you get scoped sub_live_... keys, streaming, tool-use support, and per-key usage metadata without building that infrastructure yourself. Check /docs/quickstart to get a key issued in a few minutes, or /pricing if you're deciding between Solo, Team, and Scale seats for a growing project.
FAQ
Does Claude have a built-in knowledge base feature?
No. Claude's API is stateless and has no native document store. Knowledge base integration always means building a retrieval step yourself — either pre-fetching context before the call or exposing search as a tool Claude invokes mid-conversation.
Should I fine-tune Claude on my documents instead of using retrieval?
No — Claude doesn't support fine-tuning on arbitrary customer documents through the API. Retrieval (RAG) is the correct and only supported approach, and it has the advantage of staying current as your documents change without retraining anything.
How many knowledge base chunks should I send per query?
Start with 3–5 chunks of 300–800 tokens each, ranked by similarity score. Measure answer quality against a test set of real questions before increasing chunk count — more context often adds noise rather than accuracy.