DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk6 min

Stop Losing Chunk Context: Anthropic Contextual Retrieval with Spring AI and Virtual Threads

Contextual Retrieval adds per-chunk context to semantic and lexical indexes. Here’s how to evaluate its cost and fit it into Spring AI, including Anthropic dispatcher lifecycle and tracing caveats.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Contextual Retrieval adds a short, chunk-specific description of where a passage belongs in its source document, then indexes that description with the passage for both semantic and lexical search. In a Spring AI application, this is an ingestion and indexing decision—not a substitute for the framework’s retrieval pipeline—and virtual threads are a separate option for dispatching Anthropic HTTP requests.

Why a RAG chunk can lose the answer’s context

A passage can be meaningful in its original document but ambiguous after splitting. Anthropic illustrates the problem with the question, “What was the revenue growth for ACME Corp in Q2 2023?” A chunk that says only “The company’s revenue grew by 3% over the previous quarter” omits the company and period needed to connect it reliably to that question.

As an Amazon Associate I earn from qualifying purchases.

Traditional retrieval splits documents into passages and searches them through lexical matching, semantic embeddings, or both. Splitting can separate a pronoun from its antecedent, a relative date from the date it refers to, or a technical term from the section that defines it. Anthropic describes Contextual Retrieval as a preprocessing technique: use the full document to generate brief context for each individual chunk, prepend that context, and use the resulting text for retrieval.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What Contextual Retrieval adds

For each chunk, a language model sees the full source document and the chunk, then produces concise text that situates that chunk in the document. The added context might identify the subject, time period, section, or surrounding argument. Anthropic says this text is usually 50–100 tokens; that range and its example prompt are starting points, not universal settings.

#1 Best Overall

The important distinction is that the context is specific to each chunk. Repeating the same generic document summary before every passage is a different approach; Anthropic reports that generic summaries produced limited gains in its evaluation. Chunk-specific context can supply information that is absent from the local passage while retaining the passage’s own content.

Anthropic’s method puts the contextualized text into both retrieval channels: it is used to create embeddings and to build the BM25 index. This lets semantic search and lexical search consider the added identifiers alongside the original words. It does not mean that the generated prefix should be mistaken for source text. Preserve the original passage and its provenance, and distinguish contextual metadata from source content when assembling material for the model’s final answer.

What Anthropic’s reported evaluations show

Anthropic’s 2024 engineering article reports average top-20 retrieval results across codebases, fiction, arXiv papers, and science papers, using the top-performing embedding configuration in its analysis. The reported failure rate is the rate at which retrieval failed to return the relevant chunk within the top 20:

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Approach in Anthropic’s evaluation Top-20 retrieval failure rate Reported change from baseline
Baseline 5.7% Baseline
Contextual Embeddings 3.7% 35% reduction
Contextual Embeddings plus Contextual BM25 2.9% 49% reduction

Anthropic’s separate 2024 Cookbook example concerns nine codebases, basic character-based splitting, and a dataset of 248 queries, each with a designated “golden chunk.” In that setup, Anthropic reports Pass@10 improving from about 87% to about 95% with Contextual Embeddings. Pass@10 is a different evaluation measure from the top-20 failure rates above; the figures should not be treated as one combined benchmark.

These are source-reported evaluations, not a guarantee for a different corpus. Chunk boundaries and overlap, domain terminology, embedding model, retrieval depth, and contextualizer prompt can all affect the outcome. Evaluate against representative questions from the target system and inspect whether retrieved chunks actually contain enough source material to answer them.

Account for preprocessing cost and update work

Anthropic’s 2024 article gives an illustrative one-time cost of $1.02 per million document tokens under specific assumptions: 800-token chunks, 8,000-token documents, 50 tokens of context instructions, 100 generated context tokens per chunk, and prompt caching. This is a historical estimate under those assumptions, not a current general provider price or a quote for a production workload.

Contextualization adds model work before retrieval. Estimate its impact using the actual document and chunk distributions, prompt and output token use, cache behavior, document update rate, and the cost of reprocessing changed documents and rebuilding affected indexes. A corpus that changes frequently may have a different operating trade-off from a static collection. Compare the added work with retrieval quality measured on your own evaluation set.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Fit the preprocessing into Spring AI’s RAG flow

Spring AI offers both a simpler advisor path and a modular RAG API. Its QuestionAnswerAdvisor queries a vector store and appends retrieved documents to the prompt; the documented dependency for that path is spring-ai-vector-store-advisor. The modular RetrievalAugmentationAdvisor, documented with spring-ai-rag, lets an application compose stages such as query transformation, retrieval, document joining, post-processing, and query augmentation.

Contextual Retrieval belongs in ingestion because it changes the text used to index each chunk. A practical flow is:

  1. Parse and split: retain the original document and record stable identifiers and provenance for each chunk.
  2. Generate per-chunk context: provide the full document and one chunk to the contextualizer, requesting concise text that situates that chunk.
  3. Keep source and context distinct: store the original chunk separately from the generated prefix, so downstream code can preserve provenance and avoid presenting generated context as quoted source.
  4. Index for retrieval: use the contextualized text for embedding generation and lexical indexing, including BM25 where the application uses that channel.
  5. Retrieve and assemble: use Spring AI’s chosen advisor or composed RAG stages to retrieve, join, and post-process documents, then augment the model prompt.
  6. Evaluate changes: test representative questions and retrieval outcomes when adjusting chunking, contextualization, or retrieval settings.

Spring AI’s ContextualQueryAugmenter is not the chunk-context generation step. It augments a user query with contextual data from documents that have already been retrieved. That is a query or prompt-stage operation, while Anthropic’s contextualization creates per-chunk text before indexing.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Use virtual threads for Anthropic HTTP dispatch—with lifecycle ownership

The Spring AI Anthropic integration reference documents a configurable dispatcher executor for synchronous and asynchronous streaming clients. Its example uses a virtual-thread-per-task executor:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
AnthropicChatModel chatModel = AnthropicChatModel.builder()
    .options(...)
    .dispatcherExecutor(Executors.newVirtualThreadPerTaskExecutor())
    .build();

Virtual threads are an option for high HTTP concurrency or Java 21-and-later workloads, not a performance guarantee. If the application supplies the executor, the application owns its lifecycle: Spring AI will not call shutdown() on it. Retain the executor and shut it down as part of the application’s managed lifecycle. If this option is omitted, Spring AI creates and cleans up its internal executor.

This dispatcher setting is distinct from executor configuration for RAG retrieval. A Spring AI engineering example describes a modular RAG advisor whose per-query retrieval threads are non-daemon, keeping its command-line process alive after printing a response. In that example, passing Spring Boot’s auto-configured TaskExecutor through .taskExecutor(...) addresses the issue; the example says spring.threads.virtual.enabled=true enables virtual threads for that configuration. This is scoped to that advisor example and is not the Anthropic HTTP dispatcher setting.

The same engineering example warns that its illustrated flow makes two LLM calls before retrieval and one service call per retrieved chunk. Measure latency and cost for the actual flow rather than assuming that modular composition or virtual threads make these calls free.

Plan for streaming trace context

The Spring AI Anthropic reference says synchronous HTTP spans are nested under the model operation, but streaming HTTP spans may not be. It attributes this to the Anthropic Java SDK’s asynchronous implementation moving onto ForkJoinPool.commonPool() before calling Spring AI’s HTTP client, which can lose the calling thread’s observation context. The reference says traceparent is still propagated and suggests correlating okhttp.requests with the model operation by trace ID or timestamp range.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Check this behavior against the exact Spring AI and Anthropic SDK versions in use: asynchronous execution and instrumentation can change. When diagnosing a streaming request, inspect trace IDs and timestamps rather than assuming every HTTP span will appear as a child of the model span.

Choose and validate the retrieval design

Contextual Retrieval adds useful information at an engineering cost. The design decision is not simply whether to prepend text, but whether the improvement on the application’s own questions justifies contextualization, indexing, and update work.

  • Choose the context strategy: compare no added text, a generic document summary, and per-chunk context generated from the full source.
  • Choose retrieval channels: evaluate semantic embeddings and lexical BM25 separately as well as together where appropriate.
  • Choose a relevant metric: report a defined retrieval measure, such as failure at a stated top-k or task-specific success, along with the corpus and evaluation set.
  • Track preprocessing burden: measure contextualizer calls and tokens, cache behavior, document update cadence, and the index work required when sources change.
  • Keep runtime responsibilities separate: manage the Anthropic dispatcher lifecycle independently from any RAG advisor executor, and verify tracing behavior for streaming requests.

Spring AI’s RAG reference displayed version 2.0.1 when accessed on October 7, 2026. Check the current documentation and the application’s Spring AI BOM before adopting version-specific dependencies or configuration. The Anthropic integration behavior, executor APIs, SDK caveats, and provider pricing can change over time.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.