Monday AI Insight. One practical RAG lesson: a chunk that makes sense inside its document can be useless on its own. Before you embed it, tell it where it came from.
The problem: chunking strips context
Most RAG pipelines split documents into chunks of a few hundred tokens, embed each chunk, and (if you are doing hybrid search) index the same chunks with BM25. Each chunk is now searched alone.
Anthropic's example from their Contextual Retrieval post: a user asks "What was the revenue growth for ACME Corp in Q2 2023?" The right chunk in an SEC filing says:
"The company's revenue grew by 3% over the previous quarter."
Which company? Which quarter? The chunk doesn't say. The document header did, but chunking cut it off. The embedding has no "ACME" in it and BM25 has no "ACME" or "Q2 2023" to match. So the right chunk can lose to a chunk that merely mentions ACME.
The fix: prepend a short, chunk-specific context
Contextual Retrieval (Anthropic, Sep 19, 2024) is a preprocessing step. For every chunk, ask an LLM to write a short description that situates the chunk in its full document, then prepend that text to the chunk before you embed it (Contextual Embeddings) and before you build the BM25 index (Contextual BM25).
The prompt Anthropic published is roughly:
<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. The generated context is usually 50-100 tokens. The ACME chunk becomes something like "This chunk is from an SEC filing on ACME corp's performance in Q2 2023; the previous quarter's revenue was $314 million. The company's revenue grew by 3% over the previous quarter."
A minimal version of the pipeline:
def contextualize(doc_text: str, chunk: str) -> str: context = llm(CONTEXT_PROMPT.format(doc=doc_text, chunk=chunk)) # short, 50-100 tokens return f"{context.strip()}\n\n{chunk}" for doc in docs: for chunk in split(doc.text): text = contextualize(doc.text, chunk) vector_index.add(embed(text), payload={"chunk": chunk, "doc_id": doc.id}) bm25_index.add(text, doc_id=doc.id) Two details worth copying: store the original chunk as the payload you show the model (or keep both), and use the same contextualized text for both the embedding and BM25 so the two retrievers agree on what a chunk is about.
What it bought them
Anthropic measured 1 minus recall@20 (the share of relevant chunks that did not make the top 20), averaged across codebases, fiction, ArXiv papers and science papers, with their best embedding setup (Gemini Text 004):
- Contextual Embeddings: retrieval failure rate down 35% (5.7% → 3.7%)
- Contextual Embeddings + Contextual BM25: down 49% (5.7% → 2.9%)
- + reranking: down 67% (5.7% → 1.9%)
Their summary: embeddings + BM25 beats embeddings alone, adding context helps a lot, reranking helps more, and the benefits stack.
What it costs
You call an LLM once per chunk at indexing time, and every call includes the whole document. That sounds expensive, and it would be without caching. With prompt caching you load the document once and reuse it for every chunk of that document. Anthropic's estimate, assuming 800-token chunks, 8k-token documents, 50 tokens of instructions and 100 tokens of context per chunk: about $1.02 per million document tokens, one time.
It is a one-time indexing cost, not a per-query cost. Query latency doesn't change (unless you also add a reranker).
When to use it (and when not)
- Use it when chunks lean on context they don't carry: filings, contracts, policies, long reports, code files where a function only makes sense with its module, anything with "the company", "this section" or "the above".
- Skip RAG entirely if the whole knowledge base fits in the prompt. Anthropic's own note: under about 200,000 tokens (about 500 pages), just put it all in the prompt with prompt caching.
- Tune the prompt for your domain. Anthropic suggests custom contextualizer prompts, for example adding a glossary of terms that are only defined in other documents.
- Re-index when the doc changes. The context is derived from the full document, so editing one section can make other chunks' context stale.
- Measure it. Run recall@k on your own question set before and after. Don't assume the 35-67% transfers to your data.
Checklist
- Build a small eval set of real questions with the chunk(s) that answer them.
- Measure recall@20 with your current pipeline.
- Add a contextualize step at ingestion (with prompt caching per document).
- Embed and BM25-index the contextualized text; keep the original chunk as payload.
- Re-measure. Then try adding a reranker and measure again.
Takeaway: your retriever only sees the chunk. If the chunk doesn't say what it's about, write that down for it before you embed it.
Have you tried contextual chunks in production? Did the gain hold on your data?
References
- Anthropic, Introducing Contextual Retrieval (Sep 19, 2024): https://www.anthropic.com/news/contextual-retrieval
- Anthropic docs, Prompt caching: https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching
- My earlier posts in this series:
- Hybrid search for RAG (BM25 + vectors + RRF): https://dev.to/m_t_ramkrushna/hybrid-search-for-rag-why-your-vector-search-misses-ts-999-and-the-20-line-fix-koa
- RAG chunking done right: https://dev.to/m_t_ramkrushna/rag-chunking-done-right-split-on-structure-attach-metadata-and-measure-recall-before-you-touch-2b86
- Rerank your RAG results with a cross-encoder: https://dev.to/m_t_ramkrushna/rerank-your-rag-results-with-a-cross-encoder-sentence-transformers-crossencoder-with-real-cpu-3i7c
- Prompt caching: put the stable stuff first: https://dev.to/m_t_ramkrushna/prompt-caching-put-the-stable-stuff-first-and-stop-paying-twice-for-the-same-tokens-933
