Building an Intelligent AI Agent with LLMs and Your Own Knowledge Base
Lessons from real-world production experience: why architecture choices matter more than frameworks, how to combine vector RAG, knowledge graphs, and pre-compiled wikis, plus practical guidance on model selection, tool definitions, and layered memory design.
Building an Intelligent AI Agent with LLMs and Your Own Knowledge Base
The following insights come from hands-on experience building and tuning a real production system, not from rehashing documentation. One core conclusion should be stated upfront: the solution you choose matters far more than the framework you pick. Frameworks solve engineering efficiency problems, while solutions define the boundaries of what your agent can actually do. If you choose the wrong solution, no framework can save you.
A complete agent can be abstracted into a single formula:
Agent = LLM + Knowledge + Tools + Memory
Let’s start with the heaviest component—Knowledge—and then walk through lessons learned on the other three.
1. Knowledge Base Solutions: No Silver Bullet, Only Combinations
We ran a full evaluation of three mainstream approaches using the same corpus of industry technical regulations (several hundred documents). Each conclusion is easier to overlook than the last.
1.1 Pure Vector RAG: Fine for Simple Queries, Fails at Cross-Document Reasoning
Vector retrieval works on the principle of “semantic similarity recall”: the user’s question is embedded, the system finds the Top-K most similar chunks in the vector store, and passes them to the model to generate an answer. This works very well for factual, single-point questions like “What is the standard value of parameter X?”
But it has a structural weakness: when knowledge points are scattered across multiple places, a single retrieval cannot recall them all. In our testing, when operations staff asked “After a protection relay trips and reclosing fails, which items need to be checked?”, the relevant knowledge points were spread across several chapters of the regulations. A single similarity search only hit part of them, so the model inevitably produced incomplete answers based on partial context. This is not a problem you can fix by tuning chunk sizes or reranking models—it’s an inherent limitation of the similarity-retrieval paradigm itself.
1.2 Graph-Enhanced RAG: Strong Multi-Hop Reasoning, But a Double-Edged Sword
The second approach layers a knowledge graph on top of vector retrieval, using entities and relations to support multi-hop traversal: start from entity A, follow relation edges to B and C, and chain scattered knowledge points together. For cross-chapter questions like the one above, multi-hop reasoning did significantly improve recall completeness.
But the costs are equally clear:
- Graph construction is extremely expensive. Entity extraction, relation extraction, disambiguation, and entity resolution all consume heavy model inference. Building the graph over several hundred regulations took a long time on our own GPU cluster, and incremental maintenance is required whenever the regulations are updated.
- Simple queries actually get worse. Public benchmark comparisons have shown that for single-point factual queries, the extra context noise introduced by graph traversal can lower accuracy, performing worse than plain vector retrieval.
In other words, graph-enhanced RAG is not an “upgraded RAG”—it’s “a RAG designed for a different class of problems.”
1.3 LLM Pre-Compiled Wiki: Let the Model Digest Knowledge in Advance
The third approach is more radical: instead of retrieving at query time, have the LLM “compile” documents into structured knowledge entries (similar to a manually maintained wiki) at ingestion time. When new knowledge arrives, the agent digests, merges, and updates existing entries on its own; at query time, it simply reads the already-organized knowledge.
The advantage of this route is high-quality knowledge organization and a short query path. The drawback is that token consumption is also enormous—every knowledge update is a full comprehension-and-rewrite process. Additionally, in offline or private deployment scenarios, the available model’s context length often becomes the bottleneck—compiling long documents easily hits the ceiling.
1.4 Production Practice: Hybrid Architecture with Query Routing
Our production system ultimately uses a hybrid architecture:
| Question Type | Routed Solution |
|---|---|
| Single-point factual queries | Pure vector RAG |
| Cross-document relational reasoning | Graph-enhanced RAG |
| High-frequency operational knowledge | Structured wiki maintained by humans + LLM |
Routing itself can be implemented with a lightweight classifier or simple rules. Don’t expect one solution to solve everything—this is the most valuable takeaway after building all three.
2. LLM Selection: Constraints Beat Benchmarks by a Mile
Beyond the knowledge base, model selection is the second major pitfall. Many teams start with benchmark scores, but the real logic of production is the reverse: first check what your hardware can run, then check what your business needs.
Take our on-call assistant system as an example. It had two hard constraints:
- Results must return within 3 seconds—this directly locks in the combination of model size, quantization method (e.g., FP8), and parallelism strategy (multi-GPU tensor parallelism);
- Answers must include cited sources—this demands reliable instruction following and faithfulness to context, not flashy generation abilities.
Under these constraints, a mid-sized, privately deployable, fast open-source model is far more valuable than a top-benchmarked model that can’t be deployed locally or meets latency targets. The correct selection order is: hardware budget → latency/throughput requirements → context length needs → business task type → and only then, benchmark scores.
3. Tools: The Biggest Pitfall Is Imprecise Definitions
More than 90% of tool-calling failures are not the model’s fault—the problem lies in the tool definitions themselves.
Pitfall 1: Schema too loose. Initially, our knowledge base retrieval tool only declared “input is a query string.” As a result, the model frequently passed empty strings or stuffed entire chains of thought into the parameter. The fix: give every tool a strict parameter schema—field types, required or optional, value ranges, format examples, and fallback instructions for invalid inputs. None of these can be skipped.
Pitfall 2: Example pollution in prompts. We once hit a classic “hallucinated call”: the agent invoked a tool that didn’t exist at all—simply because the system prompt contained a weather-query example, which the model treated as an available tool. The lesson: examples in prompts must be strictly relevant to your business. Leave out irrelevant examples entirely.
Pitfall 3: Reinventing the wheel. The connection between tools and agents long lacked a unified standard—every vendor built its own wrappers, making integration extremely costly. In late 2024, the industry introduced standardization efforts such as the Model Context Protocol (MCP), which describes tools, resources, and prompt templates through a unified protocol, enabling tools to be developed once and reused everywhere. For new projects, adopt these open standard protocols directly rather than building your own private calling conventions.
4. Memory: The Most Underrated Component
Memory is often reduced to “multi-turn conversation history,” but in a production system it has at least three layers:
- Session memory: the current conversation context, maintained by the context window; pay attention to truncation and summarization strategies when it gets too long;
- User/business memory: such as which equipment an on-call operator is responsible for, or historical ticket preferences—these require explicit storage and retrieval;
- Experience memory: success and failure cases the agent accumulates at runtime, which can flow back into prompts or the knowledge base, forming a continuous improvement loop.
The third layer is the most easily overlooked, but it determines whether your system “starts from scratch every time” or “gets smarter with use.”
5. Conclusion
For building enterprise-grade intelligent agents, the priority order should be: solution selection > engineering constraints > frameworks and tools. On the knowledge side, use hybrid routing to cover different question types; on the model side, work backward from hardware and business constraints; on the tool side, eliminate hallucinated calls with strict schemas and standard protocols; on the memory side, build a layered structure to support continuous evolution. Thinking through each pitfall in advance is far more valuable than jumping back and forth between frameworks.
