Part 1 · Foundations · Chapter 2
What RAG is
In one sentence: retrieval-augmented generation is a loop of three moves: retrieve documents for the question, put them in front of the model, and have the model write an answer that cites them.
Why you should care
RAG is the most common way to make a language model useful on documents it was not trained on: a company's manuals, this week's news, a library, a legal archive. It is also simple enough to build in an afternoon, and hard enough to do well that a research track at TREC is devoted to it.
Words you need
- Retriever
- The search part. It takes the question and returns documents.
- Generator
- The language model that writes the answer.
- Corpus
- The collection of documents the retriever searches.
- Passage or chunk
- A piece of a document, small enough to hand to the model.
- Context
- The retrieved text that is placed in the prompt.
- Citation
- A marker in the answer that points to the document a sentence came from.
- Attribution
- The property that each claim can be traced to a source.
- Fine-tuning
- Training a model further on your own data, so that it changes its weights.
Core The idea
- 1RetrieveSearch the corpus with the question. Keep the best few documents.Part 2
- 2AugmentBuild a prompt: instructions, the retrieved text with numbers, then the question.Ch 12, 13
- 3GenerateThe model writes the answer and marks each sentence with the numbers of the documents it used.Ch 12 to 15
The name lists the same three moves in a different order: retrieval, augmented, generation.
A whole RAG system in a few lines
This is the complete idea in Python. The search here only counts shared words, and ask_model stands for a call to any language model. Real systems replace both parts with better ones. The shape stays the same.
docs = {
"D1": "The council approved the bridge repair budget on 3 March.",
"D2": "The new bridge lane was postponed until next year.",
"D3": "The library extended its opening hours in April.",
}
def retrieve(question, k=2):
q = set(question.lower().split())
overlap = lambda text: len(q & set(text.lower().split()))
ranked = sorted(docs, key=lambda d: -overlap(docs[d]))
return ranked[:k]
def build_prompt(question, ids):
sources = "\n".join(f"[{i}] {docs[i]}" for i in ids)
return ("Answer only from the sources. "
"After each sentence, cite the source id in brackets.\n\n"
f"Sources:\n{sources}\n\nQuestion: {question}")
question = "What did the council decide about the bridge?"
prompt = build_prompt(question, retrieve(question))
answer = ask_model(prompt) # e.g. "The council approved the repair budget [D1]. ..."
Read the prompt it builds. The instruction says two things that matter: answer only from the sources, and cite. Those two phrases are the difference between RAG and simply pasting text into a chat.
RAG and its two neighbours
There are three ways to give a model knowledge it lacks. They are often confused.
| RAG | Fine-tuning | Long context | |
|---|---|---|---|
| What you do | Search first, give the model what you found | Train the model further on your data | Paste all the documents into the prompt |
| New documents tomorrow | Add them to the corpus | Train again | Paste them again |
| Can cite sources | Yes, by design | No | Yes, if you ask |
| Works on a huge collection | Yes | Partly | No, the window is finite and long prompts cost money |
| Best for | Facts that change, and answers that must be checked | Style, format and skills | A few documents you already have in hand |
| Main risk | The search misses the right document | Cost, and facts go stale | The model loses track in a very long prompt |
These are not rivals. A real system can use all three: a fine-tuned model, fed by retrieval, with a long context.
Where RAG systems fail
Almost every failure is one of two kinds, and you should learn to tell them apart:
- Retrieval failure. The right document was not in the context. The best writer in the world cannot fix this.
- Generation failure. The right document was there, and the model ignored it, misread it, or added something from memory.
When an answer is wrong, look at the retrieved documents first. This one habit saves days.
Watch and read
- VideoWhat is Retrieval-Augmented Generation (RAG)? IBM Technology. A short whiteboard explanation of the idea. Link checked 14 Nov 2026
- VideoRAG vs. Fine Tuning IBM Technology. When to use which. Link checked 14 Nov 2026
- PaperRetrieval-Augmented Generation for Knowledge-Intensive NLP Tasks Lewis and eleven co-authors, 2020. The paper that gave RAG its name. It pairs a text generator with a searchable index of Wikipedia passages. Read 14 Nov 2026
- SiteTREC RAG 2026, the RAG task. The exact output a RAG system must produce for the track: answer sentences, each with its citations. Linked from the track page, 14 Nov 2026
How we did it at TREC 2026
Our RAG system has the three moves, with more steps inside the first one.
| Move | In our system | Chapter |
|---|---|---|
| Retrieve | Seven versions of each narrative, BM25 on ClimbMix, fused with RRF, then a reranker. 100 documents kept. | 5, 7, 8, 9 |
| Augment | Snippet run: the first 500 characters of each document. Full-text run: up to 1,000 words of each. | 13 |
| Generate | GPT-5 mini writes cited sentences, at most three documents per sentence. A second pass fits the answer into the word limit. | 12, 14 |
The two runs show the “long context” column of the table above in practice. The snippet run gave the writer about 7,900 words of evidence per narrative. The full-text run gave it about 90,000 words. Same writer, same prompt, same 100 documents.
What surprised us
Try it
Lab 1. Build the ten-line RAG above.
- Replace
ask_modelwith a call to any model you can use. Run the bridge question. - Ask a question whose answer is in D3. Does the retriever return D3? Print what it retrieved before you read the answer.
- Ask a question that no document answers. Does the model say so, or does it make something up? Change the instruction until it says so.
- Ask “Was the crossing fixed?” The answer is in D1, but the question shares no useful word with it. Watch the word-overlap retriever fail. This is the reason for Chapters 5 to 7.
Check yourself
- Name the three moves of RAG in order.
- Which two phrases in the prompt turn “pasting text” into RAG?
- Your facts change every day. RAG or fine-tuning, and why?
- An answer is wrong. What do you look at first?
- Why is “paste everything into the prompt” not a solution for a large collection?
Answers
- Retrieve, augment, generate.
- “Answer only from the sources” and “cite the source”.
- RAG. You add documents to the corpus instead of training again.
- The retrieved documents, to see whether the right one was there at all.
- The context window is finite, long prompts are slow and costly, and models lose track of details in very long prompts.
Go deeper
- The original RAG was trained end to end. In the 2020 paper the retriever and the generator were trained together. Most systems today are not trained at all. They connect a ready-made search engine to a ready-made model with a prompt. The name stayed and the method changed.
- Retrieve once, or many times? The pipeline in this chapter retrieves once. An agent can search, read, notice what is missing and search again. Chapter 41 covers these loops.
- What is a good unit to retrieve? Whole documents, fixed-size chunks, sentences or facts. The choice changes both retrieval and writing. See Chapters 13 and 40.
- Project idea. Take 20 questions and one corpus. Compare a closed-book model, a RAG system and a long-context prompt on correctness, on cost per answer, and on how many claims you can verify.