How to Build a Production RAG System: A Step-by-Step Implementation Guide
A hands-on guide to implementing retrieval-augmented generation for production: chunking strategy, embeddings, vector database integration, hybrid retrieval, prompting, permissions, evaluation, and monitoring, with code.
Key takeaways
- Start with an evaluation set of real questions and expected answers before writing pipeline code.
- Chunk along document structure, store rich metadata, and keep the source text with every vector.
- Hybrid retrieval plus reranking usually beats vector search alone.
- Enforce permissions in the retrieval query, log everything, and re-run evaluations on every change.
To build a production RAG system: define an evaluation set, ingest and chunk documents with metadata, embed and index the chunks in a vector database, retrieve with hybrid search and permission filters, rerank, generate answers with citations, and monitor quality, cost, and latency continuously. This guide assumes you know the concepts in RAG explained.
Step 1: Define success first
Collect 50 to 200 real questions from users or support logs. For each, record the correct answer and the source documents that contain it. This set becomes your test for every design decision: chunk size, embedding model, retrieval settings, and prompts.
Step 2: Ingest and parse documents
- Connect to sources such as SharePoint, Google Drive, Confluence, ticketing systems, PDFs, and databases.
- Extract clean text and keep the structure: headings, lists, and tables. Poor PDF parsing is a top cause of bad answers.
- Capture metadata: source URL, title, section, last updated date, product, language, and access control list.
- Make ingestion incremental so changed documents are re-indexed and deleted ones are removed.
Step 3: Chunk thoughtfully
- Split along headings and paragraphs rather than fixed character counts.
- Start with chunks of roughly 200 to 500 tokens with a small overlap, then tune using your evaluation set.
- Prepend the document title and section heading to each chunk so it makes sense on its own.
- Keep tables intact, or convert them into readable text.
Step 4: Embed and index
Choose an embedding model (see our embeddings guide) and a vector store (see our vector database comparison). A minimal example with Chroma:
import chromadb
client = chromadb.PersistentClient(path="./rag-index")
collection = client.get_or_create_collection("help_center")
collection.add(
ids=["refund-policy#2"],
documents=["Refund policy > Eligibility: Customers can request a refund within 30 days..."],
metadatas=[{"source": "https://example.com/refunds", "team": "support"}],
)
results = collection.query(
query_texts=["Can I get my money back after two weeks?"],
n_results=5,
where={"team": "support"},
)
Chroma uses a default embedding function unless you configure one. In production, set the embedding model explicitly and record its name and version with the index, because changing models requires re-embedding everything.
Step 5: Retrieve with hybrid search and filters
- Combine vector similarity with keyword search (BM25) to catch exact terms such as error codes and product names.
- Apply permission and tenant filters inside the retrieval query, so unauthorized content never reaches the model.
- Retrieve a generous candidate set, such as 20 to 50, then rerank to the best 3 to 8.
Step 6: Generate grounded answers
Write a system prompt that tells the model to answer only from the provided context, cite source IDs, and say clearly when the answer is not available. Return citations to the UI as links. Use structured output if other systems consume the answer.
Step 7: Evaluate, then iterate
| Layer | Metric | Typical fix when low |
|---|---|---|
| Retrieval | Recall at k (did a correct chunk appear?) | Better parsing and chunking, hybrid search, a different embedding model |
| Ranking | Precision of the top results | Add or tune a reranker |
| Generation | Faithfulness and correctness | Clearer prompt, fewer and better passages, a stronger model |
| Operations | Latency, cost per answer | Caching, smaller models for simple queries, fewer tokens |
Step 8: Secure and monitor in production
- Defend against prompt injection in retrieved content, following the OWASP Top 10 for LLM Applications.
- Mask or exclude personal and secret data during ingestion where possible.
- Log queries, retrieved chunk IDs, answers, latency, and user feedback.
- Alert on rising "no answer found" rates, which often signal content gaps.
Our application security guide covers secure development practices for the surrounding application.
Example: An internal HR assistant answers 60% of test questions correctly in its first version. Evaluation shows most failures are retrieval misses on policy tables. Converting tables to text, adding section headings to chunks, and adding keyword search lifts retrieval recall, and answer accuracy improves without changing the LLM.
Parsing difficult documents
Real document collections are messy, and parsing quality often limits a RAG system more than any model choice. PDFs may contain multi-column layouts, headers and footers repeated on every page, scanned images, and tables that lose their structure when converted to plain text. Before indexing, inspect a sample of parsed output for each document type and fix problems at the source.
- Scanned documents need optical character recognition. Check accuracy on a sample, especially for numbers, names, and codes.
- Tables should be preserved as structured text, such as Markdown or HTML tables, or summarized row by row, so numbers stay attached to their labels.
- Headers, footers, and page numbers should be removed so they do not pollute every chunk.
- Images and diagrams can be described with a multimodal model and indexed as text, with a link back to the original image.
- Web pages should have navigation, cookie banners, and boilerplate removed before chunking.
Store the cleaned text and metadata separately from the index, so you can re-chunk and re-embed without parsing everything again.
Designing metadata
Metadata turns a pile of chunks into a searchable, governable knowledge base. Useful fields include the document ID, title, section heading, source system, URL, author or owner, created and updated dates, document type, product or department, region or language, version, and access control information such as groups or tenant IDs. Decide early which fields you will filter on, because some databases require filterable fields to be indexed. Include the title and section heading in the text that is embedded as well, so each chunk carries its own context.
Query understanding
User questions are often short, ambiguous, or phrased differently from your documents. Several techniques improve retrieval:
- Query rewriting: ask a model to rewrite the question into a clearer search query, expanding abbreviations and adding likely terms.
- Multi-query retrieval: generate several versions of the question, retrieve for each, and merge the results.
- Conversation awareness: in chat, rewrite follow-up questions such as "what about for contractors?" into standalone questions using the conversation history.
- Routing: classify the question and send it to the right index, tool, or database, for example policy questions to the document index and account questions to an API.
Each technique adds latency and cost, so measure whether it improves retrieval recall on your test set before adopting it.
Reranking
Vector and keyword search are fast but approximate. A reranker, usually a cross-encoder model, reads the question and each candidate passage together and scores relevance more precisely. A common pattern is to retrieve 20 to 50 candidates with hybrid search, rerank them, and pass the top few to the generation model. Reranking often produces one of the largest quality improvements for the effort involved. Managed reranking APIs and open-source rerankers are both widely available; test them on your own questions.
Citations and answer formatting
Citations let users verify answers and let you trace errors. Give each passage an identifier in the prompt, instruct the model to cite identifiers next to the statements they support, then convert identifiers into links to the source document and section in your interface. Validate citations after generation: if the answer cites a passage that was not provided, or contains no citations at all, treat it as a failure and either retry or return a safe fallback. Keep answers concise, put the direct answer first, and show source titles and dates so users can judge freshness.
Guardrails before and after generation
Input checks can detect prompt injection attempts, off-topic requests, and personal data that should not be processed. Output checks can verify groundedness, block disallowed content, and redact sensitive information. Treat all retrieved text as untrusted data: documents can contain instructions written by attackers, so the system prompt must make clear that instructions inside context are not to be followed, and the model should never have permissions that retrieved text could exploit. Log flagged inputs and outputs for review.
Observability for RAG in production
When a user reports a bad answer, you need to see exactly what happened. Log a trace for every request containing the original question, any rewritten queries, the filters applied, the retrieved passage IDs and scores, the reranked results, the prompt version, the model and version, the final answer, citations, latency for each step, token counts, and cost. Store traces with retention that matches your privacy policy, and mask personal data where required.
Build dashboards for answer volume, latency percentiles, error rates, refusal rate, cost per answer, and user feedback scores. Sudden changes in the refusal rate often indicate a broken ingestion job or a missing data source, while rising latency can point to an overloaded index or a slow model region.
Continuous improvement loop
A production RAG system improves through a steady weekly routine:
- Review answers that users rated poorly and a random sample of all answers.
- Classify each failure: missing content, retrieval miss, ranking problem, generation error, or permission issue.
- Fix the root cause: update or add documents, adjust chunking, tune retrieval, refine the prompt, or correct metadata.
- Add the failing question to your evaluation set so the fix is protected from future regressions.
- Rerun the evaluation set before deploying the change.
Over time, the evaluation set grows into a detailed specification of how the system should behave, and quality becomes measurable rather than anecdotal.
Scaling the system
As usage grows, separate ingestion from serving so heavy re-indexing jobs do not slow down user queries. Use queues for document processing, scale retrieval and generation services independently, and cache frequent queries and their embeddings. If you serve several customers or departments, partition indexes or use tenant filters, and track cost per tenant. Load-test the full pipeline, not just the vector database, because model calls usually dominate latency.
Example architecture on a typical cloud stack
A common production setup looks like this: documents arrive from SharePoint, Google Drive, a CMS, or a ticketing system through connectors that publish change events to a queue. Worker services parse, clean, chunk, and embed the documents, writing text and metadata to a database and vectors to the vector index. A retrieval API, running on containers or serverless functions, authenticates the user, applies permission filters, runs hybrid search and reranking, and calls the generation model. A web or chat interface displays answers with citations and feedback buttons. Monitoring, evaluation jobs, and cost alerts run alongside. Each component can be replaced independently, which keeps the system maintainable as models and tools evolve. For choosing the index, see our vector database comparison; for the concepts behind the pipeline, see RAG explained.
Estimating timeline and effort
A focused RAG system for one department and one or two document sources typically takes a few weeks to reach a reliable pilot: about a week for discovery, evaluation set creation, and access setup; one to two weeks for ingestion, chunking, and retrieval; and one to two weeks for generation, citations, guardrails, interface, and testing. Larger systems with many sources, complex permissions, multiple languages, or strict compliance requirements take longer, mainly because of integration and data preparation rather than the AI components. The most common cause of delay is document quality: outdated, duplicated, or contradictory content must be cleaned up before any system can answer reliably. For help planning a project, see how to choose an AI development company.
Plan for ongoing work after launch as well: content owners need time to fix documents flagged by the review loop, and someone must own the evaluation set and dashboards. Budgeting a few hours a week for this maintenance is what keeps answer quality high as documents and user needs change.
Teams that treat RAG as a product with owners, metrics, and a release process, rather than a one-off integration, consistently get better and more trusted answers.
Need a RAG system built?
Vertex Cyber Tech builds production RAG for customer support chatbots, document analysis, and internal knowledge assistants. Explore our AI/ML services or book a consultation.
Related guides
Sources and further reading
Article FAQs
What chunk size is best for RAG?
There is no universal best size. A common starting point is 200 to 500 tokens with a small overlap, split along headings and paragraphs, then tuned using an evaluation set of real questions.
How do I evaluate a RAG system?
Build a test set of real questions with known answers and source documents. Measure retrieval recall and precision separately from generation faithfulness and correctness, use automated tools such as RAGAS, and add human review on a sample.
How do I enforce document permissions in RAG?
Store access control metadata with each chunk and apply permission filters inside the retrieval query, so unauthorized content is never retrieved or passed to the model. Do not rely on prompt instructions for access control.
Why is my RAG system giving wrong answers?
Most failures come from retrieval: poor document parsing, chunks that lack context, missing keyword matching, or too many irrelevant passages. Check whether the correct passage was retrieved before changing the language model or prompt.
About Vertex Cyber Tech Engineering Team
Software engineers, AI/ML developers, cloud architects, and security specialists at Vertex Cyber Tech Solutions who design, build, and secure production systems for clients in India, the US, the UK, and worldwide.
More articles