Contextual Retrieval: Anthropic’s Chunk-Context Method
Prepending chunk-specific context before embedding, and the retrieval-failure drop Anthropic measured.
Contextual retrieval (Anthropic, September 2024) prepends a short, chunk-specific situating context — usually 50–100 tokens from an LLM — to each chunk before embedding it (Contextual Embeddings) and before building BM25 (Contextual BM25). That restores the company, quarter, or section a bare chunk left out. Under Anthropic’s published size assumptions with prompt caching, the one-time cost to generate those contexts is about $1.02 per million document tokens. The late-chunking comparison lives on late chunking and contextual retrieval; this page is the Anthropic method.
How does contextual retrieval work?
Contextual retrieval is an ingest-time transform. The query path stays ordinary retrieval — only the indexed text is richer.
- Chunk the document as usual. Boundaries still matter; contextualization does not replace choosing size and overlap on chunk size.
- Situate each chunk with an LLM. Anthropic’s published Claude 3 Haiku prompt places the whole document and the chunk in tags and asks for a short succinct context that situates the chunk for search — answer with the context only (Anthropic engineering post, 19 September 2024).
- Prepend that context to the chunk. The situating text is usually 50–100 tokens.
- Embed and index the contextualized text. Dense embeddings and BM25 both see the prepended terms.
Worked transform from Anthropic’s SEC-filing example: the original chunk “The company’s revenue grew by 3% over the previous quarter” becomes a contextualized chunk that opens with “This chunk is from an SEC filing on ACME corp’s performance in Q2 2023; the previous quarter’s revenue was $314 million…” before the same sentence. At query time you search those enriched units — you do not re-run the situate prompt per user question.
What is Contextual Embeddings versus Contextual BM25?
Anthropic names two sub-techniques that share the same prepended text:
- Contextual Embeddings embed the contextualized chunk so dense similarity can match on situating entities (company, quarter, section) that never appeared in the bare passage.
- Contextual BM25 builds the lexical index on the same contextualized string, so those situating terms are also exact-match searchable.
Their stack is embeddings alone, then embeddings plus Contextual BM25 (hybrid on contextualized text), then an optional reranker. The fusion maths for BM25 plus dense live on hybrid search; the lexical scorer itself is on BM25.
What retrieval failure-rate cuts did Anthropic measure?
Anthropic’s metric is 1 − recall@20 — the top-20-chunk retrieval failure rate — averaged across their knowledge domains with Gemini Text 004 embeddings (engineering post, 19 September 2024). Domains included codebases, fiction, ArXiv papers, and science papers.
| Stack | Failure rate | Relative cut vs baseline |
|---|---|---|
| Baseline (their top embedding config) | 5.7% | — |
| Contextual Embeddings | 3.7% | 35% cut |
| + Contextual BM25 | 2.9% | 49% cut |
| + Cohere rerank (top 150 → top 20) | 1.9% | 67% cut |
These are failure-rate cuts on their eval, not a universal “35% accuracy” claim — that shorthand appears in secondary blogs and misstates the unit. They are also not interchangeable with Jina’s BEIR nDCG@10 late-chunking numbers; different metrics and datasets. Prove the lift on your own labelled set via chunking evaluation. Reranker choice and latency depth live on reranking.
What does contextual retrieval cost?
The expensive step is one-time, at ingest — not per query.
- Situating cost. With prompt caching (document loaded once), Anthropic published $1.02 per million document tokens assuming 800-token chunks, 8k-token documents, 50-token instructions, and 100 tokens of context per chunk (September 2024).
- Larger indexed text. Every stored chunk carries the original plus the prepend, so embedding tokens and BM25 document length rise.
- Query path. Situating does not add an LLM call per user question. Hybrid search and reranking add their own query-time cost and latency — Anthropic notes the rerank trade-off explicitly.
Verify $ and latency on your stack
The $1.02/M figure and model names (Claude 3 Haiku, Gemini Text 004, Cohere rerank) are Anthropic’s published assumptions as of September 2024. Provider prices and embedding APIs move; put a real number on your ingest volume before committing.
When should you use contextual retrieval?
Use contextual retrieval when bare chunks lose the entities that make them findable — and skip the whole RAG stack when the corpus is small enough to prompt whole.
- Strong fit: long docs with pronouns, cross-section references, or time-bound metrics (Anthropic’s SEC example); knowledge bases too large for a single prompt; pipelines that can afford a one-time situate pass and a BM25 index on contextualized text.
- Skip RAG first: if the knowledge base is smaller than about 200,000 tokens (~500 pages), Anthropic recommends putting the whole corpus in the prompt with caching instead of retrieval.
- Prefer late chunking instead when you control a long-context mean-pooling embedder and refuse per-chunk LLM spend — decision depth on late chunking vs contextual retrieval.
Anthropic’s implementation notes still apply: tune chunk boundaries, consider domain-specific situate prompts, and always run evals. In their tests, passing the top 20 chunks beat top 5 or 10 — still experiment on your use case.
How does contextual retrieval differ from late chunking?
Both fix lost context; they attach that context in different places. Contextual retrieval writes literal tokens and prepends them — it works with any embedder and also lifts BM25, at the price of an LLM call per chunk at ingest. Late chunking runs the embedding model over a long window first, then mean-pools inside each chunk span — no prepended text, no situate LLM, but you need a long-context mean-pooling embedder. Full comparison, BEIR nDCG figures, and the choose-when frame live on late chunking and contextual retrieval. Neither method replaces readable chunk spans for the generator.
How do you implement contextual retrieval?
The pattern is: situate each chunk with the published prompt → prepend → embed the contextualized text → build BM25 on the same text → optionally rerank. Anthropic’s Claude cookbook, DataCamp’s LangChain tutorial, and Together.ai’s hybrid demo all follow that shape. Contextualized vectors land in stores such as Weaviate, Qdrant, Pinecone, or Milvus, with a sparse/BM25 index beside them for Contextual BM25. For a minimal runnable pipeline see building the pipeline; for hybrid fusion see hybrid search; for the rerank stage that produced Anthropic’s 67% failure-rate cut see reranking; for the retrieval cluster see retrieval.
What is contextual retrieval?
Contextual retrieval (Anthropic, September 2024) prepends short, chunk-specific LLM-written context — usually 50–100 tokens — to each chunk before embedding (Contextual Embeddings) and before BM25 (Contextual BM25). It restores situating details a bare chunk left out, such as company name or quarter.
What is the difference between Contextual Embeddings and Contextual BM25?
Both use the same prepended situating text. Contextual Embeddings put that text into the dense vector. Contextual BM25 indexes the same contextualized string for lexical search so situating terms are exact-match searchable. Anthropic’s best reported stack combines both, then optionally reranks.
What did Anthropic measure?
On their eval (1 − recall@20, Gemini Text 004, top-20), Contextual Embeddings cut the failure rate 35% (5.7% → 3.7%); adding Contextual BM25 reached a 49% cut (5.7% → 2.9%); adding a Cohere rerank reached 67% (5.7% → 1.9%). These are failure-rate cuts on their domains as of 19 September 2024 — not a universal accuracy percentage.
What does contextual retrieval cost?
Anthropic published about $1.02 per million document tokens as a one-time situating cost under their size assumptions with prompt caching (800-token chunks, 8k-token docs, 100-token contexts). Indexed text also grows by the prepend. The situate step does not add an LLM call per user query.
How does contextual retrieval differ from late chunking?
Contextual retrieval prepends LLM-written tokens and works with any embedder, also lifting BM25. Late chunking embeds a long window first and mean-pools inside chunk spans — no prepend, no situate LLM, but it needs a long-context mean-pooling embedder. Full comparison lives on /chunking/late.