Command Palette

Search for a command to run...

UnylyUnyly
Назад к скиллам

contextual-retrieval

БесплатноБез исполняемых скриптовНе проверен

Anthropic's Contextual Retrieval technique in depth. Prepend LLM-generated chunk-specific context (Claude Haiku) to each chunk before indexing. Combines…

claude-dev-suiteот claude-dev-suite★ 51 звезда на GitHub

Об этом скилле

Contextual Retrieval

The Core Idea

A chunk lifted from a long document loses context. "The company reported 3% revenue growth" does not say which company or which period. Anthropic's Contextual Retrieval prepends a short LLM-generated context string to each chunk before embedding and BM25 indexing.

From Anthropic's 2024 research (Pro Research team):

Technique Retrieval failure rate Reduction
Embeddings only (baseline) 5.7% —
+ BM25 (hybrid) 4.7% 17.5%
+ Contextual embeddings 3.7% 35%
+ Contextual BM25 2.9% 49%
+ Reranking 1.9% 67%

Measured as failure@20 on a mixed corpus (codebases, scientific papers, fiction).

The Pipeline

[Doc] -> [Chunk] -> per-chunk LLM context via prompt cache -> [context + chunk]
                                                                    |
                                   +--------------------------------+
                                   |                                |
                             [embedding]                      [BM25 tokens]
                                   |                                |
                              [vector DB]                       [BM25 index]
                                   |                                |
                                   +---------- query ---------------+
                                                    |
                                          [RRF fusion top 150]
                                                    |
                                             [reranker top 20]

Context Generation Prompt

The prompt Anthropic published. Do not paraphrase — it is tuned.

CONTEXT_PROMPT = """<document>
{whole_document}
</document>

Here is the chunk we want to situate within the whole document:
<chunk>
{chunk_content}
</chunk>

Please give a short succinct context to situate this chunk within the overall
document for the purposes of improving search retrieval of the chunk. Answer
only with the succinct context and nothing else."""

Output is typically 50-100 tokens. Prepend to the chunk with a newline before indexing.

Full Python Implementation with Prompt Caching

Prompt caching is the reason this is affordable — the whole document sits in the cache, then every chunk reuses it.

from anthropic import Anthropic
from dataclasses import dataclass
import time

client = Anthropic()

@dataclass
class ContextualChunk:
    doc_id: str
    chunk_index: int
    original: str
    context: str
    combined: str  # context + "\n\n" + original

def contextualize_document(doc_id: str, document: str, chunks: list[str]) -> list[ContextualChunk]:
    """
    Generate context for every chunk of a document, reusing the cached document prefix.
    """
    out: list[ContextualChunk] = []
    for i, chunk in enumerate(chunks):
        resp = client.messages.create(
            model="claude-haiku-4-5-20250929",
            max_tokens=200,
            system=[
                {
                    "type": "text",
                    "text": "<document>\n" + document + "\n</document>",
                    "cache_control": {"type": "ephemeral"},
                }
            ],
            messages=[
                {
                    "role": "user",
                    "content": (
                        "Here is the chunk we want to situate within the whole document:\n"
                        f"<chunk>\n{chunk}\n</chunk>\n\n"
                        "Please give a short succinct context to situate this chunk within "
                        "the overall document for the purposes of improving search retrieval "
                        "of the chunk. Answer only with the succinct context and nothing else."
                    ),
                }
            ],
        )
        context = resp.content[0].text.strip()
        out.append(ContextualChunk(
            doc_id=doc_id,
            chunk_index=i,
            original=chunk,
            context=context,
            combined=f"{context}\n\n{chunk}",
        ))
    return out

The cache TTL is 5 minutes (ephemeral). Process all chunks of one document within that window so every call after the first is a cache hit.

Cost Math

With claude-haiku-4-5 at (approximate 2025) pricing $0.80 / M input tokens, $4 / M output:

  • Document: 100k tokens, 100 chunks.
  • Without caching: 100 * 100k input = 10M input -> $8 per document.
  • With caching (write once + 99 reads): 100k base write (1.25x) + 99 * 100k * 0.1 read = 1.1M effective -> ~$0.88.
  • ~90% cost reduction.

Always enable prompt caching. It is the difference between a research demo and a production pipeline.

Batch Processing via Message Batches API

For the first-time ingestion of a large corpus, use the Batches API (50% cheaper, async, 24h SLA).

from anthropic import Anthropic
import json

client = Anthropic()

def build_batch_requests(doc_id: str, document: str, chu
Полный SKILL.md на GitHub

Установить contextual-retrieval в Claude Code и Claude Desktop

Зарегайся, чтобы установить скилл

Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.

  • Открой команду установки в одну строку
  • Сохраняй скиллы в синхронизируемую библиотеку
  • Уведомления, когда скиллы обновляются
Зарегаться бесплатноУ меня уже есть аккаунт

Разрешённые инструменты

Инструменты, которые скиллу разрешено вызывать.

Read
Grep
Glob
Write
Edit

FAQ

Что делает скилл contextual-retrieval?

Anthropic's Contextual Retrieval technique in depth. Prepend LLM-generated chunk-specific context (Claude Haiku) to each chunk before indexing. Combines contextual BM25 + contextual embeddings + reranking for up to 67% retrieval failure reduction. Full production pipeline with prompt caching (90% cost cut), batch processing, and eval numbers. USE WHEN: user mentions "contextual retrieval", "contextual embeddings", "Anthropic contextual retrieval", "chunk context", "contextual BM25", "49% retrieval improvement" DO NOT USE FOR: generic chunking - use `chunking-strategies`; hybrid search fundamentals - use `hybrid-search`; reranking on its own - use `reranking`

Как установить скилл contextual-retrieval?

Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.

Скилл contextual-retrieval запускает скрипты?

Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.

Похожие скиллы

Сравнить contextual-retrieval с