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.
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.
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.
| Task | Setting |
|---|---|
| Extraction, classification | Low temperature with strict schemas |
| Ideation, drafting | Higher 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.
- 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.