Papa Programmer
Lesson 15 of 15

Why RAG systems fail

9 min read

Optional

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.

  1. IndexThe document was never loaded, or was cleaned away.
  2. RetrieveThe search did not rank it into the top results.
  3. PromptIt was found, then cut by the token budget.
  4. AnswerIt was in the prompt, and the model ignored or distorted it.
Where the right passage can be lost

Log each stage

You cannot debug what you did not record. For every question, save four things:

  1. The question as the user typed it.
  2. The ids of the passages the search returned, in order.
  3. The final prompt that was sent.
  4. 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.

python
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:

text
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 model

Four 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.
Find the failing stageIndex, retrieve, prompt or answer. Each has its own fix.
Log every stageThe question, the retrieved ids, the final prompt and the cited sources.
Turn failures into testsEach wrong answer you fix joins the list that guards the next change.

Your reading position is saved

0%

0 of 15 complete

Contents

Learning tools