All posts

Temperature in LLMs

/AI/4 min read

Temperature divides the logits before the softmax. On the same four candidates, T = 0.2 gives the wrong answer a 0.0% chance and T = 1.5 gives it 5.2% — which across 100 users means nobody sees it, or 99.5% of the time somebody does.

At every step a language model produces a score — a logit — for every token in its vocabulary. A softmax turns the scores into probabilities, and one token is drawn from them.

Temperature is a single number applied in between. The logits are divided by it before the softmax:

p(token) = softmax(logit / T)

That is all it does. Everything people describe as "creativity" or "randomness" is the consequence of that one division.

What the division does

Dividing by a number below 1 pushes the scores apart. Dividing by a number above 1 pulls them together. Since the softmax is exponential, small changes in the gaps become large changes in the probabilities.

Take the prompt "The capital of France is" and suppose the four top candidates score:

tokenlogit
Paris4.0
the3.0
a2.0
Lyon0.5

"the" and "a" are harmless — they lead to "the city of Paris" or "a city called Paris". "Lyon" is wrong.

temperatureParistheaLyonentropy
0.299.3%0.7%0.0%0.0%0.06 bits
0.776.7%18.4%4.4%0.5%0.98 bits
1.065.2%24.0%8.8%2.0%1.32 bits
1.553.4%27.4%14.1%5.2%1.61 bits

At 1.0 the model's own distribution is used unchanged. At 0.2 it is almost deterministic: the top token takes 99.3%. At 1.5 the distribution is flattened enough that the wrong answer gets one draw in twenty.

As T approaches 0 the top token's share approaches 100% and sampling becomes greedy decoding — always take the highest logit. That is why "temperature 0" is the usual setting for reproducible output, although in practice tiny numerical differences on the hardware can still occasionally flip a near-tie.

SAME LOGITS, FOUR TEMPERATURES T = 0.2T = 0.7T = 1.0T = 1.5 99%53% 5.2% bars: Paris · the · a · Lyon (outlined — the wrong answer) dividing by T scales every gap between logits, and softmax exponentiates the gaps
Raising the temperature does not add new options; it gives the existing weak ones more of the share.

The number that matters: at scale

5.2% sounds small. But a deployed model answers the same kind of question many times. The chance that "Lyon" appears at least once across 100 requests is 1 − (1 − p)^100:

temperatureP(Lyon) per requestat least once in 100 requests
0.2~0.0%0.0%
0.70.5%40.4%
1.02.0%86.3%
1.55.2%99.5%

At the model's default temperature, a hundred users asking a factual question almost certainly produces at least one wrong answer — not because the model does not know, but because the sampler was told to sometimes pick a token the model considered unlikely.

And this is one token. A long answer makes hundreds of such draws. A low-probability token early on changes the context for every token after it, so the effect compounds over the length of the reply.

Choosing a temperature

Facts, extraction, classification, code: low. 0 to 0.3. There is one right answer, the model's top choice is usually it, and variation is a defect.

Writing, brainstorming, dialogue: moderate. 0.7 to 1.0. Here the top token is often bland — the most predictable word — and sampling from the rest produces more natural, varied text. Ten drafts that are all identical are useless.

Above 1.0: rarely. The tail of the distribution is mostly tokens the model rated low for good reason. Text degrades into non sequiturs quickly.

Sampling several answers to pick from: a higher temperature on purpose, because the point is to get answers that differ. Self-consistency — sampling many reasoning paths and taking the majority answer — relies on this.

Temperature is usually combined with top-k or top-p, which cut off the long tail entirely before sampling. That pairing makes moderate temperatures safer: the distribution is flattened only among tokens that were plausible to begin with.

What to take away

Temperature divides the logits before the softmax. Below 1 it sharpens the distribution toward the top token; above 1 it flattens it toward the tail; at 0 it becomes greedy decoding.

The effect is easy to underestimate per token and hard to miss at scale: on the same four candidates, raising T from 0.2 to 1.5 takes the chance that a hundred users see the wrong answer at least once from zero to 99.5%. Match the temperature to whether the task has one right answer or many.