Why RAG systems fail
9 min read
One wrong answer, four places to look
A customer asks, "Can I return a bike after 20 days?" The system says, "No." The real policy allows 30 days. Where did it fail?
It is tempting to rewrite the prompt, since the prompt is the part you can see. But the prompt is only one of four places, and the fix for each is different. A RAG system is a pipeline, and a pipeline is debugged stage by stage.
- IndexThe document was never loaded, or was cleaned away.
- RetrieveThe search did not rank it into the top results.
- PromptIt was found, then cut by the token budget.
- AnswerIt was in the prompt, and the model ignored or distorted it.
Log each stage
You cannot debug what you did not record. For every question, save four things:
- The question as the user typed it.
- The ids of the passages the search returned, in order.
- The final prompt that was sent.
- The answer, and the sources it cited.
With those four records, any failure becomes a lookup. Without them, you are guessing.
A diagnosis in code
The program below holds four failed questions as records. For each one it checks the stages in order and reports the first stage that does not have the right passage. The ordering matters: a passage that was never indexed cannot have been retrieved, so the earliest missing stage is the cause.
cases = [
{"question": "Can I return a bike after 20 days?", "needed": "returns", "indexed": ["returns", "sale"], "retrieved": ["sale", "shipping"], "in_prompt": ["sale", "shipping"], "cited": ["sale"]},
{"question": "Is the battery under warranty?", "needed": "warranty", "indexed": ["returns", "sale"], "retrieved": ["returns"], "in_prompt": ["returns"], "cited": ["returns"]},
{"question": "How long is express shipping?", "needed": "shipping", "indexed": ["shipping"], "retrieved": ["returns", "shipping"], "in_prompt": ["returns"], "cited": ["returns"]},
{"question": "Who pays for return postage?", "needed": "returns", "indexed": ["returns"], "retrieved": ["returns"], "in_prompt": ["returns"], "cited": []},
]
stages = [
("indexed", "never indexed: check loading and cleaning"),
("retrieved", "search missed it: check chunking, embeddings, hybrid search"),
("in_prompt", "dropped before the model: check the token budget and order"),
("cited", "in the prompt but not used: check the prompt and the model"),
]
def diagnose(case):
for stage, advice in stages:
if case["needed"] not in case[stage]:
return advice
return "every stage had it: check the answer text itself"
for case in cases:
print(f"{case['question']:<38} {diagnose(case)}")It prints:
Can I return a bike after 20 days? search missed it: check chunking, embeddings, hybrid search
Is the battery under warranty? never indexed: check loading and cleaning
How long is express shipping? dropped before the model: check the token budget and order
Who pays for return postage? in the prompt but not used: check the prompt and the modelFour questions, four different stages, and four different fixes. Rewriting the prompt would help only the last one. The first needs a better search, the second is a gap in the documents, and the third needs a bigger budget or fewer repeats. The same symptom, a wrong answer, hides different causes.
The common causes, by stage
At the index
- The document was never loaded. A file type was skipped, or a page needs a login.
- Cleaning removed too much. A table was flattened into nonsense, or a heading was stripped.
- The index is stale. The policy changed and the old version is still the only one stored.
At retrieval
- A bad chunk boundary. The condition and the rule are in different ChunkingSplitting a document into passages that a retriever can return one at a time. Chunk size and boundaries decide what the model can read, so a bad cut can separate a rule from its condition., as the Lesson How to chunk documents shows.
- Different words. The user says "money back" and the document says "refund". Keyword search misses it, and Embeddings and semantic search explains the fix.
- An exact name or code. The user types a model number and embeddings do not match it. Keyword search and BM25 covers this case.
- A filter that is too strict. A date or audience rule removed the right chunk, as in Metadata, filters and permissions in RAG.
At the prompt
- A budget that is too small, or too many repeats using up the Context windowThe maximum number of tokens a model can process in its context. Depending on the model, the limit can include the answer as well as the input..
- The best passage buried in the middle of a long list, where models read less well.
- A prompt that does not say what to do when the sources are silent.
At the answer
- The model prefers what it learned in training over a passage that disagrees with it.
- Conflicting sources. Two passages disagree, and the model picks one or blends them.
- A missing refusal. Nothing relevant was found, and the model answers from memory.
Reproduce, then fix one thing
When you find a failing question, add it to the question list from Measuring retrieval quality before you fix anything. That turns a one-off complaint into a permanent test. Then change one setting, rerun the list, and read the numbers. If the failing question now passes and others fall, the change moved the problem and did not solve it.
A few habits keep a RAG system healthy over months.
- Review a sample of real questions each week, and read the worst answers.
- Give users a thumbs-down button, and send the questions behind it to the list.
- Re-run the checks whenever documents, models or prompts change.
Where do you start debugging a wrong answer?
The answer is wrong and the prompt looks fine.
The earliest missing stage is the cause. Each stage has a different fix.
The right passage was retrieved but cut by the token budget. Which stage failed?
It was in the retrieved list, not in the final prompt.
It was found, then dropped before the model. Check the budget and the order.
What do you do with a failing question?
A user reports a wrong answer.
A fixed failure becomes a permanent test.
Your reading position is saved
0 of 15 complete
Lesson complete
Why RAG systems fail
Up next