A Practical Guide to RAG for Large Language Model Deployment
This article walks through the practical steps for deploying Retrieval-Augmented Generation (RAG) as a first large language model use case, covering data governance, parsing and chunking, vector and keyword hybrid retrieval, reranking, prompt constraints with source citation, and a staged rollout with layered evaluation. It includes sample chunk configuration, hybrid search pseudocode, and a prompt template you can adapt.
1. Why RAG Is a Good First Step for LLM Deployment
Retrieval-Augmented Generation (RAG) was named in a paper published in 2020. The core idea: before generating an answer, the model first retrieves passages relevant to the question from an external knowledge source, then receives those passages together with the original question and composes its answer from that material. The common industry explanation is simpler: pull relevant information from a knowledge base, attach it to the user prompt, and answer accuracy improves significantly.
Three reasons make it a good first deployment:
- Knowledge updates do not require retraining. Change the source material and you change the behavior.
- Answers are traceable, which makes review and accountability easier.
- Failures are diagnosable, because every stage in the chain can be observed on its own.
A classic experiment illustrates the point. Researchers built two indexes from encyclopedia snapshots taken in different years, then asked who led a certain country in each of those years. The index built from the older snapshot could not produce the newer name; the index built from the newer snapshot hit it. With model capability held constant, the freshness of the retrieval corpus directly determines answer correctness. RAG decouples “knowledge” from the parameters and hands it back to a data layer you control.
2. Data Governance Before You Start: The Step That Sets the Ceiling
The quality ceiling of any retrieval system is set by the corpus, not by the model. Data governance has three jobs:
- Remove outdated material: take invalid versions offline so old and new documents are not recalled at the same time.
- Deduplicate: merge repeated entries so one paragraph does not occupy several recall slots and crowd out other useful information.
- Annotate metadata: for each document, record the version number, effective date, applicable product scope, and classification level.
Metadata is not optional. It is later used for filtering (by product line, by date), for ranking weights (newer versions first), and for conflict resolution during generation (when two sources disagree, the newer version wins). A common mistake in practice is dumping everything into the vector store at once, so stale data and duplicate content end up in recall results and answer quality drops immediately.
3. Parsing and Chunking: The Most Underrated Stage
Parsing means extracting text and structure from PDFs, Word documents, web pages, and tables into clean plain text, preserving heading hierarchy, lists, and the row and column relationships of tables. Tables need special handling: combining headers and cells into natural-language sentences noticeably improves retrieval hit rate.
Chunking is a balance between semantic completeness and controllable granularity. Chunks that are too large mix several topics together, so recall brings in noise and unrelated content fills the context. Chunks that are too small cut off references and premises, so the model receives a passage it cannot interpret. A common industry practice is to keep each chunk in the range of a few hundred tokens and set an overlap window to preserve cross-chunk context. An illustrative configuration:
chunk_config = {"max_tokens": 500, # Upper limit per chunk; tune to corpus density"overlap_tokens": 50, # Overlap between adjacent chunks to ease semantic breaks"split_by": ["heading", "paragraph", "sentence"],"keep_table_row": True, # Keep header semantics per table row}
One more piece of experience: chunking strategy should diverge by document type. Split policy documents by clause, manuals by operational step, and Q&A material by question-answer pair. A single strategy applied to all of them usually serves none of them well.
4. Vectorization and Indexing: Single-Path Retrieval Is Not Enough
Encode each chunk with an embedding model, write the vectors to a vector database, and also build a keyword inverted index. Vector retrieval alone has a typical weakness: when a user query contains an exact string such as an error code, a model number, or an abbreviation, semantic similarity pulls results toward passages that “generally introduce error codes,” while the passage that matches exactly may not rank at all. Engineering practice therefore favors hybrid retrieval: vector recall handles semantic similarity, keyword recall handles exact matching, and the two result sets are merged and deduplicated.
Illustrative pseudocode:
def hybrid_search(query, top_k=20):vec = vector_db.search(embed(query), top_k=top_k) # Semantic recallkw = keyword_index.search(query, top_k=top_k) # Exact recallmerged = dedup(vec + kw)return rerank(query, merged)[:5] # Rerank, then take the top 5
The reranker performs a second ordering pass over the merged candidates and pushes the genuinely relevant passages to the front. The gain from this step is usually more visible than switching to a larger generation model, and it costs less.
5. Generation and Source Citation: Write the Constraints into the Prompt
Submit the recalled passages together with the question, and state three constraints explicitly in the prompt: answer only from the given material; say “I don’t know” when the material does not cover the question; and cite the ID of each passage used, item by item. Citation is not only about user experience. It lets a reviewer quickly judge whether an answer overstepped, and it turns error localization from “the model is bad” into “which passage was recalled incorrectly.”
You are an enterprise knowledge base assistant.Rules:1. Answer using only the content in <material>; do not supplement facts from parametric memory;2. If the material is insufficient, answer "The available material does not cover this question";3. After each conclusion, cite the source as [passage ID];<material> {{retrieved_chunks}}<question> {{question}}
6. Rollout Cadence: Validate on a Small Batch First, Then Expand
Do not open the pipeline to all traffic as soon as it is built. Most mainstream platforms offer recall testing capabilities that let you input a question and see which passages were hit. Prepare about 20 questions real users have actually asked as an initial evaluation set, and check two things for each one: whether the recalled passages are on topic, and whether the generated answer stays within the scope of those passages.
Attribute evaluation results by layer:
- Retrieval error (wrong passages recalled): first adjust chunk granularity, add metadata filtering, and enable hybrid retrieval with reranking.
- Generation error (passages are right but the answer drifts): then tighten prompt constraints, lower the temperature parameter, and strengthen citation requirements.
The most common mistake is patching the prompt repeatedly when retrieval is already wrong; such changes almost never solve the problem. Fix the evaluation set in place, and after every change to chunking parameters, embedding model, or rerank threshold, run a regression pass. Only then does iteration have a measurable direction. If conditions allow, make evaluation and monitoring routine tasks: periodically sample and review sessions with no recall, low confidence, or user downvotes, forming a closed loop between data governance and retrieval optimization.
