An LLM samples each token from a probability distribution, so the same prompt can produce different text on different runs. Taming that variance means shrinking it where correctness depends on it and keeping it where creativity helps.

Determinism vs StochasticityDiagram with these labels, in reading order: User Prompt; Temperature Setting; (0 or >0); Sampling from; Probability Distribution; Output Variance; Constrained Output; (Structured/Enums).User PromptTemperature Setting(0 or >0)Sampling fromProbability DistributionOutput VarianceConstrained Output(Structured/Enums)
Determinism vs Stochasticity in LLMs

What temperature 0 does not pin down

Setting temperature=0 aims to pick the most likely token at each step. Alone it is not enough. You also pin top_p, seed (where the API offers one) and the exact model version, such as model-2025-06-01. Even then, floating-point non-associativity in GPU operations, request batching and mixture-of-experts (MoE) routing can make outputs differ, so bit-exact repeatability is not guaranteed.

Providers do not promise reproducibility, and silent snapshot rotations can change behavior without notice. Pinning a version is the control you have over that.

Intrinsic flakiness Hardware and model variations mean temperature 0 cannot guarantee exact repeatability.

Constraining the shape of the output

Since wording will vary, constrain the surface instead. A JSON schema or grammar fixes the keys and types of an API response. An enum limits a field to a set of known values. The text inside can still vary, but the structure that downstream code depends on stays stable.

  • Pin the model version.
  • Use schemas or grammars for structure.
  • Use enums for categorical fields.
  • Test the structure and meaning of outputs, not exact strings.

Flaky tests as a mental model

Think of a test that passes 95% of the time because of how the system works, with no bug to fix. You design around it. LLM variance is the same kind of property, but it is inherent to how these models work, so applications need to be built to tolerate it.

Temperature by task type

Match the setting to what the task needs. Where a downstream system parses the answer, favour a predictable format. Where a person reads the answer and variety helps, allow more randomness.

TaskSetting
Extraction, classificationLow temperature with strict schemas
Ideation, draftingHigher temperature for variety

Do not treat exact-match repeatability as a guarantee of correctness. A repeated wrong answer is still wrong.

Example scenario: the same prompt gives different answers

Suppose across repeated runs of one prompt, 30% of responses are off-topic and the rest are relevant but vary in tone and detail. The likely cause is sampling from a distribution, made worse when the prompt is vague or open to several readings.

Determinism vs StochasticityDiagram with these labels, in reading order: High Output Variance; Check Sampling Temperature; Check Top-k Value; Adjust Temperature Down; Adjust Top-k Up; Evaluate Output Quality.High Output VarianceCheck Sampling TemperatureCheck Top-k ValueAdjust Temperature DownAdjust Top-k UpEvaluate Output Quality
Debugging output variability
  • Check the sampling temperature, for example whether it is set to 0.7.
  • Log outputs for 100 runs of identical prompts and measure the variance.
  • Score relevance with a classifier to count off-topic responses.

Lowering the temperature, for example to 0.3, reduces variance at some cost to creativity. Track the off-topic percentage and the tone variance to confirm the model still suits the use case.

Back to the knowledge baseRequest an assessment