Purple cover with the headline Testing LLMs without flaky builds and a grid of test results: upper rows with pink and white failures, lower rows all lime green passes

Testing LLM Output in CI Without Flaky Builds

by Evgenii Studitskikh
0 comments 19 views 5 minutes read

The first time you put an LLM call behind a feature, your test suite gets a new kind of problem. The same input can produce a different output on every run. A test that compares the response to a fixed string passes on Monday and fails on Tuesday, and nobody changed any code. After a few of those, people stop trusting CI, and a suite people don’t trust is worse than no suite at all.

Testing LLM features well is mostly about deciding what you’re actually testing, and putting each kind of check in the right place. Here’s the approach I use.

Split deterministic code from model behaviour

Most of an LLM feature isn’t the model. It’s the prompt template, the code that assembles context, the parser that reads the response, the validation, the retries, the fallbacks and the UI. All of that is ordinary, deterministic code, and it should be tested like ordinary code.

The trick is to keep the model call behind a thin interface you can replace in tests. Then most of your tests run against recorded or hand-written responses:

  • Prompt building: given this input and this context, the final prompt contains the right instructions, the right data and nothing it shouldn’t.
  • Parsing: given a well-formed response, a malformed one, an empty one and one that’s cut off halfway, the parser does the right thing every time.
  • Control flow: when the model returns an error, times out, or refuses, the feature retries, falls back or shows a sensible message.

These tests are fast, free and never flaky. In my experience they also catch most real bugs, because most real bugs live in the glue around the model, not in the model.

Record real responses, then replay them

Hand-written fake responses tend to be too clean. Real models add a sentence before the JSON, wrap it in a code fence, or use a slightly different key name. So I record real responses from the model once, save them as fixtures, and replay them in tests.

When a parser breaks in production on some odd response, that response goes straight into the fixtures. Over time the fixture set becomes a library of every strange thing the model has ever done, and the parser gets tested against all of it on every commit.

Constrain the output so there’s less to test

The easiest output to test is one that can’t vary much. Wherever a feature allows it, I ask for structured output: a JSON schema, a fixed set of labels, a number in a range. Many model APIs can enforce a schema directly. Then the test isn’t “does the text look right”, it’s “does it validate”, and validation is deterministic.

Free text still has its place, but every field you can turn into structure is one less thing to judge by eye.

Test properties, not exact strings

When you do need to check what the model actually says, compare properties instead of exact text:

  • The summary is under 300 characters.
  • The answer mentions the country it was asked about.
  • The output contains no email addresses or other personal data.
  • The classification is one of the allowed labels.
  • The generated SQL parses and only reads from allowed tables.

Property checks survive harmless wording changes and still catch the failures that matter.

Keep live-model evals out of the blocking path

Some questions can only be answered by calling the real model: did the new prompt make answers better or worse? Did switching models break a use case? These checks are valuable, but they are slow, cost money and are inherently a bit noisy. If they block every pull request, you’ll get exactly the flaky CI you were trying to avoid.

So I split them out:

  • On every commit: deterministic tests with fakes and recorded fixtures. Must pass, never flaky.
  • When prompts or models change, and on a schedule: an eval run against the live model over a fixed set of cases, scored by property checks and, where needed, by a second model acting as a judge.

The eval run reports a score, not a pass or fail on each case. A drop from 92% to 85% on a prompt change is a signal to look closer. A single case flipping between runs is noise.

Make evals repeatable enough to compare

Evals are only useful if you can compare one run to another. A few habits help:

  • Pin the model version instead of using an alias that can change under you.
  • Use the lowest temperature the feature can live with during evals.
  • Run each case a few times and look at the pass rate, not a single result.
  • Keep the eval cases in the repo, versioned next to the prompts they test.
  • Store each run’s results, so “is this worse?” has an answer.

Treat the prompt as code

Prompts change behaviour as much as code does, so they deserve the same process: they live in the repository, changes go through review, and a change to a prompt triggers the eval run automatically. A prompt edited directly in a dashboard, with no history and no tests, is a production change nobody reviewed.

The short version

  1. Test the deterministic glue with fakes and recorded responses. This is most of the value.
  2. Constrain outputs with schemas so more checks become simple validation.
  3. Check properties, not exact strings.
  4. Run live-model evals on prompt and model changes, not on every commit, and track scores over time.
  5. Version prompts and eval cases together, like code.

Done this way, CI stays green for the right reasons, and you still find out quickly when the model’s behaviour actually changes.

If you’re building LLM features and have found other ways to keep tests stable, I’d love to compare notes. Connect with me on LinkedIn.

You may also like