Imagine you join a company and ask its AI assistant: βHow many wellness days do full-time employees receive?β
The answer lives in the employee handbook: βFull-time employees receive four wellness days per calendar year.β The problem is that a general language model may never have seen this private handbook. Even if it saw an older public version during training, the policy may have changed since then.
A language model has two immediate sources of information: patterns stored in its learned weights and text supplied in the current request. Its weights are useful for language and broad knowledge, but they are a poor database for facts that are private, frequently updated, permission-controlled, or expected to carry a verifiable source.
We could paste the entire handbook into every prompt. That may work for a small document, but it becomes wasteful as the collection grows. Most questions need only one or two sections. Sending everything increases prompt processing, cost, latency, and the amount of irrelevant material the model must ignore.
Retrieval-Augmented GenerationβRAGβadds a selective memory step. Before asking the model to answer, the application searches the approved knowledge collection and places only the most useful evidence into the model's context.
The model itself has not learned the policy permanently. The application has temporarily given it the right page to read. When the handbook changes, the knowledge collection can be updated without teaching the language model from scratch.
We want a system to answer a question using a large external collection while satisfying five requirements.
These requirements create two linked problems. Information retrieval decides what the model gets to read. Grounded generation decides how the model uses what it read.
If the correct handbook section never reaches the prompt, an excellent generator cannot reliably recover it. If the correct section is present but the answer says βfive days,β retrieval worked and grounding failed. Keeping the two problems separate is the foundation of RAG debugging and evaluation.
Think of the language model as a skilled researcher taking an open-book exam. The researcher writes clearly and can connect ideas, but the reference library contains policies they have never memorized.
A librarian receives the question first. The librarian does not write the final answer. Instead, they search the catalog, pull a few likely pages, discard unrelated ones, and hand the strongest evidence to the researcher.
The researcher reads those pages, answers the exact question, and notes which page supports the statement. If no page supports an answer, a responsible researcher says the available material is insufficient instead of inventing a policy.
| RAG component | Library analogy |
|---|---|
| Knowledge collection / index | Library catalog and shelves |
| Retriever | Librarian finding candidate pages |
| Reranker / selector | Librarian checking which candidates really answer the question |
| Context builder | The small evidence packet handed to the researcher |
| Generator | Researcher writing the response |
| Citation verifier | Checking that each note actually supports its claim |
This analogy also reveals why RAG can fail. The book may be outdated, the catalog may be wrong, the librarian may misunderstand the question, the best page may be buried under distractors, or the researcher may misread a perfectly good page.
RAG has work that happens before any user asks a question and work that happens for each question. Mixing these phases makes the architecture feel more mysterious than it is.
Indexing time β prepare the library
Question time β find and use evidence
Indexing time turns raw sources into a searchable knowledge layer. Question time uses that layer under the current user's permissions. The active source, parser, chunks, representation, index, prompt, and model all have versions, so the result can be reproduced and rolled back.
Let's follow the wellness-days question through every boundary.
Step 1: Establish the request and its constraints
The application identifies the signed-in employee, their tenant, region, language, and the date for which the policy should apply. Authorization is not a similarity score: ineligible documents must be excluded by policy before their text can enter the model context.
Step 2: Build a retrieval query
The original question is retained. The application may normalize spelling, expand wellness days with a known policy term, or convert a conversational follow-up into a standalone query. Any rewrite is recorded because it may accidentally change the user's intent.
Step 3: Retrieve candidates
A lexical retriever may reward exact matches for wellness and days; a dense retriever may also find personal wellbeing leave even when the wording differs. Suppose the system returns these illustrative candidates:
| Rank | Candidate | Why it matched |
|---|---|---|
| 1 | Handbook βΊ Wellness leave | Contains full-time, wellness days, and annual allowance |
| 2 | Benefits FAQ βΊ Personal leave | Semantically related but less authoritative |
| 3 | Travel policy βΊ Recovery days | Shares `days` but answers a different question |
These ranks are illustrative, not measured. A production trace stores the actual candidate IDs, scores, index version, and filters.
Step 4: Rerank and select evidence
A stronger relevance model or deterministic policy compares the question with each candidate more carefully. The handbook passage is retained because it directly states the allowance and applies to full-time employees. The travel passage is removed as a distractor.
Step 5: Build an attributed context
The source ID, title, section, page, and exact passage travel together. The prompt tells the model to answer only from supplied evidence, cite source IDs, report conflicts, and abstain when support is missing.
Step 6: Generate, cite, and verify
A verifier can split the response into claims and check whether S1 supports each one. The final trace links the answer back to the exact source version and every decision that produced it.
External means the knowledge is not required to live in the generator's weights. At request time means the selected evidence can depend on the current question, user, permissions, and date. Intended to be grounded is deliberately careful: supplying evidence reduces uncertainty but does not guarantee the model will use it correctly.
The term originated in research that combined a sequence-to-sequence generator's parametric memory with a dense Wikipedia index as non-parametric memory. Modern engineering usage is broader: retrievers may be sparse, dense, hybrid, graph, structured, or multimodal, and the generator may be accessed without jointly training it with the retriever.
Let the knowledge collection contain passages dβ β¦ dβ. A retriever assigns each passage a relevance score for query q:
The scoring rule may come from BM25 term statistics, dense vector similarity, a learned sparse model, a graph traversal, or a combination. The first stage returns a candidate set Cβ(q) rather than sending all n passages downstream:
A reranker can apply a more expensive function r(q,d) to that smaller set. A context selector then chooses an ordered subset E that fits the prompt budget while preserving relevance, coverage, authority, diversity, and source identity.
Finally the generator receives the original question, selected evidence, and instructions:
The equations reveal an important ceiling. If the necessary evidence is absent from Cβ(q), reranking and generation cannot use it. If it is in Cβ(q) but removed by Select, the failure belongs to post-retrieval selection. If it reaches E and the answer contradicts it, generation or verification failed.
A production RAG request can be expressed as the following observable algorithm:
Offline, the system also needs a reliable ingestion algorithm: detect source changes, parse and preserve structure, chunk, attach lineage and access metadata, build representations, validate a staging index, activate it atomically, and propagate updates or deletions.
A baseline RAG application does not require training a new language model. It does require indexing work, and learned retrievers or rerankers may later be adapted with labeled or synthetic data.
| Phase | Work | Behavioral consequence |
|---|---|---|
| Training (optional) | Optimize retriever/reranker from positive and negative examples | Changes which evidence is scored as relevant |
| Indexing | Parse, chunk, encode/analyze, and load every source record | Moves corpus processing before user requests; upgrades may require full re-indexing |
| Retrieval inference | Encode/analyze query and search index | Candidate depth and search effort trade latency for coverage |
| Reranking inference | Jointly score query with candidates | Usually improves ordering at cost proportional to candidate count |
| Generation inference | Process instructions, evidence, and answer tokens | More context and longer output increase latency and cost |
Exact complexity depends on the chosen retriever and index. A brute-force dense search compares the query to every vector; approximate indexes reduce work by accepting possible neighbor misses. BM25 traverses posting lists for matched terms. Cross-encoder reranking processes each query-passage pair, so it is intentionally placed after a cheaper high-recall stage.
The practical objective is not minimum latency or maximum recall alone. Teams choose a measured qualityβlatencyβcost operating point and monitor p50, p95, and p99 behavior rather than only an average.
Language models are strong at interpreting and composing text placed in their context. RAG takes advantage of that capability while moving factual storage into an external system that is easier to search, update, authorize, inspect, and delete.
Selective retrieval also reduces the generator's problem. Instead of asking it to locate one policy inside an entire library, the application narrows the task to reading a small evidence packet. A reranker and context selector remove plausible but distracting text before generation.
Source attribution creates an audit path. Users and automated checks can compare claims with the exact passage and source version. This does not make every citation correct, but it makes support testable in a way that facts hidden only in model weights are not.
RAG is especially useful when knowledge changes independently of model behavior. A policy correction can be ingested and activated without waiting for a new model-training cycle.
These benefits are architectural possibilities, not automatic outcomes. Freshness needs ingestion monitoring; privacy needs correct filtering; provenance needs claim-support validation; modularity needs versioned interfaces and evaluation.
RAG introduces more components and therefore more places to fail. The source may be wrong. Parsing can scramble a table. Chunking can separate a condition from a claim. A query rewrite can drift. Approximate search can miss the nearest vector. A reranker can prefer a fluent distractor. The context builder can bury the answer. The generator can ignore clear evidence.
Retrieval can also make an answer confidently wrong. If a stale handbook version is retrieved and the model follows it perfectly, the answer is faithful to context but untruthful relative to the current policy. If malicious document text instructs the model to reveal secrets, retrieval has expanded the attack surface.
| Symptom | Earliest boundary to inspect |
|---|---|
| Fact absent from every result | Source, parsing, chunking, query, or first-stage retrieval |
| Fact retrieved but ranked below cutoff | Candidate depth or reranking |
| Correct evidence in candidates but not prompt | Context selection/budget |
| Correct evidence in prompt, wrong claim | Generation/grounding |
| Claim is true but citation does not support it | Citation assignment/verification |
| Unauthorized evidence appears | Identity, ACL metadata, or retrieval filtering |
Reliability comes from evaluating each boundary and the end-to-end product. There is no single universal RAG score that diagnoses all of these failures.
Mistake 1: Treating RAG as βput documents in a vector database.β A vector index is only one retrieval mechanism. Source quality, parsing, structure, metadata, permissions, query understanding, ranking, context, generation, and evaluation are equally part of the system.
Mistake 2: Assuming more context is always safer. Extra passages consume tokens and introduce distractions, contradictions, prompt injection, and positional effects. Measure evidence coverage and noise rather than filling the context window.
Mistake 3: Evaluating only the final answer. A wrong answer gives no causal diagnosis. Store candidates, ranks, selected evidence, exact prompt context, answer claims, and citations so failures can be localized.
Mistake 4: Using similarity as authorization or confidence. A high score does not grant access and is not automatically a calibrated probability that the answer is supported.
Mistake 5: Claiming that RAG eliminates hallucination. RAG can reduce unsupported answers when relevant trustworthy evidence is retrieved and used correctly. It cannot guarantee source truth, retrieval success, faithful generation, or correct citations.