Same prompt, different answer: sampling, temperature, and when to turn the dice off
I build Settl, an expense-splitting app, solo with Claude, and its receipt parser used to fail on about one upload in ten: same photo, same prompt, a different JSON key. Nothing was broken. I had left the model on creative. Here is the mechanism, and what to change.
Why one prompt has many answers
The model never produces an answer. For each position it scores every token in its vocabulary, tens of thousands of numbers called logits. A softmax turns those into a probability distribution, a sampler draws one token, and it repeats for the next position.
So at one position the distribution might be total at 70 percent, amount at 20, grandTotal
at 10. Run it once, you get total. Run it a hundred times, roughly ten come back grandTotal.
That is the model doing what it was asked, honestly, a hundred times.
What temperature actually does
Temperature divides the logits by T before the softmax runs.
- Below 1, the gaps between scores stretch. The leader pulls ahead, the tail collapses. 70/20/10 becomes closer to 95/4/1.
- Above 1, the gaps compress, the distribution flattens, and long shots start getting picked.
- At exactly 0, dividing is undefined, so implementations take a different path: skip sampling, take the highest-scoring token. That is greedy decoding.
top_p is the other knob. It keeps only the smallest set of tokens whose probabilities sum to p
and samples from those. Move one of the two, never both.
Why zero is not a guarantee
Temperature 0 is near-deterministic, not deterministic. Floating point addition is not associative and your request is batched with other traffic, so batch composition changes the order of GPU reductions and the low bits of a logit move with it. Mixture-of-experts routing can be batch-dependent too: which expert handles a token can depend on the rest of the batch.
None of that matters until two tokens are nearly tied. Then a low-bit difference flips the argmax, and one flipped token changes every token after it. Treat temperature 0 as "almost always the same", never as a contract.
Worth knowing: the newest frontier models dropped the sampling parameters entirely, and sending
temperature returns a 400. Chat interfaces never exposed the knob. So often you cannot turn it
down, and everything below applies in the prompt and the schema instead.
The signs your problem is sampling
- Retrying the identical request sometimes works. That single fact rules out most real bugs.
- The failure rate is a stable percentage of traffic, not a specific input.
- The output is right in substance and wrong in shape: renamed key, different casing, a stray sentence before the JSON.
- You cannot reproduce it locally: you tried three times and the dice went your way.
Turn it down for anything you parse
Extraction, classification, routing, anything with a schema, anything two runs must agree on:
resp = client.messages.create(
model="claude-haiku-4-5",
max_tokens=1024,
temperature=0,
messages=[{"role": "user", "content": prompt}],
)
Then stop leaning on temperature and pin the output shape, so the wrong tokens are not merely unlikely, they are unavailable:
Extract the invoice total. Reply with JSON only, matching exactly this shape,
no prose, no code fence:
{"total_cents": <integer>, "currency": <3-letter ISO code>, "confidence": <0-1>}
If the total is not legible, return total_cents as null. Do not guess a number.
Do not add fields. Do not rename fields.
Naming the keys buys your parser more than temperature 0 does. It removes the ambiguity rather than picking a favourite among ambiguous options.
Turn it up for anything you pick from
Names, taglines, test fixtures, brainstorm lists: anything where you generate many and choose one. At temperature 0, asking for ten ideas gives you one idea and nine restatements of it, because every generation walks the same top-probability path.
Give me 15 names for a feature that settles a group's debts in one transfer.
Make them genuinely different: vary the register (plain, playful, technical),
the length (one word up to three), and the metaphor. If two share a root word,
replace the second. Number them. No explanations.
What temperature will not fix
- Hallucination. Temperature 0 invents identically every time. It makes wrong answers repeatable, not correct.
- Validation. Consistent is not valid. Parse and check the schema regardless.
- A brittle prompt. If it only works at temperature 0, it is one token from breaking. Run it at 1.0 five times on purpose to find where it is fragile.
Run this on your codebase
Find every place this codebase calls an LLM. Do not suggest fixes yet.
For each call site give me one row in a markdown table:
1. File and line.
2. What the output is used for, in four words or fewer.
3. Whether the result is machine-read (JSON.parse, a regex, a validator) or
shown to a human.
4. The temperature or top_p actually sent, or "unset" if the request omits it.
5. Whether the prompt names the exact output shape, or only describes it.
Then flag two categories:
- PARSED BUT LOOSE: machine-read output where temperature is unset or above 0,
or the prompt does not name the exact keys.
- CREATIVE BUT PINNED: a human-facing list or a set of options to choose from,
generated at temperature 0.
For the first flagged call site only, write the replacement prompt with the
output shape pinned, and the one test that proves the variance is gone: the
same input 20 times, asserting identical parsed output.