Week 9 • Day 3Week 9

Day 3:Week 9, Day 3: Contextual Retrieval & Metadata Enrichment (*Anthropic Pattern*)

Battle-tested daily lesson note • 100% runnable code, interview Q&A, test suite & interactive quiz

Week 9, Day 3: Contextual Retrieval & Metadata Enrichment (Anthropic Pattern)


1. The "Implicit Context Loss" Problem in RAG

Consider this real chunk extracted from an enterprise runbook:

Raw Chunk: "If the error rate exceeds 5% for 3 consecutive minutes, run rollout undo and page the on-call engineer."

What is missing from that chunk?

  • Which service? (payments-api? auth-service? search-worker?)
  • Which environment? (production? staging?)

Neither a Dense Embedding model nor a BM25 keyword engine can match that chunk to the query "How do I handle a 5% error rate on payments-api?" because the string "payments-api" only appeared in the document title 40 lines earlier!


2. Anthropic's Contextual Retrieval Architecture

In September 2024, Anthropic demonstrated that prepending a concise Contextual Header (combining document metadata, breadcrumb hierarchy, and key entities) to every chunk before generating both Dense Embeddings and BM25 indexes reduces failed retrievals by 49% to 67%!

code
Raw Chunk: "If the error rate exceeds 5% for 3 consecutive minutes, run rollout undo." │ ▼ + Contextual Enrichment (Doc Title + Breadcrumb + Metadata Tags) Contextualized Chunk (Indexed in BOTH Vector DB & BM25): "[Context: Document=payments_service.md | Section=Payments Service > Production Rollback | Service=payments-api] If the error rate exceeds 5% for 3 consecutive minutes, run rollout undo."

Now both Dense Embeddings and BM25 Lexical Search see "payments_service.md", "Payments Service", and "payments-api" directly inside the chunk!


In Simple English: Stopping "Orphan Chunk" Amnesia (Anthropic Contextual Retrieval)

Suppose Chunk #14 from a 200-page annual report reads: "Revenue grew by 3% compared to the previous quarter." Problem: Whose revenue? Apple or Tesla? Q2 2024 or Q4 2026? Standing alone, that sentence is meaningless! Anthropic Contextual Retrieval prepends one short sentence at ingestion time: [Context: Excerpt from Q3 2026 Acme Corp Earnings Report, Section: Cloud Infrastructure] Revenue grew by 3% compared to the previous quarter. Now both BM25 keyword search ("Acme Q3 2026") and vector search retrieve Chunk #14 effortlessly — cutting retrieval failures by up to 49%!