What you will be able to do
- Explain how contextualizing chunks fixes the isolated-chunk failure and what it costs
- Choose between contextual embeddings, hybrid search and a reranking stage using accuracy, latency and per-query cost
- Recognise when an agent should query data at runtime through tools instead of relying on a pre-built index
1.Fixing the isolated chunk: contextual embeddings and contextual BM25
Chunking makes large corpora searchable, but it removes context. The Contextual Retrieval post uses an SEC-filing example. A user asks for ACME Corp's revenue growth in Q2 2023. The relevant chunk reads "The company's revenue grew by 3% over the previous quarter." Nothing in that chunk names the company or the quarter, so neither embeddings nor BM25 can reliably connect it to the question.
Contextual Retrieval fixes this when the corpus is ingested. Claude writes a short note placing each chunk within its parent document, usually 50–100 tokens. That note is prepended to the chunk before it is embedded and before the BM25 index is built. This is the prompt Anthropic used:
<document>
{{WHOLE_DOCUMENT}}
</document>
Here is the chunk we want to situate within the whole document
<chunk>
{{CHUNK_CONTENT}}
</chunk>
Please give a short succinct context to situate this chunk within the overall document for the purposes of improving search retrieval of the chunk. Answer only with the succinct context and nothing else.Generic summaries didn't help much. Context written for each chunk did. Across codebases, fiction, ArXiv and science papers, contextual embeddings cut the top-20 retrieval failure rate by 35%. Adding contextual BM25 brought the reduction to 49%. The cost is paid once, at ingestion. With prompt caching, the full document is cached once and reused for each of its chunks, which puts the post's estimate at $1.02 per million document tokens.
The post lists a few things to tune for your data. Chunk size, boundaries and overlap all affect results. Custom contextualizer prompts can do better than the generic one, for example by including a glossary of terms that are defined in other documents. Some embedding models also gain more than others. If you'd rather not build this pipeline yourself, Anthropic's embeddings guide lists Voyage contextualized chunk models (voyage-context-4, voyage-context-3) that fold document context into chunk vectors directly.
Checkpoint 1 of 6· Check yourself
In Contextual Retrieval, where does the generated chunk context go?
The technique has two parts, Contextual Embeddings and Contextual BM25. Both use the contextualized chunk.
“is prepended to the chunk before embedding it and before creating the BM25 index”Source: www.anthropic.com
2.Reranking, and choosing a stack by accuracy, latency and cost
Reranking adds a second stage at query time. Initial retrieval methods are tuned to be fast across millions of documents. A reranking model is slower but more precise, so it is run only on a small shortlist. Anthropic's experiment took the top 150 candidates and scored each one against the query with a reranker. It kept the top 20. Reranked contextual embeddings plus contextual BM25 cut the top-20 failure rate by 67%.
Checkpoint 2 of 6· Put it in order
Order the steps of the reranking stage as Anthropic ran it.
- 1.Pass those chunks, along with the user's query, through the reranking model
- 2.Perform initial retrieval to get the top candidate chunks (150 in the experiment)
- 3.Score each chunk for relevance and keep the top-K (20 in the experiment)
- 4.Pass the top-K chunks into the model as context to generate the result
Retrieval casts a wide net first. The reranker then narrows it to the most relevant chunks before generation.
“Perform initial retrieval to get the top potentially relevant chunks (we used the top 150);”Source: www.anthropic.com
Each layer improves accuracy, and each one has a different cost. The cookbook measured them on 9 codebases and 248 queries:
| Approach | Pass@5 | Pass@10 | Pass@20 |
|---|---|---|---|
| Baseline RAG | 80.92% | 87.15% | 90.06% |
| + Contextual Embeddings | 88.12% | 92.34% | 94.29% |
| + Hybrid Search (BM25) | 86.43% | 93.21% | 94.99% |
| + Reranking | 92.15% | 95.26% | 97.45% |
The cookbook prices each layer. Contextual embeddings cost about $3 once, at ingestion, for this dataset. Hybrid search needs Elasticsearch infrastructure to run and maintain. Reranking adds 100–200ms of latency and about $0.002 in API cost to every query. From that, the cookbook gives three profiles:
| Requirement | Recommended stack | Pass@10 | Cost profile |
|---|---|---|---|
| High-volume, cost-sensitive | Contextual embeddings alone | 92% | No per-query costs |
| Maximum accuracy, latency-tolerant | Full reranking pipeline | 95% | Per-query latency and API cost |
| Balanced production system | Hybrid search | 93% | Infrastructure, no per-query costs |
Within reranking there is a further setting: how many chunks to rerank. Reranking more chunks improves accuracy and costs more latency. Anthropic's embeddings guide shows the same choice at the model level. Voyage offers rerank-2.5 for highest accuracy and rerank-2.5-lite, which is optimized for latency and cost.
Checkpoint 3 of 6· Check yourself
A consumer app handles millions of queries per day on a tight per-query budget. Accuracy matters, but no per-query spend can be added. Which stack does the cookbook point to?
Contextual embeddings cost only at ingestion and reached 92% Pass@10. Reranking adds a cost and latency to every query.
“High-volume, cost-sensitive: Contextual embeddings alone (92% Pass@10, no per-query costs)”Source: platform.claude.com
Checkpoint 4 of 6· Exam question
A research agent runs for several hours, repeatedly retrieving and discussing documents, and the conversation is approaching its context window limit while still needing to reference earlier findings. Which strategy matches this query pattern?
Correct answer: A — Enable server-side compaction so earlier turns are automatically summarized while the session continues past the limit.
- A. Correct. Server-side compaction is the primary mechanism for long-running, retrieval-heavy sessions approaching the context limit: it summarizes earlier turns automatically so the conversation can continue while preserving prior findings.
- B. Raising max_tokens changes how much a single turn can output, not how much accumulated conversation history fits in the window, so it does not address the approaching limit.
- C. Re-uploading every prior document each turn accelerates hitting the context limit rather than managing it, and duplicates content already in the conversation history.
- D. Deliberately choosing a smaller context window shrinks the working space available and shifts summarization work onto the user instead of using built-in context management.
3.Beyond a pre-built index: just-in-time retrieval through tools
Everything so far builds an index ahead of time. Anthropic's context-engineering guidance describes another approach, which suits large databases and data that changes often. The agent holds lightweight references, such as file paths, stored queries and links, and loads data through tools only when it needs it. Claude Code works this way when analysing large databases. It writes targeted queries, stores the results and inspects them with head and tail, so the full data never has to enter the context window.
This retrieval is iterative. In the guide's words, each interaction yields context that informs the next decision, so the agent builds understanding step by step. The sources don't use the term "multi-hop retrieval" or compare it with single-hop lookup. What they do describe is an agent that keeps querying until it has enough. The cost is speed: runtime exploration is slower than retrieving pre-computed data, and without good tools and guidance an agent can waste context on dead ends.
A middle path is the hybrid strategy. Load some data up front for speed and let the agent explore the rest. Claude Code puts CLAUDE.md files into context at the start and uses glob and grep to fetch files as needed, which avoids stale indexes. The guide suggests this hybrid fits less dynamic content, such as legal or finance work. On the API side, a custom retrieval tool can return results as search_result blocks, which lets Claude cite your sources:
{
"type": "search_result",
"source": "https://example.com/article", // Required: Source URL or identifier
"title": "Article Title", // Required: Title of the result
"content": [
// Required: Array of text blocks
{
"type": "text",
"text": "The actual content of the search result..."
}
],
"citations": {
// Optional: Citation configuration
"enabled": true // Enable/disable citations for this result
}
}Checkpoint 5 of 6· Check yourself
An agent must answer analytical questions over a very large database whose contents change daily. Which retrieval approach matches the pattern Anthropic describes for Claude Code?
Just-in-time retrieval keeps references and fetches only what each step needs. This avoids stale indexes and the context cost of loading all the data.
“The model can write targeted queries, store results, and leverage Bash commands like head and tail to analyze large volumes of data”Source: www.anthropic.com
Checkpoint 6 of 6· Exam question
A platform wants Claude to discover tools using an existing embeddings index rather than the built-in regex or BM25 matching, because tool descriptions are sparse but semantically related through the embedding space. Which implementation matches this requirement?
Correct answer: A — Build a custom tool that runs the embeddings search and returns tool_reference blocks in its tool_result for Claude to expand.
- A. Correct. A custom client-side tool search implementation can run any retrieval method, including embeddings, and return standard tool_result content with tool_reference blocks, which the API expands into full tool definitions.
- B. BM25 is a term-frequency-based lexical ranking method, not a true semantic embedding similarity search, so it would not reproduce the intended embeddings-based matching.
- C. Regex matching operates on literal patterns against names and descriptions and cannot capture semantic relationships between sparse, differently worded tool descriptions.
- D. Loading every definition up front abandons retrieval entirely and reintroduces the context bloat problem that made an embeddings-based discovery approach necessary.
Exam traps
Each one states something that sounds right. Open it to see what is actually true.
1.Prepending a generic summary of the whole document to each chunk gets the same benefit as Contextual Retrieval.Why is that wrong?
Anthropic tested generic summaries and saw very limited gains. The improvement comes from short context that Claude writes for each individual chunk.
Covered in Fixing the isolated chunk: contextual embeddings and contextual BM25
2.Because reranking scores best, it should be the default for every production RAG system.Why is that wrong?
Reranking adds latency and API cost to every query. For high-volume, cost-sensitive systems the cookbook recommends contextual embeddings alone.
Covered in Reranking, and choosing a stack by accuracy, latency and cost
Sources
Every claim above is drawn from one of these pages, quoted as it was written on the date shown.
- 1.
“contextualized chunk embedding models produce chunk-level vectors that capture full document context without manual metadata augmentation”
↩︎ Fixing the isolated chunk: contextual embeddings and contextual BM25“Highest accuracy. Recommended for most applications.”
↩︎ Reranking, and choosing a stack by accuracy, latency and cost - 2.https://www.anthropic.com/engineering/contextual-retrievalSecondary source
“prepending chunk-specific explanatory context to each chunk before embedding”
↩︎ Fixing the isolated chunk: contextual embeddings and contextual BM25“the one-time cost to generate contextualized chunks is $1.02 per million document tokens.”
↩︎ Fixing the isolated chunk: contextual embeddings and contextual BM25“including a glossary of key terms that might only be defined in other documents in the knowledge base”
↩︎ Fixing the isolated chunk: contextual embeddings and contextual BM25“There is an inherent trade-off between reranking more chunks for better performance vs. reranking fewer for lower latency and cost.”
↩︎ Reranking, and choosing a stack by accuracy, latency and cost“adding generic document summaries to chunks (we experimented and saw very limited gains)”
↩︎ Exam trap 1“adding generic document summaries to chunks (we experimented and saw very limited gains)”
↩︎ Prediction“is prepended to the chunk before embedding it and before creating the BM25 index”
↩︎ Checkpoint“Perform initial retrieval to get the top potentially relevant chunks (we used the top 150);”
↩︎ Checkpoint - 3.
“Reranking models are slower but more accurate—they can afford to do deeper analysis on a smaller candidate set.”
↩︎ Reranking, and choosing a stack by accuracy, latency and cost“Reranking: Adds 100-200ms query latency and per-query API costs (~$0.002 per query)”
↩︎ Exam trap 2“High-volume, cost-sensitive: Contextual embeddings alone (92% Pass@10, no per-query costs)”
↩︎ Checkpoint - 4.
“From tool calls: Your custom tools return search results, enabling dynamic RAG applications”
↩︎ Beyond a pre-built index: just-in-time retrieval through tools - 5.
“use these references to dynamically load data into context at runtime using tools”
↩︎ Beyond a pre-built index: just-in-time retrieval through tools“runtime exploration is slower than retrieving pre-computed data”
↩︎ Beyond a pre-built index: just-in-time retrieval through tools“The hybrid strategy might be better suited for contexts with less dynamic content, such as legal or finance work.”
↩︎ Beyond a pre-built index: just-in-time retrieval through tools“The model can write targeted queries, store results, and leverage Bash commands like head and tail to analyze large volumes of data”
↩︎ Checkpoint