QA Interview Hub · Knowledge base · Quizzes · Українська

QA Fundamentals › AI in testing

Testing a non-deterministic LLM feature

A non-deterministic LLM feature is tested by the properties of the answer and by the success share over a series of runs, not by an exact string. Answers differ because the model samples each token from a probability distribution; temperature reduces the spread but does not guarantee identical text. The threshold depends on risk: 100% for security, lower for style. A small sample is not proof, and versions are pinned.

QA Fundamentals · AI in testing

A bot answered the same question differently twice. Is the test red or green?

In a classic test one input gives one output, and assertEquals can check it. In an LLM feature the same request returns different texts, and both may be correct. The general principles of testing such features are covered in the lesson on testing LLM features; here is the mechanism of non-determinism and how to turn the "passed or not" verdict into a statistical one.

How it works and why. At each step the model gets a probability distribution over the next token and samples from it. The temperature parameter controls how sharp that distribution is: a low value makes the choice more conservative, a high one widens it. That is why the text differs on the same request. Lowering the temperature reduces the spread but does not guarantee the same text: it depends on the provider and the configuration, so do not build a test on "at temperature 0 it is always the same". Besides sampling, the answer is affected by provider model updates, the prompt version and the data from RAG. The conclusion for a test: check the properties that must hold for any of the acceptable texts, and measure the share of answers that have them.

A test result as a share and not "yes or no"

10 runs of one request✓✓✓✓✓✓✗✓✓✓9 of 10 gave a correct answer: 90%Threshold: at least 90%The test passes
Figure 1. Each run checks properties of the answer and the verdict is based on the share of successes.

Example: from an exact string to a statistical check. A test that breaks over the word "approximately":

const answer = await ask('What is the transfer fee?');
expect(answer).toBe('The transfer fee is 1%.');

It turns red as soon as the model writes "1% of the amount" or adds a greeting, although the answer is correct. A property check across several runs:

const N = 10;
const runs = await Promise.all(
  Array.from({ length: N }, () => ask(question)),
);
const good = runs.filter(
  (a) => /1\s?%/.test(a) && !/free of charge/i.test(a) && a.length < 300,
);
expect(good.length / N).toBeGreaterThanOrEqual(0.9);

There are three properties here: the required fee is mentioned, a forbidden promise is absent, and the length is limited. The test computes the share of successful answers and compares it with a threshold the team agreed. A failure becomes not "the test is red" but "the share dropped from 97% to 70% after the prompt change", which is a direct regression signal.

Limits and alternatives. Ten runs is a small sample: 9 of 10 and 7 of 10 are hard to tell from noise, so important scenarios get more runs, and the threshold is compared with a trend over time and not with a single run. The threshold depends on risk: for the tone of an answer 90% may be acceptable, for "does not reveal another user’s data" only 100% is allowed, and one failure is a defect. Property checks also do not see meaning: an answer may contain "1%" and be wrong elsewhere. So alongside such checks keep a sampled human review. More expensive but more reliable: pin the model and prompt version in the test environment to tell a change in your code from changes outside it.

Common mistakes.

  • Comparing an answer with an exact string: the test turns red on correct but differently worded answers and the team stops trusting it.
  • Relying on temperature 0 as a guarantee: not every provider promises identical text.
  • One run and a verdict: no failure in a single run says nothing about the share.
  • The same threshold for everything: risky properties must demand close to 100%, while stylistic ones can tolerate less.
  • Forgetting off-norm input: an empty request, a long text, another language, an off-topic question.

Check yourself

A test with a 90% threshold over 10 runs passed 9 of 10. On the next run after a prompt change it passed 8 of 10. Is that proof of a regression?

Answer

No, only a signal. A difference of one answer over ten runs can be noise. Increase the number of runs, watch the trend over several runs and look at the failed answers themselves: if they share a cause (say, the model stopped mentioning a condition), the regression is confirmed.

For the property "the bot does not show another user’s data" the team set a 95% threshold. What is wrong here?

Answer

For a security property only 100% is acceptable: every leak is a separate defect and not a 5% margin. A percentage threshold suits the quality of wording but not prohibitions with consequences.

In an interview. Junior: LLM answers differ a little every time, so I do not compare exact text but check properties: required facts, format, absence of forbidden topics. I run each request several times. Middle/Senior: Non-determinism comes from token sampling, and temperature only reduces the spread, it does not remove it. So the test reports a share of successes with a threshold that depends on risk: 100% for safety, lower for style. I pin the model and prompt versions, do not treat a small sample as proof and look at the trend and at the failed answers themselves. I add sampled human review to automatic checks.

More articles in this chapter