Senior LevelAI, LLMs, Embeddings & MCP
What is Anthropic Contextual Retrieval and how does prompt-enriching chunks before embedding boost retrieval precision?
❓ Interview Prompt: Explain Anthropic Contextual Retrieval methodology, chunk context enrichment, and reducing retrieval failures by 50%.
When a document is split into individual chunks, individual chunks lose their overall document context (e.g. a chunk stating "The company's revenue grew 15%" doesn't mention which company or what year!).
Anthropic Contextual Retrieval Technique:
1
Before embedding a chunk, pass the entire document + the individual chunk to a fast LLM (e.g. Claude 3.5 Haiku).
2
Instruct the model to generate a 50-word succinct context summary placing the chunk in document context.
3
Prepend the generated context summary to the chunk before embedding!
$$\text{Enriched Chunk} = \text{Context Summary} + \text{Original Chunk}$$
4
Impact: Cuts RAG retrieval failure rates by 49% to 67%!
Code Implementation Example
typescript
async function enrichChunkWithContext(fullDoc: string, chunk: string): Promise<string> {
const enrichPrompt = "<document>" + fullDoc + "</document>\n" +
"Here is a chunk from the document:\n" +
"<chunk>" + chunk + "</chunk>\n" +
"Give a succinct 2-sentence context summary situating this chunk within the overall document.";
const contextSummary = await fastLlm.generate(enrichPrompt);
// Prepend summary context to original chunk text before vector embedding!
return contextSummary + "\n\n" + chunk;
}💡Anthropic Contextual Retrieval chunk enrichment function.
🎯 Interview Key Takeaway
Anthropic Contextual Retrieval prepends document-level context summaries to chunks prior to embedding, reducing retrieval failures by 50%.
#Contextual Retrieval#Anthropic#RAG#Embeddings#Senior
Back to all AI, LLMs, Embeddings & MCP Questions→