What you need to know
Retrieval-augmented generation (RAG) answers questions from a body of knowledge too large, too specialized or too changeable to rely on the model's training. The pipeline has two halves:
- Indexing (offline): collect documents, split them into chunks, compute representations, and store them in an index.
- Answering (online): turn the question into a query, retrieve candidate chunks, optionally rerank them, and pass the best ones to Claude with instructions to answer from them and cite.
Remember lesson 1.2: retrieval is for stable knowledge. Live state comes from tools.
Choose the architecture first
The first decision is not how to chunk, but which retrieval architecture the workload needs.
| Architecture | How it works | Fits |
|---|---|---|
| Full-corpus context | Load the whole knowledge base into the prompt, usually with prompt caching | A small, stable corpus (a few hundred pages at most) and questions that may need any part of it |
| Pre-built RAG pipeline | Index the corpus offline; retrieve the best chunks per question | A large or growing corpus, high query volume, tight latency and cost limits |
| Agentic search with tools | The model searches, opens and follows references step by step | Codebases, file trees, fast-changing collections, investigative or multi-hop questions where volume is low |
Corpus size, how often it changes, the query pattern and the latency and cost limits decide between them. Full context is simplest and accurate while the corpus fits; a pipeline earns its complexity at scale; agentic search always sees current data and can follow a trail, at more latency and cost per question.
Chunking: by the shape of the documents
The chunk is the unit that comes back. Choose the method by what the documents are, not by a default size.
| Method | How | Fits |
|---|---|---|
| Fixed-size with overlap | Equal spans, overlapping a little | Uniform, unstructured text with weak natural boundaries |
| Semantic | Split where the topic changes | Prose, where each chunk must make sense on its own |
| Structural (hierarchical) | Follow the document's sections and subsections | Manuals, contracts, policies, where the section heading carries meaning |
A common failure: a chunk loses the context that made it meaningful ("the limit is 30 days", but for which policy?). Fixes include keeping section titles and document names with each chunk, and contextual retrieval, which prepends a short, generated description of where the chunk sits in its document before indexing it.
Indexing: by how people ask
| Index | Matches | Fits |
|---|---|---|
| Dense (embeddings) | Meaning, even with different words | Questions phrased unlike the source text |
| Sparse (keyword, such as BM25) | Exact terms: part numbers, codes, names | Questions that hinge on a specific identifier |
| Hybrid | Both, with results merged | Mixed query patterns, which is most production traffic |
Match the method to the data shape
| Data shape | Examples | Retrieval method |
|---|---|---|
| Unstructured text | Manuals, policies, articles, notes | Chunked index: dense, sparse or hybrid |
| Structured records | Orders, claims, accounts, transactions | A query or API call with filters; never embeddings over rows |
| Tables and metrics | Sales figures, KPIs | SQL against the warehouse (the model can draft the query; code runs it with read-only, scoped access) |
| Live state | Stock, status, balances | A tool call to the owning system at question time |
| Codebases and file trees | Repositories, shared drives | Agentic search: the model lists, greps and opens files step by step |
Many assistants need several of these, behind one router or exposed as several tools.
Match the method to the query pattern
| Query pattern | What helps |
|---|---|
| Paraphrased, conceptual ("why does my table wobble?") | Dense embeddings |
| Exact identifiers (codes, clause numbers, names) | Keyword or hybrid search |
| Scoped ("in the 2025 policy wording", "for this product") | Metadata filters applied before ranking |
| Multi-part or multi-hop ("compare the excess in the old and new wordings") | Query decomposition or an agent that retrieves in several steps |
| High stakes, needs evidence | Citations to the retrieved passages, and quote-first answers |
Improving precision
- Metadata filters (product, version, region, date, document type) remove whole classes of wrong answers before ranking.
- Contextual retrieval adds a short description of each chunk's place in its document before indexing, which reduces misses from context-poor chunks; combined with hybrid search it lowers retrieval failures substantially.
- Reranking takes the top few dozen candidates and reorders them with a stronger relevance model, keeping only the best handful. It improves quality and adds latency and cost per query, so it belongs where accuracy matters more than milliseconds.
- Query rewriting turns a conversational question into a better search query, using the conversation so far.
Fallback configuration
Retrieval sometimes returns nothing useful: no chunk scores above the threshold, or the top results disagree. Decide what happens then, before production.
- Widen before giving up: retry with a rewritten query, drop an optional filter, or fall back from dense to keyword search for identifiers.
- Say so when evidence is missing: answer "I could not find this in the policy library" rather than letting the model fill the gap from training data.
- Route to a person or another source for high-stakes questions with no supporting passage.
Three trade-offs to name
Every retrieval design balances retrieval quality (does the right chunk come back?), latency (how much time retrieval adds) and maintenance (how much it costs to keep the index correct as documents change). Reranking improves quality and adds latency; finer chunking improves precision and increases index size; frequent re-indexing improves freshness and costs compute.
Keeping it fresh
Decide how changes reach the index: re-index changed documents on publish, on a schedule, or both; remove withdrawn documents; record the source version in each chunk so citations point to the right edition.
In the exam
Retrieval items give the corpus (size, structure, how often it changes), the query pattern, and latency and cost limits, then ask which architecture fits, or describe a failure and ask for the fix. Choose full-corpus context for a small stable corpus, a pipeline for large or high-volume workloads, and agentic search for codebases and investigative questions. When indexing is chosen, match chunking to document shape and indexing to query pattern, add contextual retrieval and filters for precision, and specify the fallback when nothing relevant comes back. Distractors apply one method to everything or answer without evidence.
Check your understanding
Technicians search a manual library using exact fault codes such as "E-4417". Embedding search often returns similar-sounding but wrong codes. What fixes this?
Show answer
Answer: D. Exact identifiers are what keyword (sparse) indexes match well. A hybrid index keeps the semantic matching for other questions and recovers exact codes.
Answers about insurance policies mix up conditions from different sections. The policies are split into fixed 400-token chunks. What is the best fix?
Show answer
Answer: C. Structured documents should be chunked along their structure, with the context that gives each chunk its meaning. Smaller fixed chunks lose even more context.
A team has 120 pages of reference material that rarely changes and is used on every request. Which TWO options are sound?
Show answer
Answer: A, C. A small, stable corpus may fit the context window, where caching makes it cheap. Retrieval should be added only if measurement shows a need.
Apply it: Design Hollis & Grant's product knowledge pipeline
Hollis & Grant's product-question route needs retrieval over 2,300 product manuals (PDFs with sections such as assembly, care and dimensions) and 400 help-centre articles updated weekly. Customers ask in their own words ("my table wobbles"), and sometimes quote product codes ("CV-2210-OAK").
- Choose chunking for each source, with a reason.
- Choose the index type and justify it with the query patterns.
- Design freshness: how revised manuals and weekly article updates reach the index, and how citations stay correct.
- Define two retrieval metrics you will track.
Done when: each metric can be computed without reading every answer by hand.
Show model answer
1. Chunking
| Source | Chunking | Reason |
|---|---|---|
| Product manuals | Structural: by section, with product name, product code and section path attached | Answers depend on the section ("assembly" vs "care") and on which product |
| Help-centre articles | One chunk per article, split by heading only when long | Articles are short and self-contained |
Contextual descriptions are generated for manual chunks so a chunk like "tighten bolts B and C" carries "Assembly, CV-2210 dining table".
2. Index. Hybrid: dense embeddings for problem descriptions in customers' words, plus keyword matching for product codes and part letters, with results merged and the top few reranked before generation.
3. Freshness. Manuals are re-indexed when the catalogue marks a product revision, removing the old version's chunks; articles are re-indexed on publish each week. Each chunk stores the document ID and version, and answers cite them, so a citation always points to the edition that was used.
4. Metrics. Recall at 5 on a labelled set of 300 real questions (is a correct chunk among the top five?), and the share of answers whose citation points to the right product, measured automatically from the product code in the question and in the cited chunk.