contextual-retrieval
Anthropic's Contextual Retrieval technique for improved RAG. Use when chunks lose context during retrieval, implementing hybrid BM25+vector search, or reducing retrieval failures.
npx skills add majiayu000/claude-skill-registry --skill contextual-retrieval --agent claude-code
Same command for any agent — swap --agent for codex, cursor, copilot.
Weekly change comes from our own snapshots, not the repository page — it measures attention, not adoption.
# Contextual Retrieval Prepend situational context to chunks before embedding to preserve document-level meaning. ## The Problem Traditional chunking loses context: ``` Original document: "ACME Q3 2024 Earnings Report..." Chunk: "Revenue increased 15% compared to the previous quarter." Query: "What was ACME's Q3 2024 revenue growth?" Result: Chunk doesn't mention "ACME" or "Q3 2024" - retrieval fails ``` ## The Solution **Contextual Retrieval** prepends a brief context to each chunk: ``` Contextualized chunk: "This chunk is from ACME Corp's Q3 2024 earnings report, specifically the revenue section. Revenue increased 15% compared to the previous quarter." ``` ## Implementation ### Context Generation ```python import anthropic client = anthropic.Anthropic() CONTEXT_PROMPT = """ <document> {document} </document> Here is the chunk we want to situate within the document: <chunk> {chunk} </chunk> Please give a short, succinct context (1-2 sentences) to situate this chunk within the overall document. Focus on information that would help retrieval. Answer only with the context, nothing else. """ def generate_context(document: str, chunk: str) -> str: """Generate context for a single chunk.
- The Problem
- The Solution
- Implementation
- Context Generation
- Batch Processing with Caching
- Hybrid Search (BM25 + Vector)
- Complete Pipeline
- Optimization Tips
- 1. Cost Reduction with Caching
- 2. Parallel Processing
- 3. Context Quality
- Results (from Anthropic's research)
- When to Use
- Related Skills
What does the contextual-retrieval skill do?
Anthropic's Contextual Retrieval technique for improved RAG. Use when chunks lose context during retrieval, implementing hybrid BM25+vector search, or reducing retrieval failures.
How do I install it?
Run `npx skills add majiayu000/claude-skill-registry --skill contextual-retrieval --agent claude-code` — it drops the skill into your project so the agent can pick it up. Swap the --agent value for codex, cursor or copilot if you use one of those.
Where does this skill come from?
From majiayu000/claude-skill-registry, a repository with 534 stars. We read it straight from the repository tree rather than a submitted listing, so what you see here is what is actually published.
Is a popular skill a good skill?
Not necessarily. Stars measure attention, not adoption — a repository can trend for a week and be abandoned. That is why we show the weekly change from our own snapshots next to the total, instead of a single flattering number.
