Engineering, , 5 min read
Most RAG bugs are search bugs
When a retrieval-augmented answer is wrong, the model is usually the last suspect. Measure what reached the prompt before you touch the prompt.
On this page
A team builds a question-answering feature over its documents. It demos well on ten questions. In production, users get confident, wrong answers. The reflex is to rewrite the prompt, move to a bigger model or add “only answer from the context”. Sometimes that helps. More often the model never saw the passage that contained the answer.
Retrieval-augmented generation is two systems joined together: a search system that finds passages, and a generator that writes an answer from them. The generator cannot use what the search did not return. So the first question about any wrong answer is not “why did the model say that?” It is “was the right passage in the context?”
Measure retrieval alone
Take 50 to 100 real questions. For each one, mark which document or passage contains the answer. Run only the search step and check whether that passage appears in the top k results. The share of questions where it does is recall at k. If recall at 5 is 60 percent, then 40 percent of questions are lost before generation even starts, and no prompt can rescue them.
Check the rank of the first correct passage too, because the generator gives more weight to some positions than to others and every extra passage costs tokens. Mean reciprocal rank, or MRR, is the simple summary: the average of one divided by the rank of the first correct result.
def recall_at_k(results: dict[str, list[str]], gold: dict[str, set[str]], k: int = 5) -> float: hits = sum(1 for q, ids in results.items() if gold[q] & set(ids[:k])) return hits / len(results)It is a small function, and it changes the conversation. The team stops arguing about prompts and starts looking at a number.
Why retrieval misses
When recall is low, read the misses and sort them. A handful of causes account for most of them.
Chunking cut the answer in half. A table, a procedure or a definition was split across two chunks, and neither one can be found on its own. Chunk by document structure, such as headings and list items, instead of a fixed character count, and add overlap or a pointer to the parent section.
The question and the passage use different words. The user asks about cancelling and the document says termination. Vector search handles some of this. Keyword search handles exact identifiers, such as error codes and product names. Combining both, called hybrid search, with a rank-fusion method such as reciprocal rank fusion, beats either alone on most corpora.
The index is missing metadata. “What changed in version 3?” needs a version filter. “Our policy for Germany” needs a country filter. Without metadata you are asking a vector to do a filter’s job.
The corpus is dirty. Duplicates, outdated versions, scanned PDFs with broken text, navigation boilerplate in every chunk. Audit the corpus before you index it. Open fifty random chunks and read them. You will be surprised.
The question is a poor query. Short, ambiguous or conversational questions retrieve badly. Rewriting the query, or asking one clarifying question, can lift recall at the price of an extra model call.
The index is stale. The answer changed last week and the index did not.
A worked miss
A user asks, “How many vacation days do new hires get?” The handbook has a table with one row per tenure band, and the heading above it says “Leave entitlements”. The chunker split the table after the second row, so the chunk that mentions new hires has no heading and the chunk with the heading has no new-hire row. Vector search ranks both low because neither one looks like the question.
You find this only by reading the miss. The fix is not a prompt. It is to keep tables whole, or to repeat the heading in every chunk. After the change, recall at 5 for the leave questions goes from 3 of 10 to 9 of 10, and the model, with the same prompt as before, starts answering correctly.
Reranking, when it pays
A reranker is a model that reads the question and each candidate passage together and scores relevance more precisely than vector similarity can. A common pattern is to retrieve 30 to 50 candidates cheaply and rerank them down to the top five. It adds latency and cost, so measure. If recall at 30 is high and recall at 5 is low, a reranker will probably help. If recall at 30 is already low, fix the first stage instead.
Then evaluate the answer
Only when retrieval is good do answer-quality problems become interesting. Now measure faithfulness: does every claim in the answer appear in the retrieved passages? And measure refusal: when the answer is not in the corpus, does the system say so? These two checks catch the confident, wrong answers that users remember.
Access is part of retrieval
One more thing belongs in the search layer: permissions. If a user may not read a document, retrieval must not return it, and the check has to happen at query time using the user’s identity, not by hoping the model stays quiet. A model that has seen a confidential passage can repeat it.
The order of work
Audit the corpus. Chunk with structure. Build the retrieval eval. Fix the biggest failure group. Add hybrid search and measure. Add reranking if the numbers say so. Then touch the prompt. It is less glamorous than prompt engineering, and it is where most of the improvement lives.