What is contextual retrieval?
Contextual retrieval is a retrieval technique that, before indexing, prepends to each document chunk a short note on where it sits in the document and what it covers, so chunks still make sense alone.
Unlike late chunking, which embeds the whole document before splitting it, contextual retrieval has a language model write a note for each chunk and adds it to the text. Both tackle chunks losing their document context.
- Topic
- Embeddings, document processing and RAG
- Also known as
- Contextual chunk retrieval
How contextual retrieval works
- The document is first chunked as usual.
- Each chunk goes to a language model with the whole document, which writes one or two sentences on its position and topic.
- The context is prepended to the chunk; the extended text goes into both the embedding index and a keyword index such as BM25.
- Queries match these enriched chunks; the original chunk text is used when generating answers.
In more detail
Hybrid score fusion (RRF) and the reranker
In the reference Contextual Retrieval pipeline, dense contextual embeddings and contextual BM25 scores are fused (commonly via Reciprocal Rank Fusion) and passed through a cross-encoder reranker. Combining semantic similarity, exact lexical matching and reranking provides significantly larger retrieval gains than contextual embeddings alone.
Hierarchical context and section summaries for very long documents
When source files exceed the ingestion LLM's context window, such as 400-page regulatory manuals, passing the full document for every chunk is impractical. Production pipelines replace the full text with a structured table of contents, document metadata and rolling section summaries so chunk context remains accurate without overflowing the window.
Prefix cache warm-up and incremental indexing
Because context generation relies on prefix caching across chunks of the same document, documents should be processed in grouped batches per file so the shared document prefix remains hot in GPU KV cache memory. Furthermore, incremental pipelines must store chunk hashes so editing one section only re-contextualises changed documents rather than re-running the entire archive.
Alternatives: CDE and multimodal document retrieval (ColPali)
Alternatives such as Contextual Document Embeddings (CDE) encode corpus-level neighbourhood context directly into the embedding model at inference time without calling a generative LLM per chunk, while multimodal document retrievers like ColPali index page screenshots directly for tables and charts where text chunking fails.
Commonly confused terms
| Term | How it differs |
|---|---|
| Contextual embedding | Contextual retrieval is a RAG chunk-enrichment strategy before indexing; contextual embedding refers to token/word vector representations that vary depending on surrounding text inside a language model. |
| Parent-document retrieval | Contextual retrieval injects static document-level context into each chunk during indexing, whereas parent-document (or small-to-big) retrieval indexes small chunks and swaps in the larger surrounding parent section only at retrieval time. |
Why contextual retrieval matters for on-premises AI
Chunks of company documents are often incomplete alone: a sentence such as “this ratio rose on the previous period” does not say which company's report it came from. Adding context helps such chunks match the right query. The price is extra model calls during ingestion. In-house this can run on a small model with prefix caching, since all chunks of a document share it as a prefix. Its effect is measured on the organisation's own question set.
An investment firm ingesting annual reports adds a sentence naming the company, year and section to each chunk, so questions such as “last year's gross profit” reach the right chunk more easily.
Common questions
How does contextual retrieval differ from late chunking?
Contextual retrieval adds a note written by a language model to each chunk. Late chunking adds no text; it carries context into vectors by embedding the whole document before splitting.
What does contextual retrieval cost?
One model call per chunk during ingestion. Caching the shared document prefix and using a small model cut this cost; query-time cost does not change.
Related terms
Sources
- Anthropic, Contextual Retrieval, 2024
- Lewis et al., Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks, 2020
In the glossary: Glossary, A to D