RAG is a retrieval problem

If the right passage never makes it into the context, no prompt will save the answer. Most RAG failures are retrieval failures in a generation costume.

One team had built a RAG system that answered roughly 60% of questions correctly. They tried a bigger model, a longer prompt, a stricter system message, JSON output. Nothing moved. The retriever was returning three chunks that looked relevant and were not; the fourth chunk, the one with the answer, never made it into the context.

RAG is usually drawn as “retrieve, then generate.” The second step gets the attention; the first step determines the ceiling.

index:  parse → chunk → embed → store + metadata
query:  rewrite → retrieve → rerank → assemble → generate

The pipeline is a search product

Indexing is a data problem: parse the documents, split them into chunks, embed them, keep the metadata that will later be needed for filtering. Querying is a search problem: rewrite the question, retrieve candidates, rerank them, assemble a context, generate, and cite. Each stage has its own failure modes. An end-to-end score tells you that something is wrong; it does not tell you where.

Evaluate retrieval separately. Build a set of questions with the passage that contains each answer. Measure recall@k: did the retriever return that passage at all? If recall@20 is 60%, a perfect generator still fails 40% of the questions that need the passage. Then measure ranking (MRR, nDCG), and only then answer quality.

Chunking is where the answer goes to die

Fixed-size chunks split tables, code blocks, and definitions. Overlap is a band-aid. Structure-aware chunking — headings, sections, code fences, table boundaries — preserves the thing the question is about. Small chunks improve precision but strip context; large chunks dilute the embedding and eat the context window. There is no universal size; there is a measurement.

Metadata filters are often better than a newer embedding model. If the answer is always in the current product docs, filter by product and version before you compare embedding models. If the question is date-sensitive, filter on the date. A retriever that ignores metadata is leaving the cheapest signal on the table.

Dense retrieval is not enough

Embeddings are good at paraphrase and bad at exact identifiers, rare names, error codes, and numbers. BM25 is the opposite. Hybrid search — BM25 plus dense, fused with reciprocal rank fusion or a tuned weighted score — catches both. It is usually a few lines of code and a meaningful improvement on real corpora.1

A cross-encoder reranker over the top 50 is often the single biggest win. It sees the query and the passage together, which is exactly what a bi-encoder cannot do; that is also why it is slower. Rerank a shortlist, not the whole corpus.2

top-k is a budget, not a quality knob. More chunks can hurt: the model has to find the needle in a larger haystack, and “Lost in the Middle” shows that models use the beginning and the end of the context better than the middle. Put the best chunk first or last; do not stuff fifty into the prompt and hope.

Evaluation that tells you where it broke

Build a small, boring evaluation set before you tune anything. For each question, keep the source passage, the expected answer, and the filters that should have been applied. Then log:

  • Retrieval: was the source passage in the top k? Which rank? Which retriever found it? This is your ceiling.
  • Generation: is the answer correct, and is every claim supported by a retrieved passage? An answer can be right for the wrong reason.
  • Refusal: when the corpus has no answer, does the system say so? A confident answer with no source is worse than a refusal.
  • Latency and cost: the reranker and the extra chunks are not free. A two-point quality gain that triples latency is a product decision.

An LLM judge is useful at scale, but calibrate it against human labels on a sample and never let it grade the retrieval it cannot see. Log the retrieved IDs and scores. A failure you cannot reproduce is a rumour, not a bug.

The model can only cite what it was given. Build the search first, then the prompt. If recall is bad, a better prompt is just a more eloquent way to be wrong.

  1. Reciprocal rank fusion is the default for a reason: it is rank-based, so it does not require the BM25 and embedding scores to live on the same scale. Tune the weights on your own queries; the defaults are a starting point. ↩
  2. A cross-encoder reads the query and passage together. A bi-encoder embeds them independently. That architectural difference is why the reranker is better and why you cannot precompute it for the whole corpus. ↩