Skip to content

Design a RAG pipeline for the corpus

Write a RAG pipeline design (chunking, chunk context, metadata, indexing, retrieval and reranking) that fits a described corpus and its questions, and judge a pipeline for the chunking or indexing choice that makes it miss answers.

Scenario

A water utility builds an assistant for its field technicians over 3,000 equipment manuals. The demo answers "how do I prime a centrifugal pump?" beautifully.

In the field, a technician asks for the restart procedure for pump model KP-450 after a power cut. The assistant returns the procedure for the KP-405, with full confidence. The chunk it found said "after a power interruption, restart the unit as follows", and nothing in that chunk said which unit.

Explanation

First, check you need RAG at all. Anthropic's Contextual Retrieval article (2024) suggests that a knowledge base under about 200,000 tokens (around 500 pages) can go into the prompt whole, with prompt caching to make that faster and cheaper. RAG is for corpora that outgrow that.

A RAG pipeline has an indexing side (run when documents change) and a query side (run per question). Each step is a design decision:

  1. Chunking. Split documents into pieces of usually no more than a few hundred tokens. Choose boundaries that match the content: sections, procedures, table rows, functions. A fixed window that cuts a procedure or a table in half produces chunks that answer nothing. Size, boundary and overlap all affect retrieval, so test them.
  2. Chunk context. A chunk often loses what it is about ("the company's revenue grew by 3%": which company, which quarter?). Contextual Retrieval has Claude write 50 to 100 tokens of context for each chunk from the whole document, and prepends it before embedding and before building the keyword index. Prompt caching keeps that one-off cost low.
  3. Metadata. Store fields with each chunk: source, title, section, version or effective date, owner, access group. Use them to filter (only current versions, only what this user may see) and to cite.
  4. Indexing. Build two indexes: embeddings for meaning, and BM25 for exact terms such as model numbers and error codes, which embeddings can miss. Embed documents and queries with the same model, and tell the model which is which (Voyage's input_type of document or query).
  5. Retrieval. Search both indexes, then merge and deduplicate with rank fusion.
  6. Reranking. Take a wide first pass (Anthropic's 2024 tests used the top 150), score each candidate against the query with a reranking model, and pass the best (they used 20) to Claude. It adds a little latency.
  7. Answering with sources. Pass chunks as search result blocks with their source and title, so Claude's citations point to your documents.

In Anthropic's 2024 tests, reported in that article, contextual embeddings plus contextual BM25 cut failed top-20 retrievals by 49%, and adding reranking by 67%. Treat those as indicative, not as what your corpus will get, and measure on your own questions: retrieval failures (the right chunk not in the top results) are what you are designing against.

Worked example

The utility's first pipeline:

Manuals split into fixed 1,000-token windows. Embeddings only. Top 5 chunks to Claude. No metadata.

Why it missed the KP-450 answer: the restart chunk never named its model, and an embedding treats "KP-450" and "KP-405" as near neighbours. A 1,000-token window also cut several procedures in two.

The redesign:

Chunking: split on the manuals' headings, one procedure or table per chunk, about 300 to 500 tokens.
Chunk context: Claude prepends a short line per chunk, e.g. "From the KP-450 pump manual, rev. 2023, section 6: restart after power loss."
Metadata: model number, manual revision, section, superseded yes/no.
Indexing: embeddings (document input type) plus a BM25 index, both over the contextualized chunks.
Retrieval: filter out superseded revisions; hybrid search with rank fusion, top 100 candidates.
Reranking: rerank to the top 15 and pass them as search results with manual and section as source.
Measure: 200 real technician questions with known answers; track whether the right chunk reaches the top 15.

Each line answers a failure: the context line and BM25 recover the exact model, heading-based chunks keep procedures whole, and the revision filter keeps old instructions out.