Module 2 · Core patterns · Chapter 05 · Free
Retrieval patterns: RAG variants and when each one fits
- Retrieval means fetching facts at answer time instead of trusting model memory.
- Six rungs: no retrieval, naive RAG, hybrid search with reranking, query rewriting, agentic retrieval, structured retrieval.
- Each rung fixes one failure of the rung below, and adds cost, latency and moving parts.
The picture: a librarian who fetches only the pages your question needs, and climbs to a higher shelf only when the first one fails.
Pattern: Retrieval family. Problem: models answer from stale memory and invent facts they never saw. Forces: freshness and accuracy pull up the ladder; latency, cost and simplicity pull down. Solution shape: classify the question, fetch with the cheapest rung that can answer it, cite what came back. Watch out: adding rungs before measuring which failure you have.
- In launch week, staff asked about leave carryover. The index still held last year's policy, so the bot quoted a rule retired months earlier.
- About 214 staff (illustrative) planned leave on the wrong rule before HR caught it, and trust in the bot took months to recover.
- Walk the ladder in the picture.
- Rungs 1 and 2: no retrieval, and naive RAG.
- Rungs 3 and 4: hybrid with reranking, and fixing the question first.
- Rungs 5 and 6: agentic and structured retrieval, then the decision table.
- A worked HR example, then a routing lab.
Next: the picture, one question routed six ways.
Step 1: The ladder in one picture
Every retrieval design is a router. A question arrives, you judge what kind of fact would answer it, and you send it to one rung. Press Next to walk the routes.
Where the picture breaks down:
- A librarian reads words. Vector search matches meaning, not truth, so a confident wrong page can rank first.
- A librarian knows the shelves. Your router is code and prompts you must build and test.
- Before any search, judge what kind of fact would settle it.
- If the model already knows it, or the user pasted the text, no retrieval runs.
- Embed, match, stuff the top chunks into the prompt.
- Codes and names need keyword search plus a reranker; meaning match blurs exact tokens.
- When users word it unlike the documents, rewrite the question, then search.
- If the answer joins two or more sources, the model searches, judges, searches again.
- Counts and current status live in tables. Query the table, not text about it.
Next: the two lowest rungs, and where the simple one breaks.
Step 2: Rungs 1 and 2, no retrieval and naive RAG
Rung 1: no retrieval. Use the model's own knowledge, or context the user pastes in.
- Fits: small, stable knowledge (a 40-page FAQ that fits in the prompt), general knowledge, writing help.
- Not for: facts that change, private facts, or answers that must cite a source.
- Cost: zero retrieval latency, no index to keep fresh. The cheapest rung is the one you skip.
Rung 2: naive RAG. The classic shape, in four steps:
- Split documents into chunks and embed each chunk.
- Embed the question, take the top-k most similar chunks.
- Paste those chunks into the prompt.
- The model answers from the chunks and cites them.
- Fits: prototypes, one document set, plain questions in the documents' own wording.
- Where it breaks: wrong chunks when wording differs, a stale index after documents change, and no permissions, so search returns documents the asker may not see.
- Cost: one embedding call and a larger prompt per question, plus index upkeep.
New to RAG? The AI engineering course chapter 09-rag-end-to-end.html builds it end to end; this chapter asks which variant to pick.
Next: two rungs that fix the match, one on the results side and one on the question side.
Step 3: Rungs 3 and 4, hybrid with reranking, and fixing the question
Rung 3: hybrid search plus reranking. Run keyword and vector search together, merge the lists, then let a reranker score the short list and keep the best few.
- Fits: exact terms, codes, names, SKUs, policy numbers, mixed with normal questions.
- Not for: a tiny corpus, or purely conceptual questions where keyword match adds noise.
- Cost: a second search and a rerank call per question, plus an index holding both keyword and vector data.
Rung 4: query rewriting and multi-query. Fix the question before searching: rewrite it into the documents' terms, or generate three or four variants, search with all, and merge.
- Fits: the vocabulary gap. Staff ask "can I carry days over?" while the policy says "annual leave rollover entitlement".
- Not for: questions already precise. Rewriting adds latency and can drift the meaning.
- Cost: one extra model call before the search, plus more searches to merge in multi-query form.
Next: the two top rungs, for questions one search cannot answer and facts that are not text at all.
Step 4: Rungs 5 and 6, agentic and structured retrieval
Rung 5: agentic retrieval. The model searches, reads what came back, judges what is still missing, and searches again, inside a step budget you set.
- Fits: multi-hop questions, where the answer joins two or more sources: "Why did signups drop after the pricing change?"
- Not for: single-fact lookups. Slower, costlier, harder to debug than one search.
- Cost: several model and search rounds per question. Always cap the rounds.
Rung 6: structured retrieval. Some facts live in tables, not text: orders, stock, balances, due dates. The model writes a query or calls an API, your code validates it, real rows come back.
- Fits: counts, sums, current status, anything a spreadsheet answers exactly.
- Not for: explanations and opinions in prose documents.
- Cost: no index to keep fresh, but you pay in schema design and query safety checks.
Predict first:
Each round is a model call plus a search. On a question one search settles, extra rounds buy nothing. Match the rung to the question.
Next: the whole ladder as one decision table.
Step 5: The decision table
| Question type | Variant |
|---|---|
| General knowledge, or small and stable | No retrieval |
| The user pasted the document | No retrieval, use the pasted context |
| Plain questions over prose documents | Naive RAG |
| Exact codes, names, policy numbers | Hybrid search plus reranking |
| Users word it unlike the documents | Query rewriting, then search |
| Answer joins two or more sources | Agentic retrieval |
| Counts and current status in tables | Structured retrieval |
Rungs combine. Rewriting usually feeds hybrid search, and an agentic loop can call a structured query as one step. The table picks the main rung, not the only one.
Next: the worked example, one rung change measured.
Step 6: Worked example, an HR policy assistant
This assistant is made up: 3,000 policy documents, 12,000 questions a month (all figures illustrative). The team scores it on a golden set of 300 questions with known right answers.
Worked example:
1. Naive RAG scores 71 percent: 0.71 × 300 = 213 right, 87 wrong. Failures cluster on policy numbers and exact clause names.
2. The team adds hybrid search and reranking. Score: 86 percent, so 0.86 × 300 = 258 right. That is 258 − 213 = 45 more right answers per 300, a 15 point lift.
3. At 12,000 questions a month, the same rates mean naive RAG gets 0.29 × 12,000 = 3,480 wrong, the hybrid version 0.14 × 12,000 = 1,680 wrong: 1,800 fewer wrong answers a month.
4. The price: reranking adds 250ms per question (illustrative), taking a typical answer from 900ms to 1,150ms. Extra running cost: an illustrative $24 a month for rerank calls plus $18 for the larger index, $42 total.
5. Cost per avoided wrong answer: $42 ÷ 1,800 = about $0.023. Cheap for HR facts, where one wrong leave answer costs time and trust.
Note what the team did not do: no agent loop, no bigger model. The eval named the failure, one rung fixed it.
Next: the lab, route questions first, then design a small assistant.
Step 7: Lab, route the question, then design
Why this exercise: retrieval design is mostly routing. Classify a question in five seconds and the architecture follows.
The routing checklist (illustrative):
route(question):
known already, or pasted? -> no retrieval
fact lives in a table? -> structured retrieval
needs 2 or more sources? -> agentic retrieval
exact code or name? -> hybrid + reranking
wording unlike the docs? -> rewrite, then search
otherwise -> naive RAG
Do this:
- Route the questions in the lab below and read the explanation on each.
- Then do the design exercise on the car manual brief and compare with the model answer.
Lab: Retrieval design is mostly routing: classify a question in a few seconds and the architecture follows. This lab runs in your browser and needs JavaScript enabled.
- Four of six questions land on the three lowest rungs. Most real traffic does too.
- The most missed routes are exact terms (hybrid) and table facts (structured), because both look like normal questions.
Design exercise: a car maker wants an assistant over a 900-page owner manual plus a live service database (illustrative brief). On paper: pick a rung for manual prose, for exact warning light codes, and for "is my service due?" Then state when you would NOT retrieve at all.
Model answer
Manual prose: naive RAG, moving to hybrid when users quote warning codes exactly, since codes are literal tokens. Service due dates: structured retrieval, because the fact changes per car and lives in rows. No retrieval at all for general driving knowledge, for a page the user just pasted into the chat, and for a small stable FAQ that fits in the prompt. Skipped retrieval is latency and staleness you never pay for.
In production design reviews, a common pattern is starting with naive RAG and adding one rung at a time, each justified by a failure class in an eval set rather than by fashion. Permission filtering at retrieval time, cutting results to what the asker may see before anything reaches the prompt, is widely treated as baseline practice. Treat both as common practice, not measured statistics.
Mix-ups to avoid
- RAG is one thing. No: it is a family. Naming the variant is the design decision.
- More chunks fix wrong answers. No: if search missed, a bigger top-k returns more of the wrong neighbourhood.
- Vector search finds exact codes. No: embeddings match meaning and blur literal tokens. Keyword search owns exact match.
- Tables can just be chunked as text. No: counts and status go stale and sum wrong. Query the table.
Chapter quiz
Five questions. Score 70% or more to mark this chapter complete.
Review this chapter in 5 minutes
Watch the whole chapter recapped: the six rung ladder, hybrid search for exact terms, rewriting for wording gaps, and structured queries for table facts.
- Six rungs: no retrieval, naive RAG, hybrid plus reranking, query rewriting, agentic, structured.
- Climb only when a measured failure names the rung: exact terms mean hybrid, wording gaps mean rewriting, multi-hop means agentic, tables mean structured.
- Naive RAG breaks on wrong chunks, stale indexes and missing permission filters.
- Worked example: hybrid plus reranking moved a golden set from 71 to 86 percent, 1,800 fewer wrong answers a month, for 250ms and an illustrative $42 a month.
- Every rung added is latency, cost and upkeep. The skipped rung is free.
Decision rule: start on the lowest rung that can answer the question type, and climb only when an eval names the failure the next rung fixes.
Review
Glossary
- Retrieval
- Fetching facts at answer time from outside the model. Example: pulling the leave policy first.
- Chunk
- A small slice of a document, stored and searched on its own. Example: one manual paragraph.
- Embedding
- Numbers capturing a text's meaning, used for similarity match.
- Top-k
- The k most similar chunks a search returns. Example: top 5 pasted into the prompt.
- Hybrid search
- Keyword and vector search run together, results merged. Example: finding code E-117 and its explanation.
- Reranker
- A second scorer that reorders candidate chunks. Example: promoting the exact clause to first.
- Query rewriting
- Restating the question in the documents' terms before searching. Example: "carry days over" becomes "leave rollover".
- Multi-hop question
- A question needing two or more finds joined. Example: a deploy, then the errors after it.
- Golden set
- Fixed questions with known right answers, used to score changes. Example: 300 policy questions.
- Structured retrieval
- Facts from tables via a query or API, not text search. Example: a stock count.
Next: "Structured output and tool use patterns: answers your code can trust".