Contextual retrieval in AI systems
# Contextual Retrieval: Improving RAG Accuracy by Adding Chunk Context The post argues that traditional RAG systems lose critical information when documents are split into chunks, causing retrieval failures. Its proposed fix, Contextual Retrieval, prepends chunk-specific explanatory context to each chunk before embedding and BM25 indexing. Combined with BM25 and reranking, this cuts the top-20-chunk retrieval failure rate from 5.7% to 1.9%, a 67% reduction. ## When Simple Prompting Is Enough - If the knowledge base is under about 200,000 tokens (roughly 500 pages), it can be placed directly in the prompt without RAG. - Prompt caching makes this approach faster and cheaper, with latency reduced by more than 2x and costs by up to 90%. - Larger knowledge bases still need a retrieval system. ## How Traditional RAG Works and Where It Falls Short - Documents are split into chunks of a few hundred tokens, converted to vector embeddings, and stored in a vector database for semantic search. - Embeddings can miss exact matches, such as an error code like "TS-999." BM25, a lexical ranking function built on TF-IDF, handles these well. - Combining embeddings and BM25 with rank fusion improves results, but chunks often lose context. A chunk like "The company's revenue grew by 3% over the previous quarter" doesn't name the company or the time period. ## Contextual Embeddings and Contextual BM25 - Each chunk is prepended with a short, 50–100 token context situating it within the whole document before embedding and BM25 indexing. - Claude (Claude 3 Haiku in the post) generates this context from the full document and the chunk, using a prompt that asks for a succinct explanation to improve search retrieval. - Manually annotating millions of chunks is impractical, so the model automates the process. - Other context-adding approaches, such as generic document summaries, hypothetical document embeddings, and summary-based indexing, showed limited gains in the authors' tests. ## Keeping Costs Down with Prompt Caching - The full document is cached once and referenced for every chunk, so it isn't resent each time. - With 800-token chunks, 8k-token documents, and 100 tokens of context per chunk, the one-time cost is about $1.02 per million document tokens. ## Experimental Results - Tests covered codebases, fiction, ArXiv papers, and science papers, across several embedding models and retrieval strategies. - Using Gemini Text 004 and top-20 retrieval, contextual embeddings alone reduced the failure rate by 35% (5.7% → 3.7%). - Contextual embeddings combined with contextual BM25 reduced it by 49% (5.7% → 2.9%). - Adding reranking brought the failure rate down to 1.9%, a 67% reduction. ## Implementation Considerations - Chunk size, boundaries, and overlap all affect retrieval performance. - Gemini and Voyage embeddings performed best among the models tested, though contextualization helped every model. - Custom contextualizer prompts, such as ones that include a glossary of domain terms, may improve results further. - Passing 20 chunks to the model outperformed 5 or 10, though more chunks can also distract the model. - Evaluate on your own use case, and consider clearly separating the context from the chunk text during response generation. ## Reranking - Initial retrieval fetches a larger candidate set (the authors used the top 150). - A reranking model, such as Cohere's, scores each chunk against the query, and the top 20 go to the generator. - Reranking improves accuracy and lowers the cost and latency of generation, but it adds a small amount of latency itself. The number of chunks to rerank is a trade-off between accuracy and speed. ## Practical Takeaway The authors recommend combining contextual embeddings (using Voyage or Gemini), contextual BM25, reranking, and passing the top 20 chunks to the model. They provide a cookbook for implementing the approach.