Building Enterprise-Grade RAG: A Complete Retrieval-Augmented Generation Architecture
This article walks through the engineering of an enterprise-grade RAG (Retrieval-Augmented Generation) system, explaining why parameter memory alone cannot serve private knowledge and how decoupling retrieval from generation makes answers updatable, traceable, and auditable. It covers the seven key stages of the pipeline—document ingestion and parsing, chunking strategy, vectorization and indexing, hybrid retrieval, reranking and context trimming, generation constraints, and evaluation with observability—along with common pitfalls around model-only tuning, over-chunking, and missing permission filtering, plus modular architecture advice for long-term evolution.
1. Why RAG: From Parameter Memory to External Knowledge
A large language model’s capability boundary is largely determined by the time cutoff and coverage of its training corpus. When the business needs answers about internal policies, product manuals, or ticket records—private knowledge—relying on the model’s parametric memory alone is unreliable: knowledge updates lag behind, and the model tends to produce plausible-sounding content with no grounding. Retrieval-Augmented Generation (RAG) is built on a simple idea: decouple generation from retrieval. The retrieval system first finds relevant passages from trusted knowledge sources, then the model organizes an answer based on those passages. The payoff is threefold: knowledge can be updated instantly, answers can be traced to sources, and the reasoning process can be audited.
It is worth stressing that RAG is not just a search box bolted onto a chat box. It is a complete engineering pipeline, and a flaw in any single stage will noticeably degrade the final result.
2. Seven Key Stages of the Full Pipeline
2.1 Document Ingestion and Parsing
Knowledge sources come in many forms: office documents, web pages, database exports, transcripts of audio and video, and more. The parsing stage has to handle layout reconstruction, table structuring, header and footer removal, and optical character recognition for scanned files. A practical approach is to normalize parsing output into a three-part structure: a section tree, plain text blocks, and metadata. The metadata should include at least the source, version, effective date, and permission tags.
2.2 Chunking Strategy
Chunk granularity directly determines retrieval quality. Chunks that are too large mix multiple meanings and introduce noise; chunks that are too small break context apart. In practice, a combination works well: split on semantic boundaries, fall back to a fixed length, and add an overlap window.
def split_text(text, max_len=500, overlap=80):chunks, start = [], 0while start < len(text):end = min(start + max_len, len(text))chunks.append(text[start:end])if end == len(text):breakstart = end - overlapreturn chunks
For tables, code, and clause-style content, structural markers should be preserved so that a complete rule is never cut in half.
2.3 Vectorization and Indexing
Map text blocks to dense vectors and write them into a vector index, while keeping a keyword inverted index in place to prepare for hybrid retrieval later. Index construction must account for incremental updates and version rollback, avoiding the service disruption that a full rebuild would cause.
2.4 Hybrid Retrieval
Pure vector retrieval is good at semantic similarity but weak on exact matches such as proper nouns, IDs, and model numbers; pure keyword retrieval is the opposite. The more robust engineering choice is to run both recall paths in parallel and then fuse the rankings:
def hybrid_search(query, k=20):dense = vector_index.search(query, top_k=k)sparse = keyword_index.search(query, top_k=k)return fuse(dense, sparse) # weighted sum or reciprocal rank fusion
2.5 Reranking and Context Trimming
The recall stage pursues high coverage; the reranking stage pursues high precision. A cross-encoder scores each candidate passage against the question pairwise, and the top few are passed to the model. Context length also needs to be controlled so that irrelevant content does not dilute the model’s attention.
2.6 Generation Constraints
The prompt should state explicitly that the model must answer only from the provided material, must say clearly when the material is insufficient to answer, and must output citation numbers. This is one of the most direct and effective ways to suppress hallucination.
2.7 Evaluation and Observability
Build an offline evaluation set that measures recall hit rate, answer accuracy, and citation accuracy separately. In production, use logging services and monitoring alerts to track latency, failure rate, and user feedback, closing the loop for iterative improvement.
3. Common Pitfalls and Engineering Recommendations
- Pitfall 1: tuning the model while ignoring retrieval. Most quality problems come from missing recall, not from insufficient generation capability.
- Pitfall 2: assuming finer chunks are always better. Chunks that are too fine destroy semantic completeness and end up lowering answer quality.
- Pitfall 3: neglecting permissions. The knowledge base must enforce row-level or document-level permission filtering, with authorization performed before retrieval.
- Recommendation: split knowledge ingestion, index construction, retrieval service, and generation service into independent modules, decoupled through a message queue so each can scale independently. Keep original documents in object storage, deploy containers on a container platform, and pair them with a unified logging and monitoring system. Only then can the system support long-term evolution.
4. Conclusion
The value of RAG lies not in “connecting a large language model,” but in turning enterprise knowledge into a production factor that is retrievable, constrainable, and measurable. Get the six things right—parsing, chunking, hybrid retrieval, reranking, generation constraints, and evaluation—and the system’s stability and trustworthiness will improve dramatically. To further reduce operational complexity, you can build the foundation on general-purpose capabilities such as object storage, container platforms, logging services, and monitoring alerts from Baidu AI Cloud, and focus your effort on refining retrieval strategy and business knowledge.
