Recipe · recipe:retrieval-and-rag

My retrieval returns the wrong chunk and I cannot see why it ranked. I want retrieval my agent can search with, not a one-shot pipeline.

Curated. Written from research and curation. Not run in our harness yet.

The job

We see the same failure repeatedly. Retrieval returns the wrong chunk, and nothing in the trace explains why that chunk won. There is no way to tell whether the chunker cut the answer in half, whether the embedding missed an exact identifier, or whether the right chunk was never retrieved at all. What we want instead is a retrieval substrate the agent can search with, as a loop, rather than a pipeline that fires once and hands back whatever it found.

The approach

We build the substrate first, then expose it as a loop.

Parse every document to text plus structure, keeping titles and headings. Chunk on structure, not raw token counts, and prepend a short blurb that situates each chunk in its document before it is embedded or lexically indexed. Contextual chunking is the highest-leverage step: Anthropic measured 49 percent fewer retrieval failures from contextual embeddings plus contextual BM25, and 67 percent with a reranker on top.

Index the same contextualised chunks twice, dense and lexical BM25. Fuse the two candidate sets, rerank with a cross-encoder or hosted reranker, and deduplicate by chunk id before returning.

Only then wrap retrieval in tools (search, read, optionally list and grep over a mirrored tree) and let the agent iterate under a search cap.

Alternatives and why not:

What you need

Not for the table: single-shot dense RAG and model-centric fine-tuning are the two alternatives we rule out in the approach above, not components to install.

Steps

  1. Measure the corpus. If the whole knowledge base is under roughly 200,000 tokens, stop: use a cached prompt and skip retrieval entirely.
  2. Parse and normalise every document to text plus structure with Docling or Unstructured (MarkItDown for simple cases). Preserve headings, titles, document identity.
  3. Chunk on structure with context. Generate a short blurb situating each chunk in its document (title, section, what the document is) and prepend it before embedding or lexical indexing.
  4. Build two indexes over the same contextualised chunks: one dense, one lexical BM25 (store built-in, SQLite FTS5, or Postgres full-text). Never ship dense-only.
  5. Retrieve top-k from each index, fuse with reciprocal rank fusion, rerank the fused set with a cross-encoder or hosted reranker, deduplicate by chunk identity, and cap the returned set to the context budget.
  6. Expose retrieval as tools, not a pipeline. At minimum search (steps 4 and 5) and read (fetch a whole document or section by id). If the corpus is navigable, mirror it as a filesystem tree and add list and grep. Describe the tools so the model can choose between them.
  7. Let the agent iterate with a bound: a maximum number of searches per turn and a rule for when to answer with what it has.
  8. Instrument retrieval quality (did the right chunk come back, at what rank) separately from answer quality. Log fused scores and, where the store allows, per-index component scores.
  9. Isolate per tenant by partitioning the index, and treat ingestion as an attack surface, not a trusted path.

Traps

Scope

Covers: the retrieval substrate for a single agent (parse to text plus structure, contextual chunking, dense plus lexical index, rank fusion, reranking, deduplication) and the shape of the loop that exposes retrieval to the agent as bounded tools. It names components with real versions and licences, and gives ordered steps and traps for that job.

Does not cover: prompt or answer-generation design beyond how retrieved context is handed over; agent orchestration outside the retrieval loop (planning, delegation, memory, tool selection); model fine-tuning or the model-centric approach, named only to be ruled out; graph retrieval and knowledge-graph construction beyond citing LightRAG as a direction; multimodal and image-based retrieval beyond citing the "just use images" argument as a live alternative; infrastructure sizing, hosting and cost modelling for the vector store; evaluation methodology in depth, since this stops at naming evaluation tools and requiring retrieval metrics be split from answer metrics.

Nothing in the source brief was run end to end. This recipe is curated, not tested: the steps above are what we would do for this job, not a record of doing them.

Sources

Every component link above comes from the source brief's component tables or source list. The figures are attributed as in that brief: contextual embeddings plus contextual BM25 for the 49 percent, contextual retrieval with a reranker for the 67 percent. We did not merge them into one number.

Open items we carried forward as unconfirmed, and did not resolve: