Sub topic 16
Diagnosing common failures
This chapter assumes you have built a basic RAG pipeline: chunking, embeddings, top-k retrieval, and a grounded prompt. It also assumes you know the recall_at_k metric from the evaluation chapter. Here you will learn to locate which stage failed when an answer is wrong.
Start by splitting the pipeline in two
A wrong answer has one of two broad causes. Either retrieval did not surface the right passage, or the model had the right passage and still answered badly. Check retrieval first, because generation cannot fix missing context.
Common failures and their symptoms
| Symptom | Likely cause | Typical fix |
|---|---|---|
| Answer is missing a fact that you know is in the documents | Text never ingested, or a PDF parser dropped tables | Inspect the cleaned text |
| Retrieved chunks are on topic but lack the detail | Chunks too small, or a boundary split the answer | Larger chunks, more overlap |
| Retrieved chunks are vague and mixed | Chunks too large | Smaller chunks |
| Exact names or error codes are not found | Embeddings blur rare tokens | Add keyword search and fuse the rankings |
| Right chunk ranks 8th, and k is 5 | Weak ranking | Reranking, or a larger k followed by a rerank |
| Confident answer that no passage supports | Model ignores the context | Stricter prompt, lower temperature, a refusal path |
| Answer uses an outdated fact | Stale index | Re-ingest changed documents |
Automate the first check
Keep a small set of questions, each paired with the ids of the chunks that should answer it. Then classify each failure by stage. This function reuses recall_at_k.
def recall_at_k(retrieved_ids, relevant_ids, k):
relevant = set(relevant_ids)
if not relevant:
return 0.0
hits = set(retrieved_ids[:k]) & relevant
return len(hits) / len(relevant)
def diagnose(retrieved_ids, relevant_ids, k, answer_supported):
recall = recall_at_k(retrieved_ids, relevant_ids, k)
if recall == 0.0:
return "retrieval miss"
if recall < 1.0:
return "partial retrieval"
if not answer_supported:
return "generation failure"
return "ok"
print(diagnose(["c9", "c4", "c7"], ["c2"], 3, False)) # retrieval miss
print(diagnose(["c2", "c4", "c7"], ["c2"], 3, False)) # generation failure
print(diagnose(["c2", "c4", "c7"], ["c2"], 3, True)) # okThe answer_supported flag comes from a human check, or from a second model call that asks whether each claim appears in the context. Treat the model-based check as a hint, since it can be wrong too.
Debugging habits
- Print the retrieved chunks and their scores for every failing question. Read them as the model would.
- Look at the chunk that should have matched. Ask whether a human could answer from that chunk alone.
- Change one thing at a time, then rerun the whole question set. Fixing one question often breaks another.
Exercise
A user asks for the meaning of error code "E-4417". The manual contains it, but the top 5 chunks are general troubleshooting text. Which stage failed, and what is one fix?
Show answer
Retrieval failed (recall at 5 is 0). Dense embeddings handle rare identifiers poorly. Add keyword search (for example BM25) and merge it with the vector results using reciprocal rank fusion. Reranking over a larger candidate set also helps.