Topic 4 of 4 · 5 min read
Flaky LLM tests in CI
Goal: Choose a quarantine and re-run policy for a flaky LLM test that keeps real regressions visible.
A flaky LLM test needs a policy, not a shrug: measure its flake rate, move it to a non-blocking lane with an owner and a deadline, and keep a trend line so real regressions still show. Deleting it or re-running until green both hide information.
Check yourself · 1 of 3
An LLM test fails about 1 time in 20 on an unchanged build. What is the best first step?
Why it matters
Martin Fowler's note on non-deterministic tests makes the case: once people learn that red can mean nothing, they stop trusting red. LLM tests are flaky by nature, so without a policy a CI pipeline drifts into that state quickly. The pytest documentation makes a related point: a retry reduces how often a flaky test fails, but it does not fix the cause.
How to do it
- Measure before you judge. Run the suspect test 20 or more times on an unchanged build. A test failing 1 in 20 on its own is flaky. A test failing 15 in 20 is probably broken or testing something the model cannot do.
- Fix the test first. Most flakes come from checks that are too strict, too few runs per case, an unpinned model alias or a shared mutable resource. These are fixable. Use the tolerance and run-count methods from the earlier topics.
- Re-run with limits. Allow one automatic re-run for the whole job, not per test, and log every re-run. If a test needs a re-run to pass often, count it as flaky, not passing.
- Quarantine, don't delete. Move the test to a lane that runs on every build and reports its rate, but does not block merges. Give it an owner and a date. An unowned quarantine list becomes a graveyard.
- Keep a hard gate for what matters. Safety checks, schema validity and the key regression cases stay blocking, with enough runs to be stable.
- Alert on trends, not single failures. Track pass rate per suite over time and alert when it falls past a set band, such as two builds in a row under the agreed bar.
Before and after
Before: a failing test is retried "until green" in a shell loop, up to five times. A real regression that passes 3 times in 5 sails through.
After:
llm-evals:
blocking:
cases: [safety, json-schema, refund-policy]
runs_per_case: 10
min_pass_rate: 0.9
quarantine:
cases: [tone-friendly] # owner: dana, review by 2026-12-15
blocking: false
report: true
Blocking checks run enough times to be stable. The flaky one is visible and has a deadline.
What goes wrong
- Retry loops that count the best of five. They turn a 60 percent feature into a green one.
- Quarantine with no owner or date. The test is never fixed and the coverage quietly disappears.
- Alerting on every red. On-call learns to ignore it.
- Lowering the threshold until it passes. Change the bar only with new data, and write down why.
Examples
Retry until green
for i in 1 2 3 4 5; do pytest evals/ && break; done
A feature that passes 3 times in 5 now passes CI almost every time. The policy should be a single logged re-run, plus a report of how many tests needed it.
A quarantine entry
- case: tone-friendly
flake_rate: 0.15 # 3 of 20 runs on unchanged build
owner: dana
review_by: 2026-12-15
action: widen check, raise runs_per_case to 10
The entry says how flaky it is, who owns it, what to try and when to decide. After the date it is fixed or removed on purpose.
Key points
- Measure a test's flake rate on an unchanged build before deciding what to do with it.
- Fix the test (looser check, more runs, pinned model) before quarantining it.
- Retry the job at most once, log every retry, and count tests that need retries as flaky.
- Quarantine means non-blocking but still running and reporting, with an owner and a date.
- Alert on pass-rate trends across builds, and keep safety and schema checks as blocking gates.
Common mistakes
- Retrying each failing test up to five times.
- Deleting a flaky test with no replacement.
- Quarantining with no owner or review date.
- Alerting the whole team on every single failure.
Check yourself · 2 of 3
What does quarantining a flaky test mean in a healthy policy?
Check yourself · 3 of 3
Which test should stay a blocking gate even when other LLM tests are quarantined?
Practice items, not counted towards your mastery.
Start here
- Defeating Nondeterminism in LLM Inference (Thinking Machines Lab) (opens in a new tab)ArticleFree
Shows why temperature 0 still gives different outputs: batch size changes the order of floating-point math.
- Adding Error Bars to Evals (Miller, 2024) (opens in a new tab)PaperFree
Standard errors and repeated sampling for eval scores, so a score change is read against its noise.
- Evaluation best practices (OpenAI) (opens in a new tab)DocsFree
A vendor's guidance on evaluating variable outputs with graders and enough samples.
More for this topic
- Non-Deterministic Tests (Martin Fowler) (opens in a new tab)ArticleFree
The classic case for quarantining flaky tests so they do not hide real failures.
- Flaky tests (pytest docs) (opens in a new tab)DocsFree
Causes of flakiness and why re-running a test is a mitigation, not a fix.