A Practical Guide to Building LLM-Powered Agents with a Custom Knowledge Base
This article walks through the real-world practice of building an intelligent agent that combines a general-purpose large language model (LLM) with a company's own knowledge base. It compares three knowledge base strategies—pure vector RAG, GraphRAG, and an LLM Wiki approach—and explains why a hybrid architecture works best in production. It then covers model selection driven by hardware fit and business constraints rather than benchmark scores, and shows how strict JSON Schema definitions and relevant prompt examples prevent tool-calling failures and hallucination. Developers will find concrete pitfalls and practical guidance for building a production-grade agent.
As large language models (LLMs) mature, a central challenge for engineering teams is combining a general-purpose model with a company’s own knowledge base to build an agent that can make decisions and execute tasks autonomously. In practice, building such an agent gets harder at every layer, and each layer is easy to overlook. This article breaks down the practice and the pitfalls across three dimensions: knowledge base construction, model selection, and tool definition.
1. Knowledge Base Construction: Comparing Approaches and a Hybrid Architecture
The knowledge base is the agent’s “brain,” and how it is built directly determines answer quality. There are three mainstream paths today, each with its own trade-offs and use cases.
1.1 Pure Vector RAG vs. GraphRAG
Pure vector RAG (Retrieval-Augmented Generation) performs well on simple factual questions, with fast responses and direct retrieval. However, it often struggles with complex, interrelated business questions. GraphRAG (graph-based retrieval augmentation) addresses relationships between entities by building a knowledge graph, but the cost of constructing that graph is very high. We once built a knowledge graph from hundreds of industry standards, and it took a great deal of time on our own GPU cluster to finish. Moreover, academic research has specifically tested this and found that for simple questions, GraphRAG’s excessive linking can actually interfere with the model’s judgment, yielding worse results than pure vector RAG.
1.2 The LLM Wiki Approach and the Context Challenge
Another cutting-edge practice is to have the LLM “compile” documents into a structured Wiki knowledge base in advance. Under this architecture, the agent system can run on a foundation similar to a local knowledge management tool. When new knowledge comes in, it is no longer just about building a vector index; instead, the agent itself digests and updates the content, achieving dynamic restructuring of knowledge. This approach is highly intelligent, but the resulting enormous token consumption is a challenge that cannot be ignored. In particular, in offline or private environments, the context window of privately deployed open-source models is often limited, and this becomes the critical bottleneck constraining the approach.
1.3 A Hybrid Architecture for Production
In real production environments, never expect a single approach to do everything. We ultimately adopted a hybrid solution: route simple questions through pure vector RAG to guarantee response speed; route complex relational queries through GraphRAG to ensure accuracy; and for operations knowledge bases that demand strong consistency and are updated infrequently, use the LLM Wiki approach with manual-assisted, semi-automatic maintenance. This context-sensitive architecture maximizes the overall effectiveness and stability of the system.
2. Model Selection: Returning to Hardware and Business Constraints
In model selection, many teams initially fall into the trap of chasing the highest benchmark scores. But in production, the core of selection is not who scores highest, but “hardware fit” and “business requirements.”
2.1 Hardware Fit
A company must first assess the compute capacity of its own GPU cluster and determine the parameter scale and quantization scheme that can run smoothly. Discussing selection without regard to hardware is a castle in the air: a model with a very high benchmark score that requires massive compute and infers extremely slowly simply cannot be deployed in routine business.
2.2 Business Performance and Functional Constraints
Take the intelligent on-call assistant system we developed as an example. The system has strict hard requirements—it must output results within 3 seconds, and every answer must include a clear citation source. In this scenario, the model’s inference speed and its ability to control output format are 100 times more important than a few percentage points of higher accuracy on a general leaderboard. When selecting a model, be sure to treat such business constraints as a veto criterion in your evaluation.
3. Tool Calling: Strict Schema Definition Is the Key to Success
In an agent’s execution chain, tool calling is the bridge connecting the LLM to external systems, and it is also one of the most easily overlooked deep pitfalls. The biggest pitfall is imprecise tool definitions.
3.1 The Trap of Imprecise Schema Definitions
Early in the project, when we defined the knowledge base retrieval tool, we simply defined the input parameter as a query string. In actual operation, the model frequently passed an empty string to the tool due to misunderstanding or loss of context, causing retrieval to fail and triggering downstream hallucinated output. This shows that the LLM’s tolerance for vague definitions is far lower than expected.
3.2 Introducing Strict Schema Validation
The fundamental fix is to configure a strict Schema definition for every tool. Use the JSON Schema specification to define parameter types, whether they are required, and enum ranges. Strong type constraints effectively prevent the model’s random input behavior. An example is shown below:
{"name": "search_knowledge_base","description": "Search the industry standards knowledge base for specific procedures or specifications","parameters": {"type": "object","properties": {"query": {"type": "string","description": "The specific search term; cannot be empty and must include the entity name"},"category": {"type": "string","enum": ["safety regulations", "operation manuals", "emergency plans"],"description": "The document category to search"}},"required": ["query", "category"]}}
3.3 Relevance of Prompt Examples
In addition, when writing the system prompt, never use examples unrelated to the business. LLMs have strong in-context learning ability, and irrelevant examples can easily cause the model to drift during reasoning. All prompt examples must strictly match the actual business scenario, ensuring the model follows the correct business logic on every tool call.
4. Conclusion
Building an intelligent agent on top of an LLM and a custom knowledge base is a systems engineering effort that deeply couples data processing, model engineering, and prompt engineering. From hybrid knowledge base design, to model selection based on business constraints, to strict and well-defined Schemas, every step requires developers to refine carefully based on the actual scenario. Only by abandoning the fantasy of a single silver bullet and confronting the difficulties at each layer can you ultimately build a production-grade agent that is truly usable and effective.
