Contextual Retrieval, step by step: stop your AI search quoting the wrong report
From Adam Rosler, AI engineer. The one you commented CHUNK for.
What's happening
Most "AI search over your docs" (RAG) works like this: every document is cut into small chunks, each chunk is turned into a vector (an "embedding") on its own, and when someone asks a question, the system fetches the chunks whose vectors are closest to the question and hands them to the model.
The problem is the cutting. A chunk like "The company's revenue grew by 3% over the previous quarter" has lost the two things that matter: which company and which quarter. Cut out of its report, it looks almost the same as every other quarter's revenue sentence, so search can fetch the wrong one, and the model answers confidently from it.
A tiny demo shows it: four short quarterly reports (made-up companies), one question, "How much did Kestrel Air's revenue grow in Q2 2024?" With plain chunks, the right chunk ranked 5th. Above it were the Q1 chunk ("grew by 5%") and another company's chunk ("grew by 11%"). Hand the top results to a model and "5%" is the likely answer: the wrong quarter.
The fix: Contextual Retrieval
Anthropic published this in September 2024. Before you embed each chunk, ask a model to write one short line that puts the chunk back in its document, and glue that line to the front of the chunk:
Kestrel Air's Q2 2024 quarterly report: revenue rose 3% quarter-on-quarter to £212 million, alongside a 6% fall in fuel costs and two new June routes. The company's revenue grew by 3% over the previous quarter, to £212 million.
Run the same demo with Anthropic's method, those lines added, and the right chunk ranks 1st.
Anthropic's prompt is simple: give the model the whole document and the chunk, and ask for "a short succinct context to situate this chunk within the overall document for the purposes of improving search retrieval of the chunk", answering "only with the succinct context and nothing else". The line is usually 50-100 tokens.
Why it matters (Anthropic's numbers)
Measured as the share of searches where the right chunk was not in the top 20 results:
| Setup | Failed retrievals | Reduction |
|---|---|---|
| Plain embeddings | 5.7% | - |
| + context lines (contextual embeddings) | 3.7% | 35% fewer |
| + keyword search on the same chunks (BM25) | 2.9% | 49% fewer |
| + a reranker on top | 1.9% | 67% fewer |
Step by step
- Write a context line for every chunk when you index (not at question time). Use a small, cheap model; send it the whole document plus the chunk.
- Embed the line together with the chunk, and put the same text into a keyword index (BM25), which catches exact names, IDs and numbers that embeddings miss.
- Rerank: fetch a wider set (Anthropic used 150), let a reranking model pick the best 20.
- Watch the cost: it's one extra model call per chunk, paid once at indexing. With prompt caching (the document is cached and reused for all its chunks), Anthropic estimated about $1.02 per million document tokens.
- Maybe skip RAG entirely: if your whole knowledge base is under about 200,000 tokens (around 500 pages), you can often just put it all in the prompt.
The code
rag_demo.py (on this page) is the whole demo in about 120 lines: four tiny reports, the chunks, a local
embedding model (all-MiniLM-L6-v2), and one model call per chunk for its context line, with Anthropic's prompt. Swap in
your own documents and question to see the ranking move.
The short version
Chunks don't remember where they came from. Tell them, in one line, before you index.
Source: Anthropic, "Introducing Contextual Retrieval" (19 September 2024). The demo: four made-up reports, the all-MiniLM-L6-v2 embedding model, context lines written by Claude Haiku with Anthropic's prompt (run 25 November 2026).
Files for this breakdown
Unlock the full CHUNK breakdown + downloadable runnable scripts
Drop your email once to read the complete step-by-step guide, grab the files, and unlock all 96 Reel breakdowns on this device forever.