AI Agent Knowledge Base Setup 2026
A general LLM knows nothing about your product docs, internal manuals, or latest policies. With a knowledge base (RAG), the agent answers from your own material and can cite which file it came from. This guide covers the whole pipeline, from cleaning documents to evaluating retrieval.
📜 Table of Contents
📖 Keep reading
Building an Agent Knowledge Base: From Documents to Answers
A strong model still does not know the product note you changed last month. Retrieval-augmented generation (RAG) fixes that: split documents into chunks, store embeddings, retrieve the relevant passages at query time, and let the model answer from those passages.
The flow sounds like three steps. In practice, quality is decided by details: cleaning, chunk size, retrieval ranking, and whether answers carry citations.
1. Decide whether you need a knowledge base at all
| Need | Right approach | Why |
|---|---|---|
| Reference a short text (a few thousand words) | Put it in the prompt | No retrieval step, most accurate |
| Tens of thousands of words, updated often, needs citations | RAG | Controllable and traceable |
| Change tone or enforce output format | Fine-tuning | That is behavior, not knowledge |
| Real-time data or exact math | Tool calls | Documents go stale, APIs do not |
2. Clean the documents first
Everything downstream runs on this text, so dirty input means dirty answers.
- Convert PDF, Word, and web pages to plain text, keeping heading levels
- Strip headers, footers, page numbers, tables of contents, and navigation
- Convert tables to markdown or "field: value" form instead of flattening them
- OCR image-only PDFs, or they are effectively not indexed
- Record source name, update date, and owner for every document
- Keep only one version of each file; duplicates with different dates produce conflicting retrieval
3. Chunking parameters
| Parameter | Suggested range | Note |
|---|---|---|
| Chunk size | 300-800 characters | Too small loses context, too large hurts precision |
| Overlap | 10-20% of chunk size | Prevents answers split across a boundary |
| Split basis | Headings first, then length | Split on semantic boundaries |
| Minimum size | Drop chunks under ~50 characters | Tiny chunks are rarely retrieved |
| Parent-child | Small chunks retrieve, large chunks feed the model | Precision plus context |
from langchain_text_splitters import (MarkdownHeaderTextSplitter,
RecursiveCharacterTextSplitter)
headers = [("#", "h1"), ("##", "h2"), ("###", "h3")]
sections = MarkdownHeaderTextSplitter(headers_to_split_on=headers).split_text(text)
splitter = RecursiveCharacterTextSplitter(chunk_size=600, chunk_overlap=80)
chunks = splitter.split_documents(sections)
for c in chunks:
c.metadata["source"] = doc_name
4. Choosing an embedding model
- Chinese-heavy corpora: bge-m3 or bge-large-zh style open models, runnable offline
- Mixed languages: bge-m3 or multilingual-e5 families
- Minimal ops: a hosted embedding API, with dimension, price, and rate limits per official docs
Two hard rules: index and query must use the same model, and changing the model means a full reindex. Higher dimensions are not automatically better; 512-1024 is plenty for a few thousand documents.
5. Do not rely on vectors alone
Vector search is weak on model numbers, error codes, and product names. Use hybrid retrieval: vector top-20 plus BM25 top-20, fused with reciprocal rank fusion, then rerank 20-50 candidates down to 3-8 passages. Add metadata filters for tenant, document type, and effective date, and set a similarity threshold so the system says "not found" rather than returning noise.
6. Expose retrieval as a tool
Define a search_knowledge_base function with a query parameter, tell the model to call it before answering product or policy questions, and return each passage with source and update date. In the system prompt, require three things: answer only from retrieved passages, cite file names, and state explicitly when documents conflict.
7. Evaluate before shipping
Build about 50 question-answer pairs with expected key points and the document that should be hit. Track recall hit rate (target above 80%), answer correctness, refusal accuracy, and latency. When an answer is wrong, check recall first; most "hallucination" cases are retrieval misses.
8. Keep it fresh
Reindex incrementally based on content hashes, delete vectors when a document is retired, and audit a sample of real questions every week. A knowledge base that is not maintained quietly turns into a source of outdated answers.
FAQ
Q: Retrieval keeps missing. Where do I start? A: Add metadata filters to narrow the search space, then rerank. If that fails, the chunking is wrong, usually several topics crammed into one chunk.
Q: Do I need a vector database? A: Not for a few thousand chunks. pgvector, FAISS, or an in-memory array is enough. Consider a dedicated store past the million-chunk mark.
Q: Knowledge base or fine-tuning first? A: The knowledge base. Facts change, fine-tuned weights do not, and only RAG can cite sources.
Related
Written by our editorial team; tools listed here are tested or verified against public sources. Links point to official sites or GitHub repos for reference only — no paid placements.