Topic 3 of 5 · 4 min read
Contextual retrieval: situating each chunk
Goal: Write the contextualizing prompt for a chunk and explain how the added context fixes a retrieval miss in a given example.
Before embedding a chunk, have a model write one or two sentences that place it inside its document, and prepend them to the chunk, so the chunk keeps the context that chunking cut away.
Check yourself · 1 of 3
What does contextual retrieval add to each chunk before it is embedded?
Why it matters
Chunking splits a document into pieces, and each piece loses its surroundings. Take the chunk "Revenue grew 3% over the previous quarter." Which company? Which quarter? The sentence is correct and unsearchable. A query for "ACME Corp Q2 2024 revenue growth" has little in common with it. Contextual retrieval, described by Anthropic in 2024, fixes this at index time. In their tests, contextual embeddings plus contextual BM25 cut the top-20 failed-retrieval rate by 49%, and by 67% when combined with reranking. Those numbers come from their datasets, so test on yours.
How to do it
- For each chunk, send the model the whole document (or the relevant section) and the chunk.
- Ask for a short context, 50 to 100 tokens, that situates the chunk, and nothing else.
- Store
context + chunkas the text you embed and index for BM25. Show the user and the answering model the original chunk, or both, as you prefer. - Use prompt caching: the document is the same for every chunk, so cache it once and pay the full price only for the chunk part of each call. Anthropic's estimate was about $1.02 per million document tokens with caching, under their stated assumptions of 800-token chunks and 8k-token documents.
The prompt:
<document>
{{WHOLE_DOCUMENT}}
</document>
Here is the chunk we want to situate within the whole document:
<chunk>
{{CHUNK_CONTENT}}
</chunk>
Give a short, succinct context to situate this chunk within the overall document, to improve search retrieval of the chunk. Answer only with the context and nothing else.
Before and after
Chunk: "The company's revenue grew by 3% over the previous quarter." Added context: "This chunk is from ACME Corp's Q2 2023 SEC filing; the previous quarter's revenue was $314 million." Now the text contains the company, the period and a figure, and a query that names ACME and Q2 2023 can match it by both meaning and exact words.
What goes wrong
- Long, chatty context that drowns the chunk. Keep it to a sentence or two.
- Invented context: the model adds facts not in the document. Spot-check a sample, and keep the instruction to use only the document.
- No caching, so the full document is billed again for every chunk.
- Re-indexing everything on each edit instead of only the changed documents.
Examples
A chunk that could belong to anyone
Original chunk: The fix is to set the timeout to 30 seconds.
Added context: From the Payments API troubleshooting guide, section on webhook delivery failures.
A query for "payments webhook timeout" now has words to match, while the bare chunk had none.
The context call as a chat
System: You write retrieval context. Use only the document.
User: <document> full guide </document> <chunk> The fix is to set the timeout to 30 seconds. </chunk> Give a short context that situates this chunk in the document. Answer only with the context.
Assistant: This chunk is from the Payments API troubleshooting guide, in the section on webhook delivery failures.
Key points
- Chunks lose their surroundings, so a correct sentence can be unsearchable without the company, date or section it came from.
- Contextual retrieval prepends a short model-written context to each chunk before embedding and BM25 indexing.
- Keep the added context to one or two sentences and tell the model to answer with the context only.
- Prompt caching lets the whole document be reused across its chunks, which keeps the indexing cost low.
- Anthropic reported 49% fewer failed retrievals, 67% with reranking, on its own datasets, so measure on yours.
Common mistakes
- Letting the model write a paragraph of context, which swamps the original chunk.
- Not telling the model to use only the document, so it invents background.
- Skipping prompt caching and paying for the full document on every chunk.
- Adding context to the embedding text but not to the BM25 index, losing half the benefit.
Check yourself · 2 of 3
Contextualizing 10,000 chunks from 500 documents is slow and costly. Which change cuts the cost most?
Check yourself · 3 of 3
The chunk reads "Revenue grew 3% over the previous quarter." Which added context would best fix a missed query about ACME Corp's Q2 2023 results?
Practice items, not counted towards your mastery.
Start here
- Introducing Contextual Retrieval (Anthropic) (opens in a new tab)ArticleFree
The source of the 49% and 67% fewer failed retrievals figures, the prompt and the caching cost estimate.
- Reciprocal Rank Fusion outperforms Condorcet and individual rank learning methods (Cormack et al.) (opens in a new tab)PaperFree
The original RRF paper: the formula and the constant of 60.
- Rerank overview (Cohere docs) (opens in a new tab)DocsFree
How a hosted rerank endpoint takes a query and documents and returns relevance scores.