Imagine two students taking an exam about your company’s policies. One has to answer from memory. The other is allowed to bring the policy handbook and look things up. Which answers would you trust more?
A large language model on its own is the first student. It answers from what it learned in training, which doesn’t include your private documents and stops at its knowledge cutoff. RAG, short for retrieval-augmented generation, turns it into the second student. This guide explains how RAG works, step by step, why it’s so widely used, and what can still go wrong.
What is RAG?
Retrieval-augmented generation (RAG) is a technique where an AI system first retrieves relevant information from a collection of documents, then gives that information to a language model, which generates an answer based on it.
The name tells you the steps:
- Retrieval: find the most relevant pieces of information.
- Augmented: add them to the prompt.
- Generation: the LLM writes the answer using them.
The term comes from a 2020 research paper by Patrick Lewis and colleagues at Facebook AI Research, University College London and New York University, presented at the NeurIPS conference. Today “RAG” is used more broadly for any setup where an AI looks things up before answering.
Why do we need RAG?
LLMs have three big gaps that RAG helps close:
- They don’t know your private information: your company handbook, product manuals, contracts or support history were never in their training data.
- Their knowledge goes out of date: they know little after their training cutoff.
- They can make things up: without a source to work from, they may produce plausible but wrong answers (hallucinations).
You could try to fine-tune a model on your documents, but fine-tuning is better at teaching style and behaviour than reliably storing facts, and every document change would mean training again. With RAG, you just update the documents.
How RAG works, step by step
A RAG system has two phases: preparing the documents once, and answering each question.
Phase 1: prepare the documents
- Collect the documents: PDFs, web pages, wiki articles, support tickets.
- Split them into smaller chunks, such as a few paragraphs each, so the system can find precise passages instead of whole files.
- Embed each chunk: an embedding model turns it into a list of numbers that captures its meaning (see embeddings).
- Store the chunks and their embeddings, often in a vector database (letter V), which is built to search by meaning quickly.
Phase 2: answer a question
- Embed the question with the same embedding model.
- Retrieve the chunks whose embeddings are closest to the question’s, such as the top five most relevant passages. Many systems also mix in ordinary keyword search, or re-rank the results.
- Augment the prompt: put the question and the retrieved chunks together, with an instruction like “Answer using only the information below. If the answer isn’t there, say you don’t know.”
- Generate: the LLM writes the answer, ideally pointing to which documents it used.
In most RAG systems today, the LLM itself isn’t changed at all. RAG works by giving it better information in its prompt. (The original 2020 research also fine-tuned its models, but that isn’t needed for RAG to work.)
A simple example
A support assistant for an online shop gets the question: “Can I return shoes I’ve worn once?”
- Without RAG, the model might reply with a generic or made-up returns policy.
- With RAG, it retrieves the shop’s actual returns page, which says worn shoes can be returned within 14 days if they’re unwashed and in the original box (a made-up example policy), and answers from that, with a link to the page.
Why RAG is so popular
- Up to date: change a document, and answers change immediately. No retraining.
- Uses private knowledge without putting it into the model’s weights.
- Fewer made-up answers, because the model works from real text.
- Citations: the system can show where an answer came from, so people can check it.
- Access control: the system can retrieve only documents the user is allowed to see.
- Cheaper than fine-tuning for keeping knowledge current.
Where RAG goes wrong
RAG is powerful, but it’s not magic. Most problems come from the retrieval step:
- The right chunk isn’t found. If retrieval misses it, the model can’t use it. Bad chunking, poor embeddings or vague questions all cause this.
- The wrong chunk is found. The model may answer confidently from an irrelevant or outdated passage.
- The answer is spread across many documents. Questions like “which of our products had the most complaints this year?” need analysis, not just lookup.
- The model still adds things. Grounding reduces hallucinations, but the model can still misread or embellish. Show sources and test it.
- Messy documents produce messy answers: duplicates, old versions and badly scanned PDFs all hurt.
- Hidden instructions: a retrieved document could contain text designed to trick the AI, a risk called prompt injection (see AI jailbreaks).
Good RAG systems are tested with real questions, including questions the documents can’t answer, to check the assistant says “I don’t know” instead of guessing.
RAG vs fine-tuning vs a bigger prompt
| Paste it into the prompt | RAG | Fine-tuning | |
|---|---|---|---|
| Best for | One or a few documents | Large, changing document collections | Style, format, narrow skills |
| Keeps knowledge current | You paste the latest version | Yes, update the documents | Needs retraining |
| Shows sources | Easy | Yes | Hard |
| Effort | Lowest | Medium | Highest |
Models with very large context windows can read whole documents at once, which reduces the need for RAG on small collections. For thousands of documents, RAG is still the practical approach.
Common misconceptions
“RAG trains the model on my documents.” No. The model stays the same; the documents are looked up and added to the prompt at question time.
“RAG eliminates hallucinations.” It reduces them a lot, but the model can still make mistakes. Showing sources helps people check.
“RAG needs a vector database.” Vector search is common, but RAG can use keyword search, a regular database, a search engine, or a mix.
“Bigger chunks are always better.” Too big and the useful sentence gets buried; too small and context is lost. Chunk size needs testing.
Key takeaways
- RAG = retrieve relevant information, augment the prompt with it, then generate an answer.
- It helps LLMs use private and up-to-date knowledge, and reduces hallucinations.
- Typical pipeline: chunk, embed, store; then embed the question, retrieve, augment, generate.
- The term comes from a 2020 paper by Lewis and colleagues (Facebook AI Research, UCL, NYU).
- Most failures come from retrieval; test with real questions and show sources.
Frequently asked questions
What is RAG in simple words?
RAG is when an AI looks up the right information from your documents first, then answers your question using what it found, like an open-book exam.
What does RAG stand for?
Retrieval-augmented generation: retrieve relevant information, augment the prompt with it, and generate an answer.
Is RAG better than fine-tuning?
They solve different problems. RAG is usually better for giving a model accurate, up-to-date facts from your documents. Fine-tuning is better for changing a model’s style, format or a narrow skill. Many systems use both.
What is a vector database used for in RAG?
It stores the embeddings of your document chunks so the system can quickly find the chunks whose meaning is closest to the question.
Can RAG work with ChatGPT, Claude or local models?
Yes. RAG works with almost any LLM, including models you run locally, because it only changes what goes into the prompt.
Comments