Ultralytics YOLO27: Join the waitlist.
License YOLO
Back to Ultralytics Glossary

Contextual Retrieval

Contextual retrieval prepends an LLM-written note to each chunk saying where it sits in its document, so embeddings and BM25 find the right passages.

Contextual retrieval is an indexing technique for retrieval-augmented generation (RAG) in which a large language model (LLM) reads each whole document and writes a short note, usually 50 to 100 tokens, that situates one chunk within it. The note is prepended to the chunk before the chunk is embedded and added to a keyword index, so search can match information the chunk alone never states. Anthropic introduced contextual retrieval in September 2024.

The Problem It Solves#

RAG pipelines split documents into chunks small enough to embed precisely and to fit several into a model's context window, and a chunk cut out of its document often loses the facts that make it findable. Anthropic's example is a chunk from a financial filing that reads "The company's revenue grew by 3% over the previous quarter." It names neither the company nor the quarter, so a question about ACME's revenue in Q2 2023 has nothing to match in it, for embeddings or for keyword search. Contextual retrieval stores it as "This chunk is from an SEC filing on ACME corp's performance in Q2 2023; the previous quarter's revenue was $314 million. The company's revenue grew by 3% over the previous quarter."

Object crops lose context the same way text chunks do. The object cropping solution in Ultralytics runs YOLO26 on images or video and saves every detection as its own file, crop_1.jpg, crop_2.jpg, and so on, so a crop shows a truck but not which camera, site, or scene it came from. Indexing each crop with a short prefix describing its source frame, such as the camera name, the time, and the other objects detected around it, restores that context for keyword and semantic search.

How Contextual Retrieval Works#

For every chunk, the LLM receives the full document and the chunk and is asked to "give a short succinct context to situate this chunk within the overall document for the purposes of improving search retrieval of the chunk." Anthropic used Claude 3 Haiku with prompt caching, which processes each document once and reuses it for all of its chunks, and put the one-time cost at $1.02 per million document tokens. The contextualized chunks go into an embedding index ("contextual embeddings") and a BM25 keyword index ("contextual BM25"). At query time both indexes are searched and their results merged, as in hybrid search, then a reranker scores the top 150 candidates and the best 20 chunks go into the prompt.

Embedding the example chunk with and without its context shows the effect on vector search:

from sentence_transformers import SentenceTransformer

model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")

# Anthropic's example chunk and the context an LLM wrote for it from the full filing
chunk = "The company's revenue grew by 3% over the previous quarter."
context = "This chunk is from an SEC filing on ACME corp's performance in Q2 2023; the previous quarter's revenue was $314 million."
other = "Globex Corporation reported that Q2 2023 revenue fell 2% on weaker demand."

query = "How did ACME's revenue change in Q2 2023?"
candidates = {"plain chunk": chunk, "contextual chunk": f"{context} {chunk}", "Globex chunk": other}

# Cosine similarity of the query to each indexed text
scores = model.similarity(model.encode(query), model.encode(list(candidates.values())))[0]
for name, score in zip(candidates, scores):
    print(f"{name}: {score:.2f}")

The plain chunk scores 0.49 against the question, the same as a chunk about a different company. With its context prepended, the chunk scores 0.83 and ranks first by a wide margin.

  • Late chunking: Gives each chunk's embedding document context through the embedding model's attention, with no LLM call per chunk. The stored chunk text stays unchanged, so keyword search gains nothing.
  • Semantic chunking: Chooses where chunks begin and end. It combines with contextual retrieval, which fills in what each chunk leaves unsaid.
  • Title and heading prefixes: The cheaper, older practice of prepending the same document title or section heading to every chunk. Contextual retrieval writes a different note for each chunk.
  • Context engineering: The broader practice of deciding what goes into a model's context window at inference time. Contextual retrieval works one step earlier, on what gets indexed.

Measured Gains#

Anthropic measured retrieval failure as 1 minus recall@20, the share of queries whose relevant chunk was missing from the top 20 results. The baseline pipeline failed on 5.7% of queries. Contextual embeddings lowered that to 3.7%, a 35% reduction, and adding contextual BM25 lowered it to 2.9%, a 49% reduction. Reranking the combined results brought the failure rate to 1.9%, a 67% reduction from the baseline.

Explore solutions

Computer vision in sports

Computer vision in sports

Discover how Ultralytics YOLO powers sports vision with player and ball tracking, performance analytics, coaching insights, and athlete rehabilitation.
Learn more
Computer vision for aerial imagery

Computer vision for aerial imagery

Transform drone and aerial imagery into real-time insights for agriculture, aquaculture, environmental monitoring, conservation, and geospatial analysis with Ultralytics YOLO.
Learn more
Computer vision in aerospace

Computer vision in aerospace

Bring vision AI to aerial monitoring with Ultralytics YOLO models. Power drones, aerospace workflows, environmental conservation & geospatial industries with computer vision solutions.
Learn more
Computer vision in security

Computer vision in security

Protect every site with Ultralytics YOLO models. Vision AI powers perimeter monitoring, threat detection, license-plate recognition, and workplace safety.
Learn more
Computer vision in robotics

Computer vision in robotics

Power smarter machines with Ultralytics YOLO models. Vision AI in robotics drives autonomous navigation, perception, object tracking, and real-time control.
Learn more
Computer vision in logistics

Computer vision in logistics

Streamline logistics with Ultralytics YOLO models. Vision AI enables package inspection, sorting, vehicle tracking, and real-time warehouse safety monitoring.
Learn more
Computer vision in retail

Computer vision in retail

Reimagine retail with Ultralytics YOLO models. Vision AI powers inventory tracking, shelf monitoring, queue management, and smarter customer insights.
Learn more
Computer vision in healthcare

Computer vision in healthcare

Build healthcare solutions with Ultralytics YOLO models. Vision AI in healthcare powers faster medical imaging, smarter diagnostics, and patient monitoring.
Learn more
Computer vision in manufacturing

Computer vision in manufacturing

Optimize manufacturing with Ultralytics YOLO models. Vision AI drives quality control, defect detection, PPE compliance, and assembly line automation.
Learn more
Computer vision in automotive

Computer vision in automotive

Apply computer vision in automotive with Ultralytics YOLO models. Vision AI elevates road safety, driver assistance, and vehicle automation for smarter roads.
Learn more
Computer vision in agriculture

Computer vision in agriculture

Bring vision AI to smart agriculture with Ultralytics YOLO models. Power crop monitoring, livestock tracking, and precision farming for higher, smarter yields.
Learn more
Computer vision in sports
Computer vision in sports
Discover how Ultralytics YOLO powers sports vision with player and ball tracking, performance analytics, coaching insights, and athlete rehabilitation.
Learn more
Computer vision for aerial imagery
Computer vision for aerial imagery
Transform drone and aerial imagery into real-time insights for agriculture, aquaculture, environmental monitoring, conservation, and geospatial analysis with Ultralytics YOLO.
Learn more
Computer vision in aerospace
Computer vision in aerospace
Bring vision AI to aerial monitoring with Ultralytics YOLO models. Power drones, aerospace workflows, environmental conservation & geospatial industries with computer vision solutions.
Learn more
Computer vision in security
Computer vision in security
Protect every site with Ultralytics YOLO models. Vision AI powers perimeter monitoring, threat detection, license-plate recognition, and workplace safety.
Learn more
Computer vision in robotics
Computer vision in robotics
Power smarter machines with Ultralytics YOLO models. Vision AI in robotics drives autonomous navigation, perception, object tracking, and real-time control.
Learn more
Computer vision in logistics
Computer vision in logistics
Streamline logistics with Ultralytics YOLO models. Vision AI enables package inspection, sorting, vehicle tracking, and real-time warehouse safety monitoring.
Learn more
Computer vision in retail
Computer vision in retail
Reimagine retail with Ultralytics YOLO models. Vision AI powers inventory tracking, shelf monitoring, queue management, and smarter customer insights.
Learn more
Computer vision in healthcare
Computer vision in healthcare
Build healthcare solutions with Ultralytics YOLO models. Vision AI in healthcare powers faster medical imaging, smarter diagnostics, and patient monitoring.
Learn more
Computer vision in manufacturing
Computer vision in manufacturing
Optimize manufacturing with Ultralytics YOLO models. Vision AI drives quality control, defect detection, PPE compliance, and assembly line automation.
Learn more
Computer vision in automotive
Computer vision in automotive
Apply computer vision in automotive with Ultralytics YOLO models. Vision AI elevates road safety, driver assistance, and vehicle automation for smarter roads.
Learn more
Computer vision in agriculture
Computer vision in agriculture
Bring vision AI to smart agriculture with Ultralytics YOLO models. Power crop monitoring, livestock tracking, and precision farming for higher, smarter yields.
Learn more
Computer vision in sports
Computer vision in sports
Discover how Ultralytics YOLO powers sports vision with player and ball tracking, performance analytics, coaching insights, and athlete rehabilitation.
Learn more
Computer vision for aerial imagery
Computer vision for aerial imagery
Transform drone and aerial imagery into real-time insights for agriculture, aquaculture, environmental monitoring, conservation, and geospatial analysis with Ultralytics YOLO.
Learn more
Computer vision in aerospace
Computer vision in aerospace
Bring vision AI to aerial monitoring with Ultralytics YOLO models. Power drones, aerospace workflows, environmental conservation & geospatial industries with computer vision solutions.
Learn more
Computer vision in security
Computer vision in security
Protect every site with Ultralytics YOLO models. Vision AI powers perimeter monitoring, threat detection, license-plate recognition, and workplace safety.
Learn more
Computer vision in robotics
Computer vision in robotics
Power smarter machines with Ultralytics YOLO models. Vision AI in robotics drives autonomous navigation, perception, object tracking, and real-time control.
Learn more
Computer vision in logistics
Computer vision in logistics
Streamline logistics with Ultralytics YOLO models. Vision AI enables package inspection, sorting, vehicle tracking, and real-time warehouse safety monitoring.
Learn more
Computer vision in retail
Computer vision in retail
Reimagine retail with Ultralytics YOLO models. Vision AI powers inventory tracking, shelf monitoring, queue management, and smarter customer insights.
Learn more
Computer vision in healthcare
Computer vision in healthcare
Build healthcare solutions with Ultralytics YOLO models. Vision AI in healthcare powers faster medical imaging, smarter diagnostics, and patient monitoring.
Learn more
Computer vision in manufacturing
Computer vision in manufacturing
Optimize manufacturing with Ultralytics YOLO models. Vision AI drives quality control, defect detection, PPE compliance, and assembly line automation.
Learn more
Computer vision in automotive
Computer vision in automotive
Apply computer vision in automotive with Ultralytics YOLO models. Vision AI elevates road safety, driver assistance, and vehicle automation for smarter roads.
Learn more
Computer vision in agriculture
Computer vision in agriculture
Bring vision AI to smart agriculture with Ultralytics YOLO models. Power crop monitoring, livestock tracking, and precision farming for higher, smarter yields.
Learn more

Let's build the future of AI together!

Begin your journey with the future of machine learning