Build a Reliable RAG System: Beyond the Demo
A practical blueprint for retrieval, grounded answers, permission checks and measurable quality.
- Design ingestion and retrieval
- Measure retrieval separately from generation
- Prevent unauthorized retrieval
Choose RAG for the right job
A support knowledge assistant needs current runbooks, version-specific advice and citations. Retrieval-augmented generation adds relevant evidence to the model’s input rather than teaching the model all documents through weight updates. It works best when the evidence exists and can be retrieved. It cannot rescue missing, stale or incorrect source material.
Ingestion and indexing
Extract text and preserve titles, headings, page references, product versions and permission metadata. Chunk at logical boundaries such as procedures rather than cutting every document at the same character count. Keep parent document IDs and version dates. Store embeddings alongside text and metadata; update or remove records when source documents change.
A hybrid retriever combines lexical search for exact error codes with semantic search for paraphrases. Rerank candidates if necessary and restrict to a context budget. Always enforce the caller’s access scope during retrieval; filtering citations only after generation is insufficient.
Generation with evidence
Supply the model with relevant passages, a task instruction and stable citation IDs. Tell it to distinguish documented facts from inference and to abstain when no evidence supports an answer. Treat all retrieved content as untrusted data: a document telling the assistant to ignore security rules is not a valid instruction. Validate that cited IDs were actually retrieved and that the passages support the claims.
Evaluation and failure isolation
Create a test set with known relevant passages, unanswerable questions, access boundaries and version conflicts. Measure recall@k for retrieval, then test claim support and completeness separately. If the answer is wrong despite correct retrieval, inspect the prompt and evidence formatting. If the right document never appears, tune chunking, filters, query formulation or reranking before changing the language model.
Deploy with feedback
Record retriever version, latency, retrieval IDs, answer quality and user feedback in privacy-aware traces. Cache only within a suitable permission scope and document version. Set limits for context size and inference cost. Rebuild indexes incrementally and verify that removed or revoked documents are no longer retrievable.
Hands-on example
Toy lexical retrieval only; not a production vector search or a complete RAG application.
docs = {"runbook": "Restart the demo service after checking logs",
"guide": "Create a backup before upgrading"}
query = "backup upgrade".split()
ranked = sorted(docs, key=lambda k: sum(w in docs[k].lower() for w in query), reverse=True)
print(ranked)Practice & self-check
Build a tiny local retriever using public documents and deliberately include one irrelevant document. Test unknown questions and compare exact keyword matching with another retrieval approach.
Is RAG the same as fine-tuning?
No. It retrieves evidence at inference time; fine-tuning changes model parameters.
Why filter by access before generation?
Unauthorized text must not reach the model in the first place.
