Block Regressions with a Gate
Once an LLM app has a test set and graders, every change to its prompt, its model or its code can be measured. A regression gate turns that measurement into a decision. It compares the scores of a new version with the saved scores of the version you last accepted, and it answers yes or no. A person, a script or an automated check can then act on that answer, so a version that got worse is stopped before it replaces the one that worked.
The baseline: saved scores of the accepted version
A gate needs something to compare with. That reference is the baseline: the scores the currently accepted version of the app got on the test set, saved to a file. Every later version is run on the same cases with the same graders, and its scores are set beside the baseline, one grader at a time.
The baseline is small. For each grader it holds the pass rate the accepted version reached, and it is often worth keeping which cases passed and which failed, so that a later run can be compared case by case and not only by its totals.
{
"version": "prompt v7",
"cases": 40,
"scores": {
"has_required_facts": 0.925,
"valid_format": 0.975,
"grounded_in_source": 0.875,
"no_refund_promise": 1.0
}
}The file lives in version control (a tool such as Git that records every change to a file), in the same repository as the test set and the graders. That placement matters for two reasons. The scores only mean something together with the cases they were measured on, so the baseline and the test set should change together and be read together. And because every change to the baseline becomes a recorded change, anyone can later see when the reference moved, from which numbers to which, and who moved it.
True or false?
A baseline is useful even if the test set it was measured on has changed since.
False. A pass rate depends on which cases were asked. Scores from a different set of cases cannot be compared fairly, which is why the baseline is kept and changed together with its test set.
Keeping the baseline in version control means every change to it can be traced later.
True. Each change is a recorded commit, so the history shows when the reference moved and by how much.
A gate is one command with an exit code
A gate is a single command you can run in a terminal. It reads the new scores and the baseline, applies its rules, prints what it found, and ends with a yes or a no. The yes or no is not the printed text. It is the exit code: a small whole number that every program hands back to whatever started it when it finishes.
Shells and the tools built on them share one convention: an exit code of 0 means success, and any other value means failure. Because the convention is shared, many things can stop on the same number:
- a person sees it in the terminal;
- a script can check it and stop;
- a Git hook (a script Git runs before a commit or a push) that exits with a nonzero status stops that commit or push;
- a continuous integration service (a system that runs checks automatically on every change, such as GitHub Actions) marks the check as failed.
On GitHub a repository can require that check to pass before a change is merged. A failing gate then blocks the change without anyone having to read the log.
- New scores this run
- Baseline accepted version
- Gate compare with rules
- Exit code 0 change may go ahead
- Exit code 1 change is stopped
- New scores to Gate
- Baseline to Gate
- Gate to Exit code 0: no rule broken
- Gate to Exit code 1: a rule broken
In Python a script sets its exit code with sys.exit(). sys.exit(0), or reaching the end of the script, reports success. sys.exit(1) reports failure, and sys.exit("message") prints the message to standard error and exits with code 1. Keep to small values: 1 for a failed gate is the usual choice, and Python's documentation notes that most systems expect codes between 0 and 127. The end of a gate script can be as plain as this:
for line in failures:
print("FAIL", line)
sys.exit(1 if failures else 0)Two details catch people out. The first: sys.exit() works by raising an exception, SystemExit. That exception inherits from BaseException rather than Exception, so an except Exception: block elsewhere in the script does not swallow it. The second: a gate's output is sometimes piped into another command, as in python gate.py | tee log.txt. A shell then reports the exit code of the last command in the pipe, here tee, unless it is told otherwise (in bash, with set -o pipefail). The gate's failure can be lost on the way.
True or false?
A gate that prints FAIL in red but exits with code 0 will stop an automated check.
False. Automated checks read the exit code, not the text. The script has to exit with a nonzero code, for example with sys.exit(1).
In Python, sys.exit("gate failed") prints the message and reports failure.
True. Any object other than an integer or None is printed to standard error, and the exit code is 1.
Wrapping the gate's main code in try / except Exception will silently turn sys.exit(1) into success.
False. SystemExit inherits from BaseException, so except Exception does not catch it. A bare except: or except BaseException: would.
How much of a drop to allow
The obvious rule is to fail whenever a score is lower than the baseline. For an LLM app it does not work. The model's output varies, so two runs of an unchanged app on the same cases score differently. A rule that fails on every small drop fails on noise. People learn to rerun it until it passes, and then it stops protecting anything.
The way out is to measure that variation first. Run the unchanged, accepted version several times on the same test set and look at how far its score moves on its own. In the illustrative runs below, the same app scored between 85 and 90 percent, so a drop of up to 5 points tells you nothing by itself.
pass rate (%) against run
- each run
- highest, 90%
- lowest, 85%
That spread sets the tolerance: how far each score may fall below its baseline before the gate calls it a regression. Statistics gives a rough sense of its size in advance, through the standard error: roughly how far a measured pass rate typically lands from the app's true rate by chance alone. When each case simply passes or fails, the standard error of a pass rate \( p \) measured on \( n \) cases is:
standard error of a pass rate
about 4.7 points
On 40 cases one case is already 2.5 points of the rate, and the expected wobble is several points. That matches what the repeated runs show. Set the tolerance from your own measured runs, for example as the largest drop seen between runs of the unchanged app. The gate then ignores that wobble and still fails on a drop beyond it. A larger test set narrows the spread and lets the tolerance shrink with it.
Must-pass checks
Some checks are not about quality in general but about something that must never happen: promising a refund the policy does not allow, revealing private data, answering in a format the rest of the system cannot parse. For these a tolerance makes no sense, because one failure is already one too many. They get a stricter rule: every case must pass, or at least no case that passed in the baseline may fail now. Keeping them as their own small group lets the gate be relaxed about noise and strict about harm at the same time.
| Check | Baseline | New run | Change | Rule | Result |
|---|---|---|---|---|---|
| has_required_facts | 92.5% | 90.0% | -2.5 points | may drop 5 points | pass |
| valid_format | 97.5% | 97.5% | 0.0 points | may drop 5 points | pass |
| grounded_in_source | 87.5% | 77.5% | -10.0 points | may drop 5 points | fail |
| no_refund_promise | 100% | 97.5% | -2.5 points | must pass every case | fail |
In the decision above the first two drops are within the noise and pass. The 10-point fall in groundedness is twice the tolerance and fails. The must-pass check fails on a single case, a drop that a tolerance would have let through. The gate should print which cases changed in each direction, so the person who reads a failure can open those answers straight away.
Demanding that every case of every check passes on every run is the strict rule applied everywhere, and with varying output it fails often even for a good app. If 40 cases each pass independently 98 percent of the time, the chance that all 40 pass in one run is \( 0.98^{40} \approx 0.45 \): more than half of the runs of a perfectly good version would be blocked.
True or false?
Any score below the baseline means the new version is worse.
False. Two runs of the same unchanged app score differently. Only a drop larger than the measured run-to-run variation is evidence of a regression.
A must-pass check can fail the gate even when its score dropped by less than the tolerance.
True. Must-pass checks have no tolerance: one failing case is enough, because the behaviour they guard must never happen.
Making the test set larger lets the gate use a smaller tolerance.
True. The standard error shrinks as the number of cases grows, so the score moves less between runs and a smaller drop can be trusted.
What a gate runs, and what it costs
A gate is meant to run often: before every commit, on every proposed change, after every model upgrade. When it produces fresh answers, each run makes live model calls, and those take time and cost money on every run. The count is easy to work out: one call per case to get the app's answers, plus one call per case for every model grader, because each verdict is its own call. Code graders add nothing, since they run locally on the saved answers.
model calls
- App answers 40
- 3 code graders 0
- 1 model grader 40
Each extra model grader adds another call per case, and each extra case adds a call for the answer and one for every judge. Multiplied by the number of changes a team makes in a week, the cost and the waiting time add up quickly, and a slow gate is one people start to skip. That shapes what goes into it:
- A small, purpose-built set, not every case you have: the core requests the app must handle, a case for each past bug so it cannot quietly return, and the known edge cases. When new failures turn up in use, representative examples are added to it.
- Code graders wherever they can decide. They are fast, cost nothing per run and give the same verdict every time, so they never add noise of their own.
- A model grader only where nothing else can judge, such as whether a claim is supported by a source text. A judge costs a call per verdict and its verdict can itself vary between runs, which widens the spread the tolerance has to cover.
The larger, more expensive evaluations still have their place. They simply run less often, on a schedule or after a significant change, rather than on every commit.
True or false?
Adding a model grader to a gate adds roughly one model call per case to every run.
True. Each verdict from a model grader is its own call, so a judge over 40 cases adds about 40 calls per run.
The best gate runs every case and every grader you have, so nothing can slip through.
False. A gate runs on every change, so its cost and time are paid again and again. A small, purpose-built set with code graders first keeps it fast enough to be used; the full evaluation runs less often.
When the baseline moves
The baseline describes the version you have accepted, so it moves only when you accept a new version. The step is deliberate: the new version passes the gate, someone reads the cases that changed and decides it is better or at least no worse, and the new scores are saved as the baseline in a commit of their own, with a message that says which version they belong to. Because the file is in version control, the move is visible in the history like any other change.
- New version one change
- Gate passes within tolerance
- Read changed cases decide to accept
- Save new baseline its own commit
The one move that is never allowed is lowering the baseline to make a failing gate pass. A gate fails precisely to say that a score fell further than noise explains. Overwriting the reference with the lower scores makes the failure disappear without changing the app, and the next version is then measured against the worse one, so a series of small give-ins can add up to a large regression nobody decided on.
Sometimes a team does choose a trade-off, for example accepting a small drop on one check in exchange for a large gain elsewhere. That is still an acceptance, made on purpose, even when the gate failed on that check. What separates it from the forbidden move is that someone read the failing cases and decided the drop is worth the gain, instead of moving the reference only so the gate turns green. The new baseline is committed with a message that names the drop and the reason for it, so the decision is on record rather than hidden inside a number. This rule is a working practice rather than a published standard, but it follows directly from what a baseline is for.
True or false?
If the gate fails after a change you are confident in, updating the baseline to the new scores is the quickest correct fix.
False. That hides the drop the gate found. Read the failing cases first; the baseline moves only when a version is accepted on purpose, and any accepted drop is named in the commit.
A baseline update belongs in its own visible commit that says which version the scores describe.
True. A separate commit makes every move of the reference easy to find and to question later.