Intermediate project
RAG failure clinic
Builds on 3 lessonsabout 1 h 25 min of reading first
In this project
What you need first
- Completed:The core RAG pipelineRetrieval-augmented generation
- Completed:Better retrievalRetrieval-augmented generation
- Completed:RAG evaluation and hallucinationRetrieval-augmented generation
Skills you will practise
- Retrieval
- Retrieval-augmented generation
- RAG evaluation
- Hallucination control
Problem
Learn to diagnose a RAG system the way you would on call. You take a small RAG system that works, break it five ways by following a written fault spec, and for each fault you record the symptom, find the metric that exposes it, name the failing stage with evidence, apply the smallest fix, and show the metric moved.
Requirements
- A small working RAG system: your RAG eval harness result or one of your own, with 20 to 100 documents you wrote or may use (record the license; no private data). It must already have a group filter inside retrieval, a score gate, and a prompt with a grounding sentence (“answer only from the context”) and a separate refusal sentence; add any that are missing. Two user groups, each document readable by one or both, with a random canary string in each restricted document.
- Logging of a request id, retrieved chunk ids, their scores and the final prompt for every request.
- A golden set of at least 20 queries including every required type. The gold for each answerable query is the document plus the exact answer text span (not a chunk id, because Fault 2 changes chunks) and the expected answer. Include at least 5 unanswerable queries, 2 about a fact you will edit, 2 that the two groups should answer differently, 2 where the corpus contradicts common knowledge (for example an unusual refund period), and 2 where an on-topic chunk passes the gate but lacks the specific fact asked for.
- A baseline measured before any fault, each of eight metrics with its own denominator (for example 3 of 5 unanswerable): span recall@k (a hit is a retrieved chunk containing the whole span; lower is bad), gate refusals on unanswerable queries (higher is better), gate refusals on answerable queries (lower is better), answer correctness and faithfulness on answered queries (lower is bad), abstain correctness (correct refusals, lower is bad; wrong refusals, higher is bad), leaks (two counts: retrieved chunk ids, and canaries in answers; higher is bad), and stale answers (higher is bad). Count leaks by running the whole golden set once as each group: retrieved chunk ids from documents that group may not read (primary) and canaries in answers (backstop), using the chunk-id and canary checks defined in RAG with per-user permissions.
- The five faults injected one at a time, restoring the baseline between them, each with a diagnosis log.
k, the score threshold and the model stay fixed across all runs, except where a fault or its fix changes it.- Acceptance, for each fault: the log shows the exposing metric at baseline, with the fault, and after the fix. Metrics computed from retrieval logs (span recall@k, retrieved-id leaks, both gate-refusal counts) return to the baseline value exactly, or better, or differ by a rank tie you can show in the log or confirm by an exact (brute-force) search. Metrics computed from generated answers (answer correctness, faithfulness, abstain correctness, stale answers, answer canaries) return to no worse than baseline by more than your measured noise, in that metric’s bad direction, where noise is the largest difference between two baseline runs at the lowest temperature the model allows, or one query, whichever is larger, per metric. Name every query whose result changed. A fault that moves no metric needs no diagnosis-driven fix; revert it before the next fault. Its log records the queries, how you strengthened the fault (for example allow own knowledge, add 3 more contradiction queries) and the result, and it counts as located. A reported null result is not a fail.
Prerequisites
Tasks
- To do: Set up the groups, canaries, logging and golden set, and write down the baseline. Run the baseline twice before Fault 1; the second run gives your noise.
- To do: For each fault: inject it, run the golden set, and fill in the diagnosis log entry.
- To do: Diagnose using the debugging tree in Better retrieval and the Symptom to stage table below: rank hypotheses by likelihood and cost to check, run the cheapest check first (for example paste the gold passage into the prompt to separate retrieval from generation), and write the evidence.
- To do: Apply one minimal fix (one diff or commit per fault), re-run the golden set.
- To do: Re-run the baseline after the last fault and check it matches under the acceptance rule.
- To do: Draw the which-lane-failed diagram: the two lanes with each fault on its stage.
- To do: Stretch: write one regression test per fault that fails on the faulty system and passes on the fixed one. Run it against the faulty version to prove it fails.
Fault spec
Starter material is this description only: you write the changes in your own system. Diagnose from the symptom, the logs and the checks you run, not from knowing the fault.
| # | Fault | What to change | Stage |
|---|---|---|---|
| 1 | Stale index | Edit a fact in one source document, update the expected answers of the affected queries to the new value, and do not re-ingest | Ingestion |
| 2 | Bad chunking | Re-ingest with a tiny fixed size, no overlap and no respect for headings, so sentences and tables are cut | Chunking |
| 3 | No permission filter | Remove the group filter from retrieval | Retrieval |
| 4 | Missing abstain | Remove the score gate and the “say you do not know” sentence; keep “answer only from the context” | Gate and generation |
| 5 | Grounding removed | Remove only “answer only from the context” and keep the refusal sentence, so the model may answer from what it already knows. The contradiction and lacks-the-fact queries are where it shows | Generation |
Diagnosis log
One entry per fault, each field short.
| Field | What to write |
|---|---|
| Fault and query | Fault number, the query you studied, the fault’s diff or commit, and a prompt, config or log excerpt showing it in effect |
| Symptom and metric | What the user sees; metric name, baseline and faulty values, query count, changed queries |
| Hypotheses and check | Two or three ranked hypotheses; the check run, the request id and logged lines (ids, scores or prompt excerpt) it showed, and the check that separated the named stage from the one upstream |
| Failing stage | The earliest broken stage |
| Fix and metric after | The single change that puts right what the fault broke (a diff, commit or one step such as re-ingest); the value after, the changed queries after the fix, and whether other metrics held. For a null result: the strengthening and its result |
Evaluation
Record real measurements and never invent numbers.
| Criterion | 0 | 1 | 2 | 3 |
|---|---|---|---|---|
| Baseline and golden set | No baseline | Baseline on fewer than 20 queries, a query type missing, or a metric without a denominator | Starting system, corpus size, license, groups, canaries and logging as required; 20 or more queries including every required type; all eight metrics with denominators; the baseline re-run after the last fault matches under the acceptance rule | As level 2, with the baseline run three times and a noise band per metric |
| Each fault located with evidence | No fault located with log evidence | One to four located with log evidence | All five located (a null result with the strengthening recorded counts as located). Each entry has the fault’s diff or commit and an excerpt showing it in effect, and quotes raw log lines (request id, chunk ids and scores, or a prompt excerpt) plus the result of a check separating the named stage from the next one upstream (for example the gold passage pasted into the prompt). Earliest broken stage named, cheapest check first, which-lane diagram drawn | As level 2, with faults injected blind (a seeded script or another person), the order recorded after diagnosis, and any wrong first guess kept |
| Fix minimal | No fix, or a stage rewritten | A fix changes something beyond what the fault broke, or a fault is fixed across more than one diff or step, the acceptance rule fails, or any other level-2 condition not met | Each fix puts right exactly what the fault broke, as one diff, commit or single step (for Fault 1, re-ingesting the edited document, keeping the edit and the updated expected answers), with k, threshold and model unchanged except where a fault or its fix changes it, and the acceptance rule holds | As level 2, plus a regression test per fault that fails on the faulty build and passes on the fixed one |
| Before and after metric | No metric | Metric after only, denominators missing, the acceptance rule missed on any fault, or any other level-2 condition not met | The acceptance rule met for all five faults, changed queries named, and null results reported and re-measured | As level 2, with a per-query table of baseline, faulty and fixed values for every fault |
| No fix without a measurement | Fixes first | Some fixes preceded by a measurement | Every fix follows a logged measurement that pointed at the stage (a fault with a null result needs no fix) | As level 2, with the metric change the fix should cause written before applying it |
Pass mark: at least 2 on every criterion (so the lowest passing total is 10 of 15).
Architecture
Symptom to stage
| Symptom | Likely stage | First check | Metric that moves |
|---|---|---|---|
| Old figure or policy, stated confidently | Ingestion (stale index) | Is the retrieved chunk the old version? Compare the chunk text with the source | Stale-answer count |
| Right document retrieved, answer missing or wrong | Chunking | Does the retrieved chunk contain the answer, or was it cut? | Span recall@k |
| Another group’s text in an answer | Retrieval (filter) | Which ids were retrieved, and who may read them? | Leak count |
| Confident answer to an unanswerable question | Gate and generation | Was any relevant chunk retrieved? Is there a refusal rule? | Abstain correctness |
| Details the chunks never state | Generation | Is the right chunk in the prompt? Does each claim appear in it? | Faithfulness, answer correctness |
| Correct chunk not in the top-k | Retrieval | Wrong query, filter or embedding? Ranked out? | Span recall@k |
Interview questions
Provenance: Generated practice. Not reported from any company.
- A wrong answer although the right document is in the corpus: what do you check, in order? Practise with question 35 in the Top 100 priority questions.
- The chatbot confidently states a policy that does not exist. Practise with S1 in the Scenario-based bank.
- A document was updated but search returns the old version. Practise with S5 in the Scenario-based bank.
- Retrieval returned the right chunk and the system still hallucinates. Practise with A1 in the Advanced bank.
Portfolio checklist
- To do: The README has the baseline metrics with query counts.
- To do: Five complete diagnosis logs with evidence lines, and a before, faulty and after table.
- To do: The which-lane-failed diagram.
- To do: No private data is in the repository.