
A customer asks a shop’s AI assistant a simple question: “If I order at 11:30 on Sunday, can you deliver to Zone A today?” The assistant knows plenty about delivery services in general. What it needs is this shop’s current policy.
Retrieval-augmented generation, usually shortened to RAG, gives an AI application a way to find relevant information before producing an answer. The idea is straightforward: search for evidence, put selected results into the model’s input, then generate a response using that material.
The useful question is whether the system found the right evidence and used it correctly. A fluent answer can still fail at either step.
What RAG adds to a language model
Earlier in this series, we explained how language models predict the next token and the difference between training and inference. RAG builds on those ideas by supplying information at answer time.
The influential 2020 RAG paper by Patrick Lewis and colleagues combined a language-generation model with a searchable index of Wikipedia passages. It studied specific models and training methods. Today’s applications use the term more broadly for systems that retrieve outside information to support generation.
In a common application, updating a document and refreshing the search index changes the evidence available for future questions. It does not, by itself, change the answer model’s learned weights. Developers can also train or fine-tune components of a RAG system, but that is a separate operation.
A basic workflow is question → search → selected passages → model → answer. Microsoft’s RAG overview describes this separation between retrieval and answer generation, along with more elaborate approaches that plan several searches.
First, prepare information that can be found
Many RAG systems prepare a searchable collection before users ask questions. They extract text from documents, divide longer material into smaller sections called chunks, and store those sections with useful details such as the document title, section, version and source location.
Chunking helps a system retrieve a relevant section instead of sending an entire handbook to the model. But the boundaries matter. A paragraph describing an offer may need the exception immediately below it. Microsoft’s chunking guidance describes approaches based on document structure, meaning and overlapping text. There is no single chunk size that fits every collection.
Consider this entirely fictional delivery policy:
- Current policy, section 2: Zone A orders placed before noon qualify for same-day delivery, subject to the operating days in section 3.
- Current policy, section 3: Deliveries operate Monday to Saturday. There are no Sunday deliveries.
- Archived FAQ: An older service ran seven days a week.
A useful collection preserves the link between sections 2 and 3 and clearly marks the FAQ as superseded. Otherwise, search can return convincing text that supports the wrong answer.
How does the search find relevant passages?
Keyword search is useful when exact wording matters, such as “Zone A” or a product code. Vector search uses an embedding model to turn text into a list of numbers. Comparing the question’s representation with stored representations helps find passages with related meaning, even when the wording differs.
For example, a question about “getting a parcel today” might match a section headed “same-day delivery.” The retrieved passage still needs checking: similarity is a clue about relevance, not proof that a policy applies.
Some systems use hybrid search, combining keyword and vector results. They may then rerank the candidates to put more useful passages first. Microsoft’s vector search documentation explains numeric representations, similarity matching and hybrid queries.
RAG does not require every application to use the same search engine or a dedicated vector database. The important outcome is relevant source material that the next stage can actually use.
Then the model receives the evidence
The application assembles the question, selected passages and instructions for answering. These enter the model’s available context. Retrieval helps choose what to include; it does not give the model unlimited space or access to every document in storage.
For our Sunday question, the model needs both the before-noon rule and the operating-day exception. An appropriate response would be:
“No. The current policy excludes Sunday deliveries. Ordering before noon does not override that exception. See Delivery Policy, sections 2 and 3.”
That is an illustrative answer to the fictional policy, not a real delivery promise. It shows why the response should identify the decisive condition rather than simply repeat the most prominent sentence.
A citation is useful only if it points to the actual supporting material. Check the cited passage, its version and whether it supports the whole claim. Attaching a source to an answer does not make every sentence in that answer correct.
Three ways the same question can go wrong
The right section was never retrieved. Search returns the before-noon rule but misses Sunday closure. The model’s input lacks essential evidence. Changing the wording of the final answer prompt will not repair an absent section reliably.
The wrong version was retrieved. Search returns the archived seven-day FAQ. The model might accurately repeat outdated information. Here, the problem is document maintenance and version selection.
The right evidence was retrieved but misused. Both current sections are present, yet the response says yes because 11:30 is before noon. The generation step failed to apply the exception.
These failures call for different fixes. For the fictional shop, inspect retrieved passages first. Then check document status and finally compare each answer claim with the supplied evidence. If the current policy is missing or contradictory, the assistant should report the gap instead of inventing a service commitment.
Private documents need access controls
A search system must respect who is asking. Someone who can read customer-facing delivery information should not automatically receive internal staff records merely because both are indexed.
Microsoft’s document-level access guidance describes filtering search results using permission information. The design principle is to enforce access before restricted material reaches an unauthorised user’s answer context, rather than trusting a final instruction to keep it secret.
Retrieved documents can also contain text that tries to redirect the assistant. OWASP’s prompt-injection guidance identifies this risk in external files and websites and warns that RAG does not eliminate it. Retrieved content should be treated as evidence to evaluate, not permission to override the application’s rules or perform unrelated actions.
Test retrieval and answers separately
A useful evaluation starts with questions whose required evidence and acceptable answers are known. Microsoft’s RAG evaluation documentation separates retrieval quality from answer groundedness, relevance and completeness. That separation makes failures easier to diagnose.
For the fictional shop, create a small test set:
- Ordinary case: a Monday order before noon. Does the answer apply the normal rule?
- Exception: a Sunday order before noon. Does it retrieve and apply the closure?
- Missing information: a question about a delivery zone absent from the policy. Does it acknowledge the gap?
- Conflicting versions: an archived FAQ appears alongside the current policy. Does the system use the effective version?
- Restricted material: a question from an account without access. Does retrieval exclude the protected document?
For every test, retain the retrieved passages as well as the final answer. An answer can be grounded in an outdated document and still be wrong for today’s service. A correct sentence can also be incomplete if it omits the condition the customer asked about.
RAG is especially useful when answers depend on a changing collection of documents. Its value comes from a chain you can inspect: maintained sources, appropriate access, relevant retrieval and answers that faithfully use the evidence. Checking that chain is more useful than treating a confident response as proof that the search worked.