Search Tutorials
--Ads--Debugging Common RAG Failure Modes
A step-by-step method to find out whether retrieval or generation is at fault.
When a RAG system gives a wrong answer, the instinct is to change the prompt. Often the prompt is not the problem. A wrong answer can start anywhere in the pipeline, and fixing the wrong stage wastes time or makes things worse. This lesson gives you a systematic method: trace the answer backwards, stage by stage, until you find where the evidence was lost.
The four questions
- Is the answer in the indexed documents at all? Search the raw extracted text, not the vector index. If it is missing, no retrieval technique can help: the source document is missing, failed to parse, or the index is stale.
- Was the right chunk retrieved? Search with a large k (50) and find the rank of the chunk containing the answer. If it is not in the top 50, it is a retrieval problem.
- Did it reach the final prompt? If the chunk was at rank 12 but you send the top 5, or a reranker or threshold dropped it, it is a post-retrieval problem.
- Is the answer faithful to it? If the right chunk was in the prompt and the answer is still wrong, the problem is generation: prompt, conflicting sources, or reasoning.
A debugging helper
This function answers the four questions automatically for one question and an evidence phrase that should appear in the correct source.
debug_rag.py
def norm(s: str) -> str:
return " ".join(s.lower().split())
def debug_question(question: str, evidence: str, raw_docs: dict[str, str], chunks: list[dict],
search, build_prompt, answer: str, final_k: int = 5) -> None:
ev = norm(evidence)
# 1. In the source text?
in_docs = [name for name, text in raw_docs.items() if ev in norm(text)]
print("1. Evidence in documents:", in_docs or "NO -> content gap or parsing failure")
if not in_docs:
return
# 1b. In the chunks? (catches evidence split across a chunk boundary)
in_chunks = [c["id"] for c in chunks if ev in norm(c["text"])]
print(" Evidence in chunks: ", in_chunks or "NO -> split across chunks; check chunking/overlap")
# 2. Retrieved?
hits = search(question, k=50)
rank = next((r for r, h in enumerate(hits, start=1) if h.id in in_chunks), None)
print("2. Rank of evidence chunk:", rank or "not in top 50 -> retrieval problem")
if rank:
print(" Top 3 instead:", [(h.id, round(h.score, 3)) for h in hits[:3]])
# 3. In the prompt?
prompt = build_prompt(question, hits[:final_k])
print("3. Evidence in final prompt:", ev in norm(prompt))
# 4. Faithful?
print("4. Answer:", answer[:300])
print(" (check faithfulness with the generation-metrics judge if step 3 is True)")
Common failures and fixes
| Symptom | Likely cause | Fix | Lesson |
|---|---|---|---|
| "I don't know" for documented facts | Text not extracted (scans, tables) | Better parser, OCR or vision model | Parsing |
| Outdated answers | Index not synced; old versions indexed | Incremental sync, archive old versions | Re-indexing |
| Half an answer | Evidence split across chunks | Overlap, structure-aware or parent-child chunking | Parent-child |
| Fails on codes, names, numbers | Dense-only retrieval | Hybrid search | Hybrid search |
| Fails on casual or vague questions | Vocabulary mismatch | Query rewriting, multi-query, HyDE | Rewriting |
| Follow-ups fail in chat | Pronouns in the search query | Query condensing | Condensing |
| Right chunk at rank 8, k = 5 | Weak ranking | Reranker, retrieve wider | Reranking |
| Chunk from the wrong product or year | Missing context in chunk | Contextual headers, metadata filters | Contextual retrieval |
| Answer ignores the provided source | Weak grounding instructions | Prompt with reasons and an explicit fallback | Prompt templates |
| Answer blends two versions | Conflicting sources | Dates in headers, conflict rules | Conflicts |
| Wrong totals or comparisons | Arithmetic over text | Text-to-SQL or code for calculations | Text-to-SQL |
| Correct-looking but uncited claims | Model used its own knowledge | Citations, groundedness checks | Citations |
Debugging habits
- Print the final prompt. Most mysteries disappear when you read exactly what the model saw.
- Debug one failure fully before changing anything, then look for the same pattern in other failures.
- Turn each fixed failure into a test case so it stays fixed.
- Change one thing at a time and re-run the evaluation set.
Key takeaways
- Trace wrong answers backwards: documents, chunks, retrieval rank, prompt, generation.
- Each stage has its own class of fixes; fixing the wrong stage wastes effort.
- Automate the trace with a helper and an evidence phrase.
- Read the final prompt, fix one thing at a time, and add regression tests.