Retrieval-augmented generation (RAG) is a technique in which an AI system searches a document collection for passages relevant to your question, adds them to the prompt and has the language model answer from them, so replies can draw on current or private sources without retraining.
Table of contents 7 sections
- RAG retrieves relevant passages from a document collection and gives them to a language model before it answers.
- It updates what an AI system knows by changing documents rather than retraining the model.
- It reduces made-up answers but cannot remove them, because the model can still misread or ignore what it retrieves.
- Most failures come from poor chunking, weak retrieval, stale documents, leaky permissions or hidden instructions in retrieved text.
- Choose RAG for changing, private or citable facts and fine-tuning for tone and format, or combine the two.
Retrieval-augmented generation, usually shortened to RAG, is a technique that lets an AI model look up relevant documents before it writes an answer. Instead of relying only on what it absorbed during training, the model reads fresh, specific text and answers from that. The name comes from a 2020 research paper by Patrick Lewis and colleagues at Facebook AI Research, University College London and New York University.
Why do language models need to look things up?
A language model keeps what it learned as billions of numerical weights, and that knowledge stops at its training cutoff. It has never seen your company's refund policy, last week's price list or the contract on your desk. When it lacks a fact it does not stay silent. It writes the most plausible-sounding text, which can be wrong.
RAG treats the problem like an open-book exam. Rather than hoping the model memorised the answer, the system finds the relevant pages first and puts them in front of it. The model still does the writing, and the retrieved text steers what it says.
How does retrieval-augmented generation work, step by step?
Most systems follow the same five steps. The names differ between products, but the order rarely does.
- Prepare the library. Documents are split into chunks, often a paragraph or a few hundred words each. An embedding model turns every chunk into an embedding, a long list of numbers that captures its meaning, so chunks about similar things end up with similar numbers.
- Index the chunks. The embeddings are stored in a vector database or search index built to find nearest neighbours quickly.
- Retrieve. The user's question is converted into an embedding in the same way, and the system fetches the chunks that sit closest to it. Many systems also run an ordinary keyword search alongside and merge the results.
- Augment. The best few chunks are pasted into the prompt together with the question and an instruction such as "answer only from the passages below".
- Generate. The model writes its answer from those passages, and the system can show which ones it used as citations.
The original paper worked at a larger scale than most office projects. Its library held 21 million Wikipedia passages of 100 words each, and the retriever and the text generator were fine-tuned together. Many systems built today skip that joint training and simply place the retrieved passages in the prompt.
How the passages are found matters as much as how the answer is written. The paper's retriever compares embeddings rather than exact words, and a companion study on dense passage retrieval reported that this approach beat the keyword method BM25 by 9 to 19 percentage points in top-20 retrieval accuracy on several question-answering datasets. Amazon Web Services describes the same flow and adds a final stage: updating the documents and their embeddings as the source material changes.
What does retrieval-augmented generation fix, and what does it leave alone?
The gains are practical. You update knowledge by changing documents instead of retraining a model. You can point the model at private material without baking it into the model itself. And because each answer is tied to specific passages, a reader can check the source.
What it does not do is guarantee truth. The model can still misread a passage, ignore it or blend it with something it half-remembers. If retrieval returns the wrong chunk, the model may build a confident answer on top of it. Done well, the method reduces made-up answers but does not remove them.
RAG or fine-tuning: which one do you need?
Fine-tuning continues a model's training on extra examples, which changes its weights. Retrieval-augmented generation leaves the model alone and changes what it is shown at question time. They solve different problems.
- Choose RAG when facts change often, the collection is large, answers must cite sources, or different users may see different documents.
- Choose fine-tuning when you need a consistent tone, output format or specialist vocabulary that prompts alone do not deliver.
- Combine them when you need a house style and current facts at once.
Where does a RAG system go wrong?
Most failures of retrieval-augmented generation trace back to the library or the search, not the model. These are the usual culprits.
- Poor chunking. Cutting a table or a long argument in half can leave each piece meaningless.
- Weak retrieval. Passages that sound similar but answer a different question get fetched, or the right one is missed.
- Stale or conflicting documents. The system quotes an old policy as faithfully as a new one.
- Leaky permissions. If the index mixes documents with different access rules, a user can retrieve text they should never see.
- Hidden instructions. Retrieved text can contain commands aimed at the model. OWASP lists prompt injection first in its 2025 top ten for LLM applications and says RAG does not fully mitigate it.
How is RAG different from an AI agent?
RAG is a method for supplying knowledge. A chatbot is the interface that uses it, and an agent is a system that can plan steps and call tools. Retrieval is often one of the tools an agent has, next to a calculator or a company database. Our explainer on what an AI agent is and how it differs from a chatbot draws the line in more detail.
How do you decide whether to build one?
Begin with the questions, not the technology. Retrieval-augmented generation earns its cost mainly when users ask about material the model has never seen or needs to cite.
- Collect real questions your users ask, along with the documents that answer them. Test whether the right passage comes back among the top few results before you judge the model's writing.
- Tell the model to say the answer is not in the documents rather than guess, and require it to cite the passages it used.
- Give the library an owner and a refresh schedule so outdated files do not linger.
- Filter retrieval by what each user is allowed to read, and log which passages fed each answer. Rules such as the EU AI Act set documentation and transparency duties that vary by use, and those logs help.
- Decide whether to run the search layer yourself or buy it as a managed service. Our guide to IaaS, PaaS and SaaS explains what each option leaves you responsible for.
Frequently asked
- Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks — arXiv (Lewis et al., NeurIPS 2020)
- What is RAG? Retrieval-Augmented Generation AI Explained — Amazon Web Services
- LLM01:2025 Prompt Injection — OWASP GenAI Security Project
- Dense Passage Retrieval for Open-Domain Question Answering — arXiv (Karpukhin et al., EMNLP 2020)
0 comments
No comments yet — be the first.