6. RAG Optimization
Retrieval-Augmented Generation has a well-documented failure mode: the retrieval step returns chunks that are semantically relevant in isolation but lack the context to be useful. A fragment mentioning "Q3 revenue grew 3%" is meaningless without the company name and year, both of which may have been in the same document but in a different chunk.
Anthropic Contextual Retrieval
Anthropic's contextual retrieval method addresses chunk isolation by pre-contextualizing each fragment before indexing. A short LLM-generated preamble is prepended to each chunk, situating it within the document it came from.
Before: "Revenue grew 3% in Q3."
After: "From Acme Corp Q3 2024 earnings report: Revenue grew 3% in Q3."The preamble is generated once per chunk at indexing time, not at retrieval time. With prompt caching, the cost of generating preambles for a large document corpus is reduced by ~90% (the document is cached; only the per-chunk instruction varies).
Published results from Anthropic's evaluation:
| Method | Failure Rate Reduction |
|---|---|
| Contextual embeddings only | 35% |
| Contextual BM25 (keyword + semantic) | 49% |
| Contextual embeddings + BM25 + reranking | 67% |
The combination of semantic search (embeddings), keyword search (BM25), and a reranking step that re-orders results by relevance to the actual query produces the best outcomes. Reranking providers include Cohere and Voyage AI.
Cost at scale: generating contextual preambles for 1M document tokens costs approximately $1.02 after prompt caching. For most production corpora, this is a one-time indexing cost.
JIT / Agentic Search
Traditional RAG loads the retrieval results at the start of the request. JIT (Just-in-Time) retrieval defers this: the agent starts with minimal context and retrieves information on-demand as the task reveals what it actually needs.
This matters for agent workflows with unpredictable information requirements. A code debugging agent may need one set of docs for a Python error and a completely different set for the database error encountered two steps later. Loading both upfront wastes context; loading neither forces hallucination. JIT retrieval threads the needle.
In Claude Code terms: this is what the agent does naturally when it uses tools (list_directory, read_file, grep) rather than receiving a pre-assembled context. The "search when needed" pattern is a design principle, not just a Claude capability.
Query-Side Indexing: Semantic Chunking and Synthetic Questions
Two indexing-time techniques consistently improve retrieval quality beyond what better embeddings alone achieve.
Semantic chunking splits documents by logical unit (paragraph, section, argument block) rather than by a fixed token count. A 500-token boundary drawn mid-sentence produces two fragments that are individually ambiguous and poorly retrieve against any query. Splitting at paragraph or section boundaries preserves the unit of meaning, even when chunk sizes become irregular.
Synthetic question generation takes each chunk and asks an LLM: what questions would this chunk answer? Those generated questions are indexed alongside (or instead of) the raw chunk text. At query time, user questions match indexed questions far more reliably than they match prose, because the vocabulary and phrasing align better. The technique is formalized in the doc2query line of work (Nogueira & Lin, 2019) and in HyDE (Hypothetical Document Embeddings, Gao et al., 2022). In production systems, practitioners have observed retrieval improvements in the 10% range on their evaluation sets, though gains vary significantly with dataset and embedding model.
The two techniques compose well with Anthropic's contextual retrieval approach described above: contextualize each chunk first, then generate synthetic questions from the contextualized version. The questions inherit the surrounding document context and produce richer index entries.
Source: Guillaume Laforge (Developer Advocate, Google Cloud), IFTTD ep 361 "Pourquoi le RAG n'est pas mort"; doc2query: Nogueira & Lin (2019); HyDE: Gao et al. (2022).
RAG Triad Evaluation
The RAG Triad is a framework for evaluating RAG output quality across three dimensions:
| Dimension | Question | What it catches |
|---|---|---|
| Context Relevance | Is the retrieved context relevant to the question? | Retrieval failures |
| Answer Relevance | Is the answer relevant to the question? | Generation drift |
| Groundedness | Is the answer supported by the retrieved context? | Hallucination |
All three can fail independently. A system can retrieve perfectly relevant context and still hallucinate (groundedness failure). It can generate a relevant answer not supported by what was retrieved. Evaluating all three simultaneously identifies which part of the RAG pipeline is the weak link.
Arize Phoenix implements the RAG Triad as a production evaluation framework (see section 9).