Sub topic 13
Finding out whether the retriever or the generator is at fault
This chapter assumes you can read Python, know precision and recall, and have already built an evaluation set with relevance labels. You should also know how to compute recall@k, faithfulness, and answer relevance from the earlier chapters. Here you combine those scores to decide which component to fix.
Why a single score is not enough
A RAG system has two parts that can fail independently. The retriever finds passages. The generator writes an answer from them. If you only measure the final answer, a bad result tells you nothing about where to spend effort.
Fixing the wrong part wastes time. Rewriting the prompt does not help when the right passage was never retrieved. Tuning the embedding model does not help when the right passage was retrieved and the model ignored it.
The approach is to score each stage separately on the same questions, then read the scores together.
The signals you need
For each question, collect four numbers:
| Signal | Question it answers | Needs labels? |
|---|---|---|
| Recall@k | Did the relevant documents reach the generator? | Yes |
| Context relevance | Was the retrieved text on topic? | No, if you use a judge |
| Faithfulness | Did the answer stay within the retrieved text? | No |
| Answer relevance | Did the answer address the question? | No |
Recall@k and context relevance describe the retriever. Faithfulness and answer relevance describe the generator. A fifth check, answer correctness against a reference answer, tells you whether the whole system succeeded.
A decision table
Read the scores for one question as a pattern. Treat "high" and "low" as above or below a threshold you pick for your data, such as 0.8 and 0.5.
| Recall@k | Faithfulness | Answer correct | Likely cause |
|---|---|---|---|
| High | High | Yes | Working as intended |
| Low | High | No | Retriever failure. The generator faithfully summarized the wrong context. |
| Low | Low | No | Both failed, or the generator guessed from its own knowledge because context was missing. |
| High | Low | No | Generator failure. The right context was present and the answer went beyond it or contradicted it. |
| High | High | No | Check the label, the reference answer, or whether the context itself is wrong or outdated. |
The last row matters. If the context is relevant, the answer is faithful, and the answer is still wrong, the source document may contain an error, or your reference answer may be stale. Neither the retriever nor the generator is at fault.
Classifying each question in code
You can turn the table into a function and run it over your whole evaluation set. This example assumes you already computed the scores.
def diagnose(recall, faithfulness, correct, recall_threshold=0.5, faith_threshold=0.8):
"""Assign one failed question to a likely cause.
recall: recall@k for the question, or None if no labels exist.
faithfulness: claim-level score, or None if the answer had no claims.
correct: True if the answer matches the reference.
"""
if correct:
return "ok"
if recall is None or faithfulness is None:
return "unscored"
retrieved_ok = recall >= recall_threshold
faithful = faithfulness >= faith_threshold
if not retrieved_ok and faithful:
return "retriever"
if retrieved_ok and not faithful:
return "generator"
if not retrieved_ok and not faithful:
return "both"
return "check_data"
results = [
{"id": "q1", "recall": 0.0, "faith": 1.0, "correct": False},
{"id": "q2", "recall": 1.0, "faith": 0.4, "correct": False},
{"id": "q3", "recall": 1.0, "faith": 1.0, "correct": True},
{"id": "q4", "recall": 0.0, "faith": 0.2, "correct": False},
{"id": "q5", "recall": 1.0, "faith": 1.0, "correct": False},
{"id": "q6", "recall": None, "faith": 1.0, "correct": False},
]
counts = {}
for r in results:
label = diagnose(r["recall"], r["faith"], r["correct"])
counts[label] = counts.get(label, 0) + 1
print(counts)Running it prints {'retriever': 1, 'generator': 1, 'ok': 1, 'both': 1, 'check_data': 1, 'unscored': 1}. Counting the labels across the set shows which component accounts for most failures. That count, not any single example, tells you where to work.
Why "faithful" can mislead
Faithfulness measures support from the retrieved context. It does not measure truth. A generator that faithfully repeats an irrelevant passage scores 1.0. This is why the retriever-failure row has high faithfulness.
The reverse also happens. If the retriever returns nothing useful, the generator may answer from its training data. The answer can be correct and still score low on faithfulness, because the claims are not in the context. Do not treat that as a generator bug. It is a retriever miss that the model happened to cover. In production this is risky, since the model may be wrong next time.
Isolating a component with controlled inputs
Reading scores gives a likely cause. To confirm it, change one component and hold the other fixed.
Test the generator with perfect context
Replace the retrieved passages with the labeled relevant passages and run the generator again. This is often called an oracle retrieval test.
- If answers become correct, the generator is fine and retrieval was the bottleneck.
- If answers stay wrong, the generator, the prompt, or the data has a problem.
Test the retriever without the generator
Retrieval metrics need no generator at all. Compute recall@k and nDCG directly from the ranked document ids. If recall@k is low here, no prompt change can fix it.
Compare the two ceilings
The oracle test gives an upper bound for what better retrieval can achieve. Compare three accuracy numbers:
- Current system accuracy.
- Accuracy with oracle context.
- Perfect score of 1.0.
The gap between 1 and 2 is the most you can gain by improving retrieval. The gap between 2 and 3 is the generator's own error. Fix the larger gap first.
def headroom(current_acc, oracle_acc):
return {
"retrieval_headroom": round(oracle_acc - current_acc, 3),
"generator_headroom": round(1.0 - oracle_acc, 3),
}
print(headroom(current_acc=0.58, oracle_acc=0.86))
# {'retrieval_headroom': 0.28, 'generator_headroom': 0.14}Here retrieval limits the system twice as much as generation does.
Edge cases that distort the diagnosis
- Recall is high but the passage is buried. The relevant document is at rank 9 and k is 10. Recall@10 is 1.0, but if the prompt fits only five passages, the generator never sees it. Check the number of passages actually placed in the prompt, not only your evaluation k. Rank-aware metrics such as nDCG expose this.
- Relevant passages are present but noisy. Many irrelevant passages around the right one can distract the generator. Context relevance catches this when recall@k does not. Low precision with high recall points to a retriever that needs a reranker or a smaller k.
- Missing labels. Recall is undefined without labeled relevant documents. Return
None, as the earlier function does, and exclude those questions from the retriever side. Do not count them as zero. - Claim extraction errors. A weak claim extractor can split an answer badly and lower faithfulness without the generator being at fault. Spot-check a sample of extracted claims.
- Judge bias. An unvalidated judge can mark faithful answers as unsupported, especially long ones. Validate it against human labels before trusting a "generator failure" count.
- Multi-hop questions. An answer may need two documents. Recall@k can be 0.5 because one of two was found, and the generator then fills the gap. Report these questions as their own slice.
Slicing results
An overall percentage hides patterns. Group failures by question type, document source, or length, and compute the diagnosis counts inside each group. A retriever that fails only on questions containing product codes suggests a tokenization or keyword-matching problem. A generator that fails only on long contexts suggests a context-window problem.
from collections import defaultdict
def slice_counts(rows):
table = defaultdict(lambda: defaultdict(int))
for row in rows:
table[row["type"]][row["cause"]] += 1
return {k: dict(v) for k, v in table.items()}
rows = [
{"type": "product_code", "cause": "retriever"},
{"type": "product_code", "cause": "retriever"},
{"type": "policy", "cause": "generator"},
{"type": "policy", "cause": "ok"},
]
print(slice_counts(rows))Using the result
Match the failure to the fix:
- Retriever failures: change chunk size, add keyword search to vector search, tune the embedding model, add a reranker, or improve query rewriting.
- Generator failures: tighten the prompt to answer only from context, ask for citations, reduce the number of passages, or switch models.
- Data failures: correct or update the source documents and the reference answers.
Change one thing at a time and rerun the full evaluation. Report the change with a confidence interval, as the statistics chapter describes, because a small set can show a shift that is only noise.
Exercise
A question has recall@5 of 0, faithfulness of 0.95, and an incorrect answer. A teammate proposes rewriting the generation prompt. Is that the right fix, and what test would you run to confirm your reasoning?
Show answer
No. Zero recall means the relevant document never reached the generator, and the high faithfulness shows the generator accurately summarized the irrelevant context it received. The retriever is at fault. To confirm, run an oracle retrieval test: give the generator the labeled relevant passages for this question and for similar ones. If answers become correct, retrieval is the bottleneck, so work on chunking, hybrid search, reranking, or query rewriting instead of the prompt.
Exercise
Your system scores 0.60 accuracy. With oracle context it scores 0.90. Which component has more headroom, and by how much?
Show answer
Retrieval has 0.30 of headroom (0.90 minus 0.60), and the generator has 0.10 (1.0 minus 0.90). Retrieval has three times as much room for improvement, so fix it first.