Introduction
Retrieval-augmented generation (RAG) has become one of the most common architectural patterns for production LLM applications. Instead of asking a model to answer from parametric memory alone, a RAG system retrieves relevant documents from an external knowledge base and grounds the model's response in that retrieved context. This reduces hallucination, supports citation, and lets you update knowledge without retraining the model.
Moving a RAG prototype to production introduces operational concerns that a demo does not: chunking strategy, embedding model selection, vector index freshness, retrieval latency, reranking, and continuous evaluation of retrieved relevance. This guide walks through a practical checklist for building and operating a production RAG system, from data ingestion through query serving and ongoing maintenance.
Phase 1: Data Ingestion and Chunking
Begin by inventorying your knowledge sources: internal documentation, product manuals, support tickets, policy documents, or structured data exports. For each source, define how often it changes and who owns accuracy. A RAG system is only as good as its source data, so stale or contradictory documents will produce stale or contradictory answers regardless of model quality.
Chunk documents into retrieval units sized for your embedding model's context and your typical query length. A common starting point is 512–1024 tokens with 10–20% overlap between adjacent chunks. Overlap helps preserve context that falls at chunk boundaries, but excessive overlap increases index size and retrieval cost without adding signal.
Phase 2: Embedding Model and Vector Index
Select an embedding model based on retrieval quality, language coverage, dimensionality, and latency. General-purpose embedding models work well for broad knowledge bases, while domain-specific models may outperform on technical or proprietary content.
Choose a vector index that matches your scale and update pattern. For small to medium knowledge bases, a simple flat index with exact search may be sufficient and simpler to operate. For larger corpora, approximate nearest-neighbor indexes such as HNSW or IVF offer faster search at the cost of approximate results.
Decide on your update strategy. If documents change infrequently, batch re-embedding on a schedule may be acceptable. If knowledge changes continuously, implement incremental ingestion that re-embeds only modified or new chunks and updates the index without downtime.
Phase 3: Retrieval and Reranking
Implement a retrieval pipeline that converts user queries into embeddings, searches the vector index, and returns the top-k most similar chunks. Start with k between 5 and 10, then tune based on observed relevance and latency. Larger k improves recall but increases prompt size and model cost.
Consider a reranker between retrieval and generation. A cross-encoder reranker scores query-chunk pairs more precisely than vector similarity alone.
Apply access control at retrieval time. Filter results by user permissions before passing chunks to the model.
Phase 4: Prompt Construction and Generation
Build prompts that clearly separate retrieved context from the user query. A common format lists retrieved chunks with source identifiers, then instructs the model to answer using only that context and to cite sources.
Set a context budget. Retrieved chunks plus system instructions plus user query must fit within the model's context window.
Control generation parameters for factuality. Lower temperature values produce more deterministic, extractive answers suitable for RAG.
Require citations in the model output. Instruct the model to reference source identifiers when making claims, and validate that cited sources actually appear in the retrieved context before displaying them.
Phase 5: Evaluation
Build a golden dataset of question-answer pairs with source documents marked. Include questions that can be answered and questions that cannot (testing abstention).
Evaluate retrieval and generation separately. For retrieval, measure recall@k. For generation, measure answer correctness, citation accuracy, and refusal rate on unanswerable questions.
Run evaluations continuously and compare against baseline when making changes.
Phase 6: Observability and Operations
Instrument every RAG query with structured logs. Record the user query, retrieved chunk IDs, retrieval scores, model ID, prompt token count, output token count, latency, and response status.
Create dashboards for retrieval quality, generation latency, and cost per query. Set alerts on anomalies.
Plan for index maintenance and document incident response for RAG-specific failures.
Worked Example: Internal Knowledge Base Assistant
Imagine an internal assistant that answers employee questions about benefits, policies, and engineering documentation. In Phase 1, the team ingests HR policy PDFs, engineering runbooks, and an internal wiki, chunking each document by section with 25% overlap. Each chunk carries metadata: source URL, department, last-reviewed date, and access group.
In Phase 2, they compare two embedding models on 150 employee questions and select the one with higher recall@5. They use an HNSW index with weekly batch re-embedding for documents that change infrequently and incremental updates for the wiki.
In Phase 3, they retrieve top-8 chunks per query and add a cross-encoder reranker that reduces irrelevant results by 30% on their evaluation set. Access control filters chunks by department so engineering-runbook chunks are not visible to HR-only users.
In Phase 4, they construct prompts listing retrieved chunks with source IDs, instructing the model to answer only from context and cite sources. They set temperature to 0.3 and cap context at 8000 tokens.
In Phase 5, they build a golden dataset of 200 employee questions with reference answers and source documents. They measure recall@5 (88%), answer correctness (85%), and citation accuracy (90%) before launch, then re-run evaluations after every change.
In Phase 6, they log every query with chunk IDs and token counts, create dashboards for retrieval latency and cost per query, and schedule monthly re-embedding tests.
Limitations
RAG does not eliminate hallucination. A model can still misrepresent retrieved content, combine unrelated facts, or ignore relevant chunks. Evaluation and citation requirements reduce but do not remove this risk. Retrieval quality depends on chunking and embedding choices that may not generalize across document types.
Cost scales with query volume, chunk size, and reranking. Monitor cost per query from day one and set budgets before usage grows. RAG systems inherit the permissions and biases of their source data.
Conclusion
A production RAG system requires more than vector search and a model call. Success depends on thoughtful ingestion and chunking, embedding model selection, retrieval and reranking tuning, grounded prompt construction, continuous evaluation, and comprehensive observability. Treat the first deployment as a baseline and iterate based on measured retrieval quality and real user feedback.
For primary-source guidance on RAG architectures and best practices, consult: Anthropic: Building effective agents, OpenAI: Retrieval-augmented generation, Google Cloud: RAG patterns, and Meta: Llama RAG documentation.
For related guidance on output contracts and human-in-the-loop design, see AI Code Review Workflow: Human-in-the-Loop Validation Framework.