# What Is Contextual Retrieval?

> What contextual retrieval is, why a cut-out chunk loses its meaning, and how a short note per chunk fixes it, explained simply for your AI interview.

**In short:** Contextual retrieval adds a short note about where each chunk sits in its document, and indexes the note with the chunk. Anthropic reported 49% fewer failed retrievals in its own tests, and 67% fewer with reranking added.

AI and LLM interview question Q141, Retrieval-augmented generation (RAG). Advanced, occasional (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/what-is-contextual-retrieval

## Say it in 30 seconds

Contextual retrieval fixes a weak spot in retrieval-augmented generation (RAG), where an app looks up document chunks before it answers. A chunk cut out of its document can lose the product, period or section it belongs to. So a model writes a short note for each chunk, placed in front of it, and both the embedding index and the keyword index use the note. Anthropic reported 49% fewer failed retrievals in its own tests, and 67% fewer with reranking. I would still measure it on my own questions first.

## Say it in 2 minutes

Contextual retrieval is a fix for the search step in RAG. A RAG app cuts its documents into chunks and looks up the best chunks when someone asks a question. The trouble is that a cut-out chunk often loses its header, so it no longer says which product, period or section it belongs to.

The fix is a short note for each chunk. A model reads the whole document together with the chunk and writes a note that places the chunk in its document. That note goes in front of the chunk before anything is embedded or put in a keyword index. Anthropic reported that the method cut failed retrievals by 49%, and by 67% with reranking added, in its own tests.

The costs are real. Every chunk needs a model call, so prompt caching matters, because the whole document can be loaded once and reused for each chunk. When a document changes, its chunks need new notes. And the numbers come from Anthropic's test sets, so I would run my own evals before quoting any figure.

So my answer in an interview: a note per chunk, written from the whole document, placed before both the embedding index and the BM25 index. Then I measure the miss rate on real questions. I would leave out one summary of the whole document, because Anthropic found summaries gave very limited gains.

## What is contextual retrieval, in plain words?

Contextual retrieval is a way to make search work better for AI apps that look things up before they answer. That pattern is called retrieval-augmented generation, or RAG. A RAG app cuts its documents into small pieces called chunks, stores them in a search index, and pulls the best matches into the prompt when a user asks something.

The weak spot is the cut. A chunk on its own often loses the words that say what it is about: the company, the product, the year, the section. Contextual retrieval fixes that by writing a short note for each chunk that places it in its whole document, then indexing the note together with the chunk. The note is written once, while documents are loaded, and reused for every question.

Think of a new claims handler who finds one torn-out page of the policy manual on the desk. The page says claims must be reported within 30 days. Without the cover page, they cannot tell which policy it belongs to. Now the same thing happens to an AI assistant at a mid-size bank. It gets a chunk that says the late fee is 2% of the overdue amount, with no product name anywhere, and it must answer a customer asking about one specific account.

- **Chunk:** A short piece of a document, cut so it fits into a search index.
- **Embedding:** A list of numbers that stands for the meaning of a piece of text, so similar meanings sit close together.
- **BM25:** An older keyword ranking method. It favours exact word matches, such as a product code or a fee name.
- **Reranking:** A second, slower step that re-sorts the top search results by how well each one answers the question.

## Why does a chunk lose its meaning?

Search matches a question to chunks by words and by meaning, but it judges each chunk only by its own text. If the chunk leaves out the product name, the year or the section, the question's words may not appear in it, so the index ranks it low or misses it.

Anthropic describes the same problem. Traditional RAG removes context when it encodes information, so the right chunk can go unfound. Here is a small worked example. A mid-size bank's assistant indexes its fee schedules for dozens of accounts, and each schedule gets a new edition every year. One chunk reads: the fee is 2% of the overdue amount, charged once a month. A customer asks about the late fee on the Premier Current Account. The chunk never says Premier, the year or the section, so keyword search matches only the word fee.

*Diagram, One chunk, two outcomes: The same chunk, twice. Alone, it shares only the word fee with the question, so it gets buried or missed. With its note, it also shares the product and section words, so it ranks higher.*
1. The question is checked against the bare chunk, which shares only the word fee.
2. The same question is checked against the chunk with its note, which shares the product words too.
3. Too few shared words, so the chunk sinks in the ranking or drops out.
4. The product and fee words match, so the chunk ranks near the top.

Here is the same idea in a few lines of Python. It is a simplified keyword count, not BM25, which also weighs how rare a word is and how long a chunk is. The point is the gap between the two scores.

```python
# A simplified keyword count, not BM25: it counts the question's words a text contains.
# Real systems use BM25 and embeddings, but the gap between the two scores is the point.
QUERY = "premier current account late fee"
chunk = "The fee is 2% of the overdue amount, charged once a month."
note = "From the Premier Current Account fee schedule, 2026 edition, section 3 on late fees."

def score(text, query):
    words = set(text.lower().replace(",", "").replace(".", "").split())
    return sum(1 for w in query.split() if w in words)

print("Chunk alone:", score(chunk, QUERY))
print("With note:  ", score(note + " " + chunk, QUERY))
```

Output:

```text
Chunk alone: 1
With note:   5
```

## How is contextual retrieval built?

Anthropic calls it a preprocessing technique, so the work happens at indexing time, when documents are loaded. The question-time search stays the same, apart from an optional reranking step covered below. The app mainly gets better chunks to search.

1. **Cut the documents:** Split each document into chunks, as in any RAG system.
2. **Write a note for each chunk:** A model reads the whole document together with one chunk and writes a short note that places the chunk in its document.
3. **Put the note in front:** The note goes before the chunk, before anything is embedded or indexed.
4. **Build two indexes:** One is an embedding index for meaning, and one is a BM25 index for exact words. Both use the note and the chunk together.
5. **Search, merge and rerank:** For a question, both indexes return candidates. The two lists are merged, optionally reranked, and the top chunks go to the model.

> "Contextual Retrieval solves this problem by prepending chunk-specific explanatory context to each chunk before embedding" (Anthropic)
>
> "Answer only with the succinct context and nothing else." (Anthropic's prompt for the note)

*Diagram, Contextual retrieval at indexing time: Built once per chunk, when documents load. The model reads the whole document with each chunk, and the note goes in front of the chunk before both indexes are built. Questions then search those indexes as usual.*
1. The whole document goes to the model, so it can see where the chunk sits.
2. The chunk goes into the same model call.
3. The model writes a short note, which goes in front of the chunk.
4. The joined text is embedded, so its meaning can be matched.
5. The same joined text goes into the keyword index.
6. Meaning matches come back for each question.
7. Keyword matches come back too, and the two lists are merged.

Why a note for each chunk, and not one summary? Anthropic also tried adding a generic summary of the whole document to every chunk, and saw very limited gains. A note written for each chunk says where that chunk sits, which is the detail the search needs.

Anthropic also found that adding BM25 to embeddings helps. Embeddings plus BM25 did better than embeddings on their own, so the keyword index earns its place. It catches exact codes and names that meaning search can miss.

## What do the results show, and what are the limits of contextual retrieval?

Anthropic tested across several kinds of text, including codebases, fiction, arXiv papers and science papers, with several embedding models. Its headline numbers are averages across those domains, using the top-performing embedding setup and the top 20 chunks.

> "We use 1 minus recall@20 as our evaluation metric, which measures the percentage of relevant documents that fail to be retrieved within the top 20 chunks." (Anthropic)

- **Baseline: Starting miss rate.** Relevant chunks missed in the top 20 came to 5.7% in Anthropic's tests.
- **Contextual embeddings: 35% fewer misses.** The miss rate fell to 3.7%, a relative cut of 35%.
- **Plus contextual BM25: 49% fewer misses.** The miss rate fell to 2.9%, a relative cut of 49%. Adding reranking brought it to 1.9%, a cut of 67%.

The 49% is a cut in misses, measured from the starting rate. Going from 5.7% to 2.9% is a cut of that size, and it says nothing about how many answers become right. Read every figure as Anthropic's result on its own data, which may not match yours.

- **Chunk size and cut points:** They still change results. Anthropic says the choice of chunk size, boundary and overlap can affect retrieval.
- **Cost of the notes:** One model call per chunk. With prompt caching, the whole document is loaded once and reused for every chunk.
- **Documents that change:** A changed document needs new notes for its chunks. Plan a job that redoes only the documents that changed.
- **Reranking adds delay:** The reranker is an extra step on each question. It adds a small amount of latency, which Anthropic notes.
- **Small knowledge bases:** In its September 2024 post, Anthropic says a knowledge base under 200,000 tokens can go straight into the prompt, so RAG may not be needed at all.

> **Tip:** Asked whether it always works? Say it depends on the documents and the questions, then name the test you would run first: a set of real questions with known right answers, scored before and after.

### How would you roll out contextual retrieval for an insurer's policy library?

Say a mid-size insurer has 60,000 pages of policy wording across motor, home and travel products, with a new edition of each product every year. Claims handlers ask questions that name a product and an edition, so a chunk that loses its header is a real risk.

1. **Build the test first:** Collect a few hundred real questions from claims handlers, each with its right answer and source page. Measure the miss rate before changing anything.
2. **Add notes one document at a time:** Load each full policy document once, then write the notes for its chunks. When a new edition arrives, redo only that document's chunks.
3. **Compare, then decide:** Run the same questions on both indexes. Keep the change if the miss rate falls on your questions, and check the cost of the notes and the reranker before you widen it.

> **Tip:** Anthropic says a prompt tailored to your domain may beat its generic one. In its own tests, reranking started from the top 150 chunks.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] A plain definition: a short note on each chunk, placed before it, before indexing.
- [ ] The problem it fixes: a chunk loses its product, period or section when it is cut out.
- [ ] How it is built: a model writes the note from the whole document, and both indexes use it.
- [ ] Honest numbers: Anthropic's relative cuts on its own tests, and the need to test on your data.
- [ ] Costs and upkeep: one call per chunk, prompt caching, and redoing the notes when documents change.
- [ ] Its place: it sits alongside keyword search, embeddings and reranking, and it still depends on good chunking.

**Common traps**

- Trap: "Contextual retrieval means a bigger embedding model." Better: It adds a note to each chunk before indexing. The embedding model can stay the same.
- Trap: "Just add one summary of the whole document to every chunk." Better: Anthropic tried that and saw very limited gains. Each chunk needs its own note.
- Trap: "The 49% means half of our answers become correct." Better: It is a relative cut in failed retrievals, in Anthropic's own tests. Run your own evals.

## Can you spot the weak note?

**Spot the weak note:** A bank's indexer wrote these notes. Which one would you ship?

A. This chunk is about current account fees in general.
B. This chunk is from the Premier Current Account fee schedule, 2026 edition, section 3 on late fees.
C. This chunk is from the Premier Current Account fee schedule, approved by the regulator in 2019, section 3.
D. Late fees appear in many products, so this chunk applies to all of them.

**Answer:** B. The right note names the product, the edition and the section, and says only what the document says. The approval line is invented, and a made-up fact in a note can pull the wrong chunk up.

**Quick check:** A bank's assistant keeps quoting the wrong product's late fee. Which change fixes most of that?

A. A bigger chat model
B. A short note on each chunk that names the product, placed before indexing
C. Re-indexing all documents with a newer embedding model
D. Removing the product names from the fee schedules

**Answer:** B. The note puts the product name and section back in front of the chunk, so both indexes can match it. A bigger chat model or a newer embedding model cannot find a name the chunk does not hold. Removing the names would make the problem worse.

## Can you answer these from memory?

**Flash cards**

- **What is contextual retrieval?** A short note on each chunk, placed before it, so search can find it.
- **Where does the note go?** In front of the chunk, before the embedding and BM25 indexes are built.
- **Who writes the note?** A model that reads the whole document together with one chunk.
- **Why not one summary for all chunks?** Anthropic tried it and saw very limited gains.
- **What did Anthropic report?** 49% fewer failed retrievals with both indexes, 67% with reranking. Its tests only.
- **What makes the notes affordable?** Prompt caching: load the whole document once, reuse it for every chunk.

## Likely follow-up questions

- Q123 What is hybrid search (BM25 + vectors)? https://designervenkat.online/interview/ai/what-is-hybrid-search
- Q124 What is BM25? https://designervenkat.online/interview/ai/what-is-bm25
- Q126 Rerankers: cross-encoder vs bi-encoder? https://designervenkat.online/interview/ai/cross-encoder-vs-bi-encoder-rerankers
- Q133 What are common RAG failure modes? https://designervenkat.online/interview/ai/common-rag-failure-modes
- Q142 What is parent-child (small-to-big) retrieval? https://designervenkat.online/interview/ai/what-is-parent-child-small-to-big-retrieval

## Learn it in depth

- [Chunking](https://designervenkat.online/learn/ai-engineering/chunking)
