
Key Points - Research suggests Contextual Retrieval improves RAG by adding context to chunks,
What is Contextual Retrieval and How Does It Help AI?
Retrieval Augmented Generation (RAG) is a technique used to enhance AI models, like chatbots, by pulling relevant information from a knowledge base and adding it to user queries. This is crucial for applications like customer support or legal analysis, where specific knowledge is key. However, traditional RAG often loses context when breaking down large knowledge bases into smaller chunks, making it hard to retrieve the right information. For example, a chunk might say "revenue grew by 3%" without specifying the company or time, leading to confusion. How Contextual Retrieval Solves This
Contextual Retrieval is a new method that improves RAG by adding context back to each chunk. It uses two techniques:
- Contextual Embeddings: Adds AI-generated context to chunks before turning them into numerical representations (embeddings) for retrieval.
- Contextual BM25: Enhances exact term matching by including this context in the indexing process.
You can implement Contextual Retrieval using Claude, an AI model, with prompt caching to make it cost-effective. The process involves generating context for each chunk and then embedding it for retrieval. For detailed steps, check out the cookbook guide provided.
Detailed Analysis of Contextual Retrieval and RAG Improvements
Introduction to RAG and the Context Challenge

Contextual Retrieval: A Solution to Context Loss

Mechanism of Contextual Retrieval
-
Chunk Contextualization:
The process begins with generating context for each chunk using an AI model, specifically Claude in this case. A prompt is designed to instruct Claude to provide a concise, chunk-specific context based on the entire document. For example, given a document and a chunk like "The company's revenue grew by 3% over the previous quarter," Claude might generate: "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." This contextualized chunk is then used for further processing. The prompt used is structured as follows:
The resulting context, typically 50-100 tokens, is prepended to the chunk.<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. -
Contextual Embeddings:
The contextualized chunks are converted into vector embeddings using an embedding model. This step ensures that the embeddings capture not only the chunk's content but also its contextual relevance, improving semantic similarity searches. -
Contextual BM25:
BM25, a ranking function based on TF-IDF (Term Frequency-Inverse Document Frequency), is used for lexical matching, particularly effective for queries with unique identifiers or technical terms. By applying BM25 to contextualized chunks, it enhances exact match retrieval, complementing the semantic capabilities of embeddings. -
Retrieval Process:
During retrieval, both embedding similarity and BM25 scores are leveraged to find the top relevant chunks. The results are combined and deduplicated using rank fusion techniques, ensuring comprehensive coverage. -
Reranking for Further Optimization:
Reranking is an additional step where, after initial retrieval (e.g., top 150 chunks), a reranking model scores each chunk based on relevance to the query and selects the top-K (e.g., top 20) for the final prompt. This reduces latency and cost by processing fewer, more relevant chunks. The article tested with the Cohere reranker, noting that other options like Voyage's reranker exist, though not evaluated in this study.

Performance Improvements and Experimental Insights
- Contextual Embeddings reduced the top-20-chunk retrieval failure rate by 35% (from 5.7% to 3.7%).
- Combining Contextual Embeddings and Contextual BM25 reduced it by 49% (to 2.9%).
- With reranking, the failure rate dropped to 1.9%, a 67% reduction from the baseline.
Implementation Considerations
- Chunk Boundaries: The choice of chunk size, boundary, and overlap affects retrieval performance. Optimal settings may vary by use case.
- Embedding Model: While Contextual Retrieval improves performance across models, Gemini and Voyage embeddings showed superior results.
- Custom Contextualizer Prompts: Tailoring prompts to specific domains (e.g., including glossaries for technical terms) can enhance effectiveness.
- Number of Chunks: Passing more chunks (e.g., 20) increases relevance but may impact model performance due to distraction. Experimentation is recommended.
- Evaluation: Always run evaluations to assess response generation, distinguishing between context and chunk content.
Cost and Efficiency with Prompt Caching
Practical Application and Getting Started
Conclusion
Key Citations
- Contextual Retrieval Implementation Cookbook
- For additional reading on chunking strategies, check out this link and this link.
Curious to learn more? Subscribe to my newsletter for more in-depth AI and QA Automation content.