Skip to content
mentoor

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?

Choose one answer

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

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. Keep a hard gate for what matters. Safety checks, schema validity and the key regression cases stay blocking, with enough runs to be stable.
  6. 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?

Choose one answer

Check yourself · 3 of 3

Which test should stay a blocking gate even when other LLM tests are quarantined?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic