By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
You have documents—proprietary, constantly changing, too many to fit in a model's context window—and you need an LLM to answer questions about them accurately and without hallucinating. Retrieval-Augmented Generation solves this by fetching relevant documents at question time and putting them in the prompt, so the model answers from your data instead of its training memory.
RAG splits into two pipelines. The offline pipeline runs when your documents change: you parse them, chunk them into smaller pieces, embed each chunk into a vector, and store both vectors and keywords in an index alongside metadata like source, owner, access control, and date. The online pipeline runs per question and must be fast: you rewrite the question if needed, search both vector and keyword indexes, filter by the user's permissions, rerank the top candidates with a stronger model, and finally put the best few into the prompt for the LLM.
OFFLINE (ingestion; runs when documents change)
docs ─▶ parse ─▶ chunk ─▶ embed ─▶ vector index + keyword index
└─ keep metadata: source, owner, ACL, date
ONLINE (per question; must be fast)
question ─▶ rewrite? ─▶ HYBRID SEARCH ─▶ RERANK ─▶ prompt ─▶ LLM ─▶ answer
vector+keyword top 20-50 top 3-8 +cites
+ permission filter applied inside the queryChunking is your first hypothesis. Chunks that are too large waste tokens on irrelevant text; too small and they lose context. A few hundred tokens with modest overlap is a reasonable starting point. Prefer structure-aware splitting—by headings, paragraphs, or code blocks—over blind character counts. Treat your chunk size as something to tune against your actual evaluation set, not a constant.
Vector search understands meaning ('car' matches 'automobile') but misses exact strings like error codes and version numbers. Keyword search (BM25) catches those but misses paraphrases. Hybrid search runs both and merges results; this is why most production systems use it. The combination covers gaps neither alone can fill.
Reranking retrieves a wide net—say 20 to 50 candidates—then uses a stronger model to score each one against the question and keeps only the top 3 to 8. It improves precision but adds latency and cost. Measure whether the quality gain justifies the overhead for your use case before making it mandatory.
Security belongs in retrieval, not in the model. Store access-control metadata on each document and filter at query time by the user's permissions. Do not rely on the model to refuse to mention documents it shouldn't see—if they reach the prompt, they can leak. Retrieved text is untrusted input: a document can contain instructions that hijack the prompt.
Freshness requires discipline. Re-embed only changed documents by tracking a content hash. Delete vectors when a source is deleted; stale data is a compliance problem. Switching embedding models means re-indexing everything, so version your model choice and plan for that cost.
RAG suits facts that change, private data, cases where you need citations, and scenarios with per-user permissions. Fine-tuning teaches style, format, or a narrow skill but is poor for fast-changing facts. Long context—pasting a small corpus directly into the prompt—is simple but costly per request and may degrade in quality with huge inputs.
Published RAG benchmarks vary a lot with data and setup. Build a small evaluation set from YOUR documents and questions before trusting any vendor's numbers.
A vendor's benchmark may use different documents, different chunking, different retrieval settings. Your production system will look nothing like it. Spend time building a small set of real questions from your domain and measuring retrieval quality and end-to-end accuracy on those before you commit to a tool or architecture.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.