Why an LLM is not tested like regular code
An LLM is not tested by comparing exact text: it is non-deterministic and free text has no single correct answer. Test the properties of an answer (format, the right tool called, no forbidden words) and measure the success rate on a set of examples. Safety rules require 100 %. The logic around the model (parsing, routing, errors) is tested conventionally with a mocked model.
AI for QA · Testing AI
A test with toBe fails although the bot answered correctly, in other words. What should you check now?
For the request "Cancel order 1042" the bot answered "Your order 1042 has been successfully cancelled", while the test expected "Order 1042 has been cancelled." and failed. The team fixes the expected string, and the next day the bot words it differently again. There is a flip side too: a wrong answer that happens to match the template passes. The usual logic "input → expected string" does not work with an LLM, and this lesson explains why and what to put in its place.
How it works and why. A classic test fixes an input and compares with an expected value. With an LLM this breaks for three reasons.
- Non-determinism. The same input gives different but equally correct answers. Even zero temperature does not guarantee identical text (see the lesson on generation parameters). String equality does not work;
- The oracle problem. For free text there is no single "correct" answer, so it is unclear what to compare with;
- Implicit dependencies. Behaviour is changed by the model version, the prompt, the parameters, RAG data and tool results. None of these changes looks like broken code, so a regression is not visible in a diff.
Two shifts follow. First: check the properties of an answer, not the exact text. Second: measure the success rate over a set of examples, not "passed or not" on one run.
Exact text or properties
Exact text
toBe with one string. It fails on a correct answer in other words and passes on a wrong one that happened to match.
Properties on a sample
We check that the tool was called, the phrase promises nothing extra and the format is valid. We measure the success rate over repeats.
Example: from a brittle test to a sampled one. The initial version:
const res = await bot.reply('Cancel order 1042');
expect(res.text).toBe('Order 1042 has been cancelled.');
It fails on a synonym and does not catch a wrong answer that matched. The fixed test checks what matters to the business: whether the cancellation tool was called with the right number and whether the bot promised nothing extra. It runs the case several times:
const RUNS = 20;
let passed = 0;
for (let i = 0; i < RUNS; i++) {
const res = await bot.reply('Cancel order 1042');
const calledTool = res.toolCalls.some(
(c) => c.name === 'cancel_order' && c.args.orderId === 1042,
);
const noFalsePromise = !/we will refund|refunded/i.test(res.text);
if (calledTool && noFalsePromise) passed++;
}
// the threshold comes from a baseline, not by eye
expect(passed / RUNS).toBeGreaterThanOrEqual(0.9);
There are three kinds of checks. Deterministic where possible: valid JSON by a schema, required fields, the right tool called with the right arguments, the state in the database changed, no forbidden words, length within bounds. Semantic for free text: similarity by embeddings to a reference, fact checking against a reference text, LLM-as-judge (see the lesson “Eval set, metrics and LLM-as-judge”). Statistical: the success rate over repeats or over a set, a comparison of the average score between versions. A threshold, like 0.9 in the example, comes from a baseline and from risk. Over twenty repeats the noise is noticeable, so a difference of a few percent needs more runs. Safety rules (do not disclose foreign data, do not perform a forbidden action) require 100%, not "most of the time".
A test pyramid for LLM features
The pyramid stays. The most numerous are fast deterministic tests with a mocked model: parsing the answer, routing by category, handling errors and limits, building the context (see the lesson on the context window). Fewer are eval sets with a real model. Fewer still are end-to-end scenarios. The model mock returns answers known in advance, including bad ones: a cut-off, a refusal, broken JSON, an empty answer, because that is what the surrounding code breaks on.
Limits and alternatives. Properties do not replace a person: style, tone and relevance are hard to reduce to a check, and here a judge with a rubric and a sampled manual review help. A test over repeats is slow and paid, so it does not run on every commit (see the lesson on evals in CI). There is also a cautious way for critical answers: do not rely on generation, take the text from a template and use the model only to pick the template or extract fields.
Common mistakes.
- An expected string in
toBe. Consequence: it fails on correct answers and lets wrong matches through. - "Treating" an unstable test by rerunning until green. Consequence: the real failure rate is hidden.
- One run per case. Consequence: a regression cannot be told from noise.
- Mocking the model everywhere. Consequence: nobody checks the behaviour of the real model.
- Taking a threshold by eye. Consequence: the threshold is either noisy or catches nothing.
Check yourself
A test requires the answer to contain the word "cancelled". The bot answered "The order was not cancelled because it has already shipped". What is wrong with the test?
Answer
The test checks a word match, not behaviour. The word "cancelled" is present in the negation, and the right check is whether the cancellation tool was called and whether the order state changed. Text checks complement checking the action and do not replace it.
A case passes in 18 of 20 runs. Is that acceptable?
Answer
It depends on the risk. For answer style 90% may be fine, for a safety rule it is 2 violations out of twenty, which is unacceptable. The threshold is set per kind of check and baseline, and 20 runs give only a rough estimate.
Which part of the logic around an LLM can be tested without the model?
Answer
Everything that does not depend on the answer content: parsing and validating the output, routing by category, building and trimming the context, handling errors, timeouts and limits. The model mock returns answers known in advance, including broken ones.