Deployed Lab

Module 2 · Core patterns · Chapter 05 · Free

Retrieval patterns: RAG variants and when each one fits

18 min1 clickable picture1 hands-on lab1 predict, 2 quick quizzes, 5-question final quiz5-min video review
In 30 seconds

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 card

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.

The cold open Meridian Foods is a made-up company. Its HR assistant launched with naive RAG over policy PDFs. The model was fine. The retrieval rung was wrong for facts that change and carry exact terms.
The plan for this chapter
  1. Walk the ladder in the picture.
  2. Rungs 1 and 2: no retrieval, and naive RAG.
  3. Rungs 3 and 4: hybrid with reranking, and fixing the question first.
  4. Rungs 5 and 6: agentic and structured retrieval, then the decision table.
  5. 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:

The retrieval ladder A question is routed to one of six rungs: no retrieval, naive RAG, hybrid plus rerank, query rewrite, agentic search, or structured SQL. Each rung feeds the answer. Questionfrom the user Pick a rungone per question No retrieval Naive RAG Hybrid + rerank Query rewrite Agentic search Structured SQL Answerwith sources
  1. Before any search, judge what kind of fact would settle it.
  2. If the model already knows it, or the user pasted the text, no retrieval runs.
  3. Embed, match, stuff the top chunks into the prompt.
  4. Codes and names need keyword search plus a reranker; meaning match blurs exact tokens.
  5. When users word it unlike the documents, rewrite the question, then search.
  6. If the answer joins two or more sources, the model searches, judges, searches again.
  7. 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.

Rung 2: naive RAG. The classic shape, in four steps:

  1. Split documents into chunks and embed each chunk.
  2. Embed the question, take the top-k most similar chunks.
  3. Paste those chunks into the prompt.
  4. The model answers from the chunks and cites them.

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.

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.

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.

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.

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 typeVariant
General knowledge, or small and stableNo retrieval
The user pasted the documentNo retrieval, use the pasted context
Plain questions over prose documentsNaive RAG
Exact codes, names, policy numbersHybrid search plus reranking
Users word it unlike the documentsQuery rewriting, then search
Answer joins two or more sourcesAgentic retrieval
Counts and current status in tablesStructured 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:

  1. Route the questions in the lab below and read the explanation on each.
  2. 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.

What to notice

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.

Field note, November 2026

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

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.

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".