Debugging Generation
Trace answer sentences to their evidence, classify generation failures and fix them in order of effort.
The evidence was there, the answer was still wrong
Sometimes the trace is perfect: the right chunks were retrieved, ranked first and placed in the prompt. And the answer is still wrong. Now the problem really is generation: how the language model used (or failed to use) the evidence in front of it. Generation failures are worth separating carefully from retrieval failures, because their fixes are completely different.
The quickest test is simple: read the actual prompt. If the answer's facts are not in the prompt, it is a retrieval or context problem. If they are, and the answer still gets them wrong, it is generation.
Common generation failures
| Failure | Example | Typical causes |
|---|---|---|
| Hallucinated detail | Adds "a ₹200 late fine" not in any source | Weak grounding instructions; model fills gaps from habit |
| Ignored context | "Most colleges allow 15 days" despite the source saying 10 | Prompt doesn't require using sources; model prior wins |
| Misread evidence | Swaps the student and staff limits | Similar passages side by side; long, crowded prompt |
| Wrong citation | Cites [2] for a fact from [1], or cites [4] when there are 3 | Loose citation instructions; no post-check |
| Context overflow | Key passage cut off because the prompt was too long | Too many or too long passages; budget not enforced |
| Conflicting sources merged | "₹90,000–₹96,000" from two fee versions | No rule for conflicts; no version metadata in labels |
| Incomplete answer | Answers only the first half of a two-part question | Instructions don't ask for all parts; answer length too short |
Reading the actual prompt is the fastest way to tell the two apart.
Tracing an answer back to its evidence
To debug a generation failure, take the answer apart sentence by sentence, and for each sentence find the passage it came from. Sentences with no supporting passage are hallucinations; sentences that cite the wrong passage are citation errors; sentences that contradict their passage are misreadings.
Try it below. The prompt contained two sources, and the answer has problems of several kinds.
Fixes, in order of effort
- Tighten the instructions. State plainly: answer only from the sources; cite each claim; if the sources do not answer, say so; if sources conflict, prefer the most recent and mention the conflict; answer every part of the question.
- Improve the context. Fewer, more relevant passages; labels that include document, section and date; the most relevant passage first. Remove look-alike passages for the wrong audience with filters.
- Lower the temperature. The temperature setting controls how much randomness the model uses when choosing words; a low value (for example 0 to 0.3) makes answers more consistent and less inventive, which suits factual RAG.
- Check after generating. Run automatic citation and faithfulness checks; if a claim is unsupported, regenerate or remove it before showing the answer.
- Change the model. Larger or newer models generally follow grounding instructions better, at a higher cost. Try this after the cheaper fixes, not before.
The program below shows a small "check after generating" step: it removes sentences whose numbers do not appear in their cited source, a simple but effective guard against misquoted figures.
Expected output:
KEPT: You can take up to 10 days of medical leave [1]. Beyond 10 days, the Dean of Academics must approve [2].
REMOVED: ['Up to 14 days can be approved by your HoD [2].']
For each sentence, cites holds the source numbers it cites and numbers the figures it states. The line all(any(n in sources.get(c, "") for c in cites) for n in numbers) reads: every number in the sentence must appear in at least one of the cited sources. A sentence that fails, or that cites nothing, goes into removed.
A number check like this cannot catch every error, but it catches one of the most damaging kinds cheaply: confident, cited, wrong numbers.