Sub topic 15
Regression Tests and Production Monitoring
This chapter assumes you can compute retrieval metrics and faithfulness scores, and that you know how a bootstrap confidence interval works from the previous chapter. Here you will use those numbers to decide whether a change to your RAG system made it better or worse, and to watch quality after release.
Why a regression test needs a rule
A regression test for RAG compares a candidate system (new chunk size, new embedding model, new prompt) with the current baseline on the same evaluation set. A single before-and-after mean is not enough. With a small set, a drop of 0.02 can be noise. So the test needs a rule written down before you run it.
A workable rule has three parts:
- Fixed inputs: the same questions, labels, judge model, judge prompt, and sampling settings (temperature 0 where possible).
- A set of gated metrics, for example recall@5, nDCG@5, and faithfulness.
- A threshold on the difference, tested with a confidence interval.
Compare paired scores
Both systems answer the same questions, so compare them per question. Take the difference in score for each question, then bootstrap that list of differences. Pairing removes the variation caused by some questions being harder than others.
import random
def paired_bootstrap(baseline, candidate, n_resamples=2000, alpha=0.05, seed=0):
assert len(baseline) == len(candidate)
diffs = [c - b for b, c in zip(baseline, candidate)]
rng = random.Random(seed)
n = len(diffs)
means = []
for _ in range(n_resamples):
sample = [diffs[rng.randrange(n)] for _ in range(n)]
means.append(sum(sample) / n)
means.sort()
lo = means[int((alpha / 2) * n_resamples)]
hi = means[int((1 - alpha / 2) * n_resamples) - 1]
return sum(diffs) / n, lo, hi
def gate(baseline, candidate, max_drop=0.02):
mean_diff, lo, hi = paired_bootstrap(baseline, candidate)
if hi < 0:
return "fail: significant drop"
if lo < -max_drop:
return "warn: a drop larger than the allowed margin is plausible"
return "pass"
baseline = [1.0, 0.5, 1.0, 0.0, 1.0, 0.75, 1.0, 0.5]
candidate = [1.0, 0.5, 1.0, 0.5, 1.0, 0.75, 0.5, 0.5]
print(paired_bootstrap(baseline, candidate))
print(gate(baseline, candidate))The gate has three outcomes. If the whole interval is below zero, the drop is real and the change fails. If the interval is wide enough to include a drop bigger than your margin, the result is inconclusive, which usually means you need more questions. Otherwise the change passes.
Keep the suite useful
- Store each run's per-question scores, not just the means. You will need them to find which questions changed.
- Add every production failure you investigate to the evaluation set, with labels, so it cannot return unnoticed.
- Keep a small fast set (about 50 questions) to run on every commit and a larger set for release decisions.
- Pin the judge version. A judge model update can shift every faithfulness score without any change to your system.
Monitoring in production
In production you have no relevance labels, so you track signals that do not need them.
| Signal | How to get it | What a change suggests |
|---|---|---|
| Sampled faithfulness | Run the claim-level judge on a random 1–5% of traffic | Generator or prompt drift |
| Judge-based context relevance | Judge each retrieved passage against the query | Retriever or index drift |
| Empty or low-score retrieval rate | Log top similarity score and count of results | New topics missing from the index |
| User feedback | Thumbs down, rephrased repeat questions | Any failure, needs triage |
Sample randomly, not only from flagged answers, or your estimate will be biased toward bad cases. Plot each metric with its interval per day or week, and alert when the interval leaves a band around your release-time value. Periodically send a sample to human reviewers and recompute judge agreement, since the judge itself can drift.
Exercise
Your candidate system lowers mean faithfulness from 0.84 to 0.82 on a 40-question set, and the paired bootstrap interval for the difference is [-0.07, +0.03]. Should the gate fail the change? What would you do next?
Show answer
No, the interval includes zero, so the drop is not statistically distinguishable from noise. But it also includes -0.07, so a meaningful drop is plausible and the result is inconclusive, not a pass. Add more labelled questions, rerun the paired test, and inspect the per-question differences for a pattern before deciding.