Why RAG, and the canonical pipeline
Open-book answers from a closed-book model
A model's weights are frozen knowledge: stale, public and willing to guess. Retrieval-augmented generation fixes all three by fetching evidence at query time and letting the model answer from it with citations. Almost every serious LLM product built since 2023 — support assistants, enterprise search, coding tools, research agents (10.1 What is an agent? Loops, autonomy and workflows vs agents) — is a RAG system, and its failure modes are the failure modes of this pipeline. This lesson gives you the map that the rest of the phase fills in.
You'll be able to
- Explain the three gaps of a frozen LLM and how retrieval addresses each
- Decide between RAG, fine-tuning and long context for a given problem using cost and update frequency
- Name the eleven stages of the canonical pipeline and what each one is responsible for
- Diagnose a failing RAG system using the seven failure points and the retrieval-ceiling argument
Key ideas
- A frozen LLM has three gaps — stale, public-only and ungrounded knowledge; RAG fetches evidence at query time so the model answers from text it can cite.
- Lewis et al. (2020) wrote RAG as p(y|x) ≈ Σ_z p(z|x) p(y|x,z); modern RAG concatenates the top-k chunks and lets attention do the weighting.
- Knowledge problems → RAG; behaviour problems → fine-tuning; a small corpus queried rarely → long context with prompt caching. Real systems combine all three.
Show 4 more key ideas
- The pipeline has an offline half (ingest, parse, chunk, embed, index) and an online half (retrieve, rerank, assemble, generate, cite), with evaluation wrapped around everything.
- Retrieval quality is a ceiling: if the needed chunk is not in the top-k, no prompt can save the answer — measure recall@k before tuning prompts.
- The seven failure points (missing content, missed top rank, not in context, not extracted, wrong format, wrong specificity, incomplete) map each symptom to a pipeline stage.
- By 2026 RAG is a design pattern, not a model: hybrid retrieval, rerankers, contextual chunks, structured outputs and agentic, tool-driven retrieval.
Suggested path through this lesson
Three gaps in a frozen model
A language model's weights encode what it saw during pretraining (8.1 The LLM lifecycle and model landscape) — its parametric knowledge. That knowledge has three structural gaps. It is stale: anything after the training cutoff does not exist. It is public: your company's wiki, your lab's notes and yesterday's support tickets were never in the corpus. And it is ungrounded: when the model does not know, it still produces fluent text, the behaviour we call hallucination (8.12 Safety, risks and responsible AI). Fine-tuning (8.2 Fine-tuning and PEFT) can inject some facts, but it is slow, expensive to update, hard to attribute, and surprisingly bad at reliably storing specific facts.
Retrieval-augmented generation (RAG) attacks all three gaps with one move: at query time, fetch the most relevant pieces of text from an external store (non-parametric knowledge) and place them in the prompt so the model answers from evidence it can quote. Lewis et al. (2020) formalized this as marginalizing over retrieved passages :
with a trainable retriever (a dense bi-encoder, 9.2 Embeddings deep dive) and a generator (BART, 7.7 Encoder–decoder: T5, BART and choosing an architecture). Modern RAG keeps the shape but simplifies the math: a frozen LLM reads the concatenation of the top- chunks, and the weighting happens implicitly through attention (7.2 Scaled dot-product and multi-head attention, fully derived).
Intuition. Fine-tuning is studying for a closed-book exam; RAG is an open-book exam with a good index. The model still has to read and reason, but it no longer has to remember everything.
RAG vs fine-tuning vs long context
| RAG | Fine-tuning | Long context (put it all in) | |
|---|---|---|---|
| Best at | Facts, freshness, private data, citations | Style, format, domain language, new skills | Small corpora, one-off analysis |
| Update cost | Re-index changed documents (minutes) | Retrain and re-evaluate (hours to days) | None, but every request pays again |
| Attribution | Natural: cite the chunk | None | Possible but weaker at 500k tokens |
| Per-request cost | Retrieval plus a few thousand context tokens | Cheapest at inference | Grows linearly with corpus size |
| Failure mode | Retrieval misses, so context is wrong or absent | Forgetting, invented facts, staleness | Lost in the middle, latency, cost |
| Needs | Clean documents and an eval set | Thousands of good examples and GPUs | A corpus that fits comfortably in the window |
Rules of thumb: knowledge problems → RAG; behaviour problems → fine-tuning; a document set that fits comfortably in context and is queried a handful of times → just put it in the prompt, ideally with prompt caching (9.9 Production: latency, cost, caching, guardrails, security and privacy). Serious systems combine them: a fine-tuned small model for formatting, RAG for facts, and a long-context read of a few whole documents at the end (9.7 Advanced RAG patterns).
The canonical pipeline
RAG has an offline half that prepares knowledge and an online half that answers questions:
- Ingest — collect sources (PDFs, HTML, tickets, code, databases) with their metadata and permissions.
- Parse — convert to clean text or Markdown while preserving structure (headings, tables).
- Chunk — split into retrieval units of a few hundred tokens (9.4 Chunking and ingestion).
- Embed — map each chunk to a vector with an embedding model (9.2 Embeddings deep dive).
- Index — store vectors (and a lexical index) for fast nearest-neighbour search (9.3 Vector search and indexes).
- Retrieve — embed the query and fetch the top- candidates, usually from dense and sparse indexes (9.5 Retrieval quality: hybrid search, reranking, query transforms and metrics).
- Rerank — re-score candidates with a cross-encoder and keep the best few (9.5 Retrieval quality: hybrid search, reranking, query transforms and metrics).
- Assemble — build a prompt with instructions, numbered sources and the question inside a token budget (9.6 Generation side: context assembly, citations, structured outputs and tool calling).
- Generate — the LLM answers, grounded in the sources, often as structured output (9.6 Generation side: context assembly, citations, structured outputs and tool calling).
- Cite — map claims back to sources so users can verify.
- Evaluate — measure retrieval and answer quality continuously (9.8 Evaluating and observing LLM apps).
Steps 1–5 run when documents change; 6–10 run per query; 11 runs always. Every step is a place where quality is won or lost, which is why most of them get their own lesson in this phase.
Where the pipeline breaks: the seven failure points
Barnett et al. (2024) studied three production RAG systems and catalogued seven recurring failures. They make an excellent debugging checklist:
| # | Failure | Stage | Fix lives in |
|---|---|---|---|
| FP1 | Missing content: the answer is not in the corpus | Ingest | Coverage, honest refusal (9.6 Generation side: context assembly, citations, structured outputs and tool calling) |
| FP2 | Missed the top-ranked documents | Retrieve | Hybrid search, better embeddings, reranking (9.5 Retrieval quality: hybrid search, reranking, query transforms and metrics) |
| FP3 | Not in context: retrieved but cut by the budget | Assemble | Choice of , ordering, compression (9.6 Generation side: context assembly, citations, structured outputs and tool calling) |
| FP4 | Not extracted: in context but ignored by the model | Generate | Prompting, less noise, stronger model |
| FP5 | Wrong format: asked for a table, got prose | Generate | Structured outputs (9.6 Generation side: context assembly, citations, structured outputs and tool calling) |
| FP6 | Incorrect specificity: too general or too detailed | Chunk / Generate | Chunk design, query rewriting |
| FP7 | Incomplete: partial answer although the sources cover it | Retrieve / Assemble | Multi-query, decomposition (9.5 Retrieval quality: hybrid search, reranking, query transforms and metrics, 9.7 Advanced RAG patterns) |
Math check. Retrieval quality is a ceiling on answer quality. If the relevant chunk is in the top- for only 70% of queries (recall@ = 0.7, 9.5 Retrieval quality: hybrid search, reranking, query transforms and metrics), no prompt can make the system right on more than roughly 70% of the questions that need that chunk. Measure retrieval on its own before touching the prompt.
What changed between 2020 and 2026
The original RAG was a research recipe: a jointly trained retriever and generator. Today "RAG" names a design pattern: frozen frontier models, off-the-shelf embedding models, hybrid retrieval with rerankers, LLM-written chunk context (9.4 Chunking and ingestion), structured outputs, and increasingly agentic retrieval, where the model decides when and what to search, iterates, and uses other tools such as SQL or web search (9.7 Advanced RAG patterns, 10.2 Tool use and function calling). Context windows of hundreds of thousands of tokens and prompt caching moved the boundary — more systems retrieve whole documents or sections instead of tiny fragments — but did not remove the need to choose which documents to read. Retrieval is also the backbone of agent memory (10.4 Memory and context engineering).
Where this shows up later
- Embeddings and vector search — 9.2 Embeddings deep dive, 9.3 Vector search and indexes; chunking — 9.4 Chunking and ingestion; retrieval quality — 9.5 Retrieval quality: hybrid search, reranking, query transforms and metrics.
- Grounded generation, citations and tool calling — 9.6 Generation side: context assembly, citations, structured outputs and tool calling; advanced patterns — 9.7 Advanced RAG patterns; evaluation and production — 9.8 Evaluating and observing LLM apps, 9.9 Production: latency, cost, caching, guardrails, security and privacy.
- Agents treat retrieval as one tool among many — 10.2 Tool use and function calling; memory is retrieval over an agent's own past — 10.4 Memory and context engineering.
Common pitfalls (4)
- Starting with a vector database before establishing a baseline — paste the documents into a long-context prompt first and measure; many projects never need more.
- Blaming the model for wrong answers when the relevant chunk was never retrieved; check recall@k before changing prompts.
- Treating fine-tuning as a way to teach facts; it changes behaviour and style far more reliably than it stores knowledge.
- Assuming a bigger context window makes retrieval unnecessary — you still have to choose which documents to read, and you pay for every token every time.
Recap: all 7 key points
- A frozen LLM has three gaps — stale, public-only and ungrounded knowledge; RAG fetches evidence at query time so the model answers from text it can cite.
- Lewis et al. (2020) wrote RAG as p(y|x) ≈ Σ_z p(z|x) p(y|x,z); modern RAG concatenates the top-k chunks and lets attention do the weighting.
- Knowledge problems → RAG; behaviour problems → fine-tuning; a small corpus queried rarely → long context with prompt caching. Real systems combine all three.
- The pipeline has an offline half (ingest, parse, chunk, embed, index) and an online half (retrieve, rerank, assemble, generate, cite), with evaluation wrapped around everything.
- Retrieval quality is a ceiling: if the needed chunk is not in the top-k, no prompt can save the answer — measure recall@k before tuning prompts.
- The seven failure points (missing content, missed top rank, not in context, not extracted, wrong format, wrong specificity, incomplete) map each symptom to a pipeline stage.
- By 2026 RAG is a design pattern, not a model: hybrid retrieval, rerankers, contextual chunks, structured outputs and agentic, tool-driven retrieval.
Watch
The seven-minute mental model; watch first.
Read
Exercises
- Zero-infrastructure baseline
Take a 20–40 page document you know well. Paste it whole into a long-context model and ask ten questions, including two it cannot answer. Record correctness, refusals, latency and the token cost. This is the baseline every RAG system in this phase must beat.
- Classify ten use cases
Write ten realistic scenarios (support bot over a product manual, a model that must write in your company's tone, a one-off analysis of a 50-page contract, a coding assistant for a private repository...). For each, choose RAG, fine-tuning, long context or a combination and justify the choice with the comparison table: freshness, attribution, cost per request, update cost.
- Trigger the failure points
Using any RAG tutorial or the minimal pipeline from 9.6 Generation side: context assembly, citations, structured outputs and tool calling, deliberately produce FP1 (ask about something not in the corpus), FP2 (ask with a synonym the embedding model misses) and FP3 (set the context budget too small for the retrieved chunks). Write down the symptom you observed for each so you recognize them later.
Quiz
Flashcards
These cards are now in your spaced-repetition queue. Review all due cards →