A company launches an internal assistant. An employee asks, "How many days of parental leave do we get?" The assistant answers confidently: twelve weeks. The real policy, updated in March, says sixteen. The model was never wrong about language. It simply never saw the policy.
Retrieval-augmented generation (RAG) fixes this by adding one step before the model writes: find the relevant passages in your documents and put them in the prompt. It is the pattern behind most "chat with your documents" tools, help-centre bots and internal knowledge assistants. The idea comes from a 2020 research paper by Lewis and colleagues at Facebook AI Research, and today it is the most common way to give a model knowledge it was not trained on.
TL;DR
| Question | Short answer |
|---|---|
| What is RAG? | Search your documents for the relevant passages, add them to the prompt, then let the model answer |
| What problem does it solve? | The model does not know your documents or anything after its training cut-off |
| The four steps? | Index the documents, retrieve passages for the question, add them to the prompt, generate the answer |
| Do I need a vector database? | Not always; keyword or hybrid search often works as well |
| RAG or fine-tuning? | RAG for facts that change or need sources; fine-tuning for behaviour and style |
| Why does RAG fail? | Most often the search found the wrong passages |
| First thing to check? | Which chunks were retrieved for the failing question |
The problem RAG solves
A large language model answers from two sources: the patterns it learned in training, and whatever is in its prompt. Your HR policy, product docs and last quarter's contracts are in neither. Ask about them and the model does what it always does: it writes the most likely answer. When it has no facts to work from, that answer is a plausible guess, which is how hallucinations happen.
You could paste the whole handbook into every prompt. For a two-page document, that is fine. For thousands of pages, it is slow, expensive and unreliable, because a model's context window is limited and details buried in very long inputs get less attention. RAG sends only the few passages that matter for this question.
How RAG works, in four steps
How RAG works: documents split into chunks and indexed so the right passages can be retrieved
1. Index the documents (once, then on every change). Split each document into chunks: a few paragraphs each, ideally along headings so a chunk holds one idea. For each chunk, compute an embedding, a list of numbers that captures its meaning, and store it in a search index, often a vector database. Many systems also keep a keyword index of the same chunks.
2. Retrieve for the question. When a question arrives, embed it the same way and find the chunks whose meaning is closest, usually the top five to twenty. Keyword search finds exact names and codes that meaning-based search can miss, which is why many systems combine both in hybrid search. An optional reranking step then puts the best matches first.
3. Augment the prompt. Put the retrieved chunks into the prompt with clear instructions: answer only from these passages, cite the source of each claim, and say so when the passages do not contain the answer.
4. Generate. The model writes the answer from the passages it was given, with citations the user can follow.
A worked example
Take the parental-leave question again, with RAG in place.
- Index. The HR handbook was split into 400 chunks when it was uploaded. When HR updated the leave policy in March, the changed page was re-indexed.
- Retrieve. The question "How many days of parental leave do we get?" matches three chunks: the March leave policy, an older FAQ entry, and a section on adoption leave.
- Augment. The prompt now reads, in short: "Answer the employee's question using only the passages below. Cite the passage title. If the passages disagree, prefer the most recent and say so." Then the three passages and the question follow.
- Generate. The answer: "Sixteen weeks of paid parental leave, under the policy updated in March (Leave Policy, section 2). An older FAQ entry still says twelve weeks; HR has replaced it."
The model did not get smarter. It got the right page, and an instruction to use it.
When RAG is the right choice
RAG is one of three ways to change what a model produces. They solve different problems, and the prompt engineering vs fine-tuning vs RAG guide goes deeper on the trade-offs.
| Your problem | Best first choice | Why |
|---|---|---|
| The model should follow a format, tone or rule | A better prompt | Cheapest, instant to change |
| The model needs facts from your documents | RAG | Reads current documents at question time and can cite them |
| Facts change often (prices, policies, stock) | RAG | Re-index the changed document; no retraining |
| The model must behave in a specialised way at scale | Fine-tuning | Changes behaviour, not stored facts |
| A small, fixed set of documents | A long prompt | Simpler than a search pipeline |
A useful test from our grounding vs RAG vs fine-tuning guide: if the wrong answer comes from missing knowledge, retrieve; if it comes from wrong behaviour, prompt or tune.
Where RAG goes wrong
Most RAG problems are not the model's fault. They happen before the model writes a word.
RAG answers that cite their source passages, so a wrong answer can be traced to its search step
| Symptom | Likely cause | Fix |
|---|---|---|
| Answer says "I don't know" when the document has it | Search did not retrieve the right chunk | Check chunking, add keyword or hybrid search, add a reranker |
| Answer uses an outdated fact | Old and new versions are both indexed | Remove or re-index stale documents; prefer recent passages |
| Answer mixes two unrelated topics | Chunks are too large or split mid-idea | Chunk along headings, one idea per chunk |
| Right passage retrieved, answer still wrong | Prompt does not make the model stick to the passages | Tell it to answer only from the passages and to cite them |
| A user sees content they should not | Search ignores document permissions | Filter results by the user's access before they reach the model |
The habit that saves the most time: debug retrieval and generation separately. For a failing question, look at which chunks were retrieved. If the right passage is missing, the fix is in indexing or search. If it is there and the answer is still wrong, the fix is in the prompt. Our guide to adding a reranker covers the most common retrieval upgrade, and the Bedrock access-control guide shows how to enforce permissions per user.
How to test a RAG system before you trust it
A RAG demo that answers three hand-picked questions proves little. Before people rely on it, test it the way they will use it.
- Collect 20 to 50 real questions. Take them from support tickets, chat logs or the people who will use the assistant, not from the documents themselves. Real questions use different words from the documents, and that gap is where search fails.
- Write down where each answer lives. For every question, note the document and section that holds the answer, or note that no document answers it. This list is your answer key.
- Score retrieval first. Run each question through the search step only. Did the right section appear in the top results? The share of questions where it did is your retrieval hit rate, and it caps the quality of everything after it.
- Then score the answers. For questions where retrieval succeeded, check whether the answer is correct, cites the right passage and adds nothing that is not in the passages. For questions with no answer in the documents, the right response is "I don't know".
- Re-run after every change. New chunking, a new search setting or a new prompt can fix five questions and break two. Keep the question set and run it each time.
This is the same discipline as evaluating prompts, with one extra column for retrieval. It turns "the bot seems worse this week" into a number you can act on, and it tells you which half of the pipeline to fix.
Beyond basic RAG
Basic RAG searches once and answers once. Two extensions are common:
- Agentic RAG. An AI agent decides when to search, rewrites the query, searches again if the first results are weak, and combines several searches. It handles harder questions at the cost of more time and tokens. See RAG vs agentic RAG.
- RAG through tools. Instead of a fixed pipeline, the agent calls a search tool, often exposed through MCP. Our RAG vs MCP comparison explains when each fits.
Both build on the same four steps. If the basic pipeline retrieves the wrong passages, an agent on top of it will retrieve wrong passages more times.
What to take away
RAG gives a model the right pages before it answers. It is the standard way to build an assistant on your own documents, because it stays current and can cite its sources.
When you build one, spend your effort where the failures are: clean documents, sensible chunks, search that finds the right passage, and a prompt that keeps the model to it. Then measure retrieval on real questions before you judge the answers. For the full pipeline design, continue with RAG context injection: pipeline design. To build agents that use retrieval with an instructor, see the AI Builder workshop.
Related reading
- What are embeddings? Vector search explained
- RAG context injection: pipeline design
- Semantic vs vector vs hybrid search
- Vector databases for RAG compared
- RAG vs agentic RAG
- Prompt engineering vs fine-tuning vs RAG
- What is context engineering?
Primary source: Lewis et al., "Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks" (2020) · Anthropic: Contextual Retrieval
Retrieval tools and vector databases change often; the four steps do not. Accurate as of December 2026. Follow @explainx_ai for updates.
