By Sagi Shaier · 5 November 2026 · 6 min read
LLM temperature explained
What the temperature setting does to a model's next-token probabilities, worked through with real numbers, and how to add it to a PyTorch sampling loop.
Temperature is a number that controls how random a language model's output is. A low temperature makes the model stick to its most likely next tokens, so the text is steadier and repeats more from run to run. A high temperature spreads the chances out, so less likely tokens show up more often and the text varies more. It changes the probabilities the model samples from and leaves the trained model itself untouched.
Where temperature fits
A language model writes text one token at a time, where a token is a whole word or a piece of a word. At each step it gives every token in its vocabulary a raw score called a logit. A higher logit means the model rates that token as a more likely next token.
Softmax turns the logits into probabilities that are positive and add up to 1. A decoding rule then picks one token from them. Greedy decoding takes the top token, and sampling draws a token at random with each token's chance equal to its probability. Filters such as top-k, top-p and min-p first remove unlikely tokens and then sample from the rest. The decoding strategies post covers those rules.
Those filters decide which tokens are allowed into the draw. Temperature does something else: it changes the probabilities themselves before any rule runs.
The formula
Temperature divides the logits by a number $T$ before softmax:
$$p_i = \frac{\exp(z_i / T)}{\sum_j \exp(z_j / T)}$$
Here $z_i$ is the logit of token $i$, $p_i$ is its probability after temperature, and the sum in the denominator runs over every token $j$ in the vocabulary. With $T = 1$ the logits are unchanged, so this is plain softmax.
Take two tokens with logits 2.0 and 1.0. At $T = 1$ softmax gives 0.73 and 0.27. At $T = 0.5$ the logits become 4.0 and 2.0, because dividing by 0.5 doubles them. Their gap grows from 1 to 2, and softmax gives 0.88 and 0.12. At $T = 2$ the logits become 1.0 and 0.5, the gap shrinks to 0.5, and softmax gives 0.62 and 0.38.
Softmax only depends on the gaps between logits, so dividing by $T$ scales every gap by the same factor. A value of $T$ below 1 widens the gaps and pushes more probability onto the top tokens, which is called sharpening the distribution. A value above 1 narrows the gaps and gives lower-ranked tokens more room, which is called flattening it.
A worked example with six tokens
Take six tokens with the logits the 2.0, cat 1.0, sat 0.5, on 0.1, mat -0.4 and zebra -1.2. This script prints their probabilities at three temperatures:
1import torch
2import torch.nn.functional as F
3
4vocab = ["the", "cat", "sat", "on", "mat", "zebra"]
5logits = torch.tensor([2.0, 1.0, 0.5, 0.1, -0.4, -1.2])
6
7for T in [0.5, 1.0, 1.5]:
8 probs = F.softmax(logits / T, dim=-1)
9 print(f"T={T}:", " ".join(f"{w}={p:.2f}" for w, p in zip(vocab, probs.tolist())))1T=0.5: the=0.82 cat=0.11 sat=0.04 on=0.02 mat=0.01 zebra=0.00
2T=1.0: the=0.53 cat=0.20 sat=0.12 on=0.08 mat=0.05 zebra=0.02
3T=1.5: the=0.40 cat=0.21 sat=0.15 on=0.11 mat=0.08 zebra=0.05At $T = 0.5$, "the" holds 82% of the probability and sampling comes close to greedy decoding. At $T = 1.5$ it holds 40%, and "zebra" goes from about 0.1% to 5%. The ranking stays the same at every temperature, with "the" first and "zebra" last.

The ranking stays the same because dividing by a positive $T$ keeps the logits in the same order, and softmax keeps the order of whatever it receives. So temperature changes how often each token gets drawn, and it never changes which token is on top. Greedy decoding takes the top token, so temperature has no effect on greedy output.

Temperature in a sampling loop
In code, temperature is one division before softmax:
1def sample_with_temperature(logits, temperature):
2 scaled = logits / temperature
3 probs = F.softmax(scaled, dim=-1)
4 return torch.multinomial(probs, num_samples=1)torch.multinomial draws one token id, each with a chance equal to its probability. To see the effect, this script samples 10,000 tokens at each temperature from the same six logits and counts how often the pick is not "the", and how often it is "zebra":
1import torch
2import torch.nn.functional as F
3
4def sample_with_temperature(logits, temperature):
5 scaled = logits / temperature
6 probs = F.softmax(scaled, dim=-1)
7 return torch.multinomial(probs, num_samples=1)
8
9vocab = ["the", "cat", "sat", "on", "mat", "zebra"]
10logits = torch.tensor([2.0, 1.0, 0.5, 0.1, -0.4, -1.2])
11
12torch.manual_seed(0)
13for T in [0.5, 1.0, 1.5]:
14 draws = [sample_with_temperature(logits, T).item() for _ in range(10_000)]
15 not_the = sum(d != 0 for d in draws) / len(draws)
16 zebra = sum(d == 5 for d in draws)
17 print(f"T={T}: not 'the' {not_the:.0%}, 'zebra' {zebra} times")1T=0.5: not 'the' 18%, 'zebra' 11 times
2T=1.0: not 'the' 47%, 'zebra' 199 times
3T=1.5: not 'the' 60%, 'zebra' 452 timesAt $T = 0.5$ the sampler picks something other than "the" in 18% of draws, and at $T = 1.5$ it does so in 60%. "Zebra", the token with the lowest logit, comes up 11 times out of 10,000 at the low temperature and 452 times at the high one. That rise in rare picks is where a high temperature gets both its variety and its risk of text that drifts off topic.
If the output looks too random, lower the temperature first. If every run gives nearly the same text, raise it a little.
Temperature with top-k, top-p and min-p
In a decoding pipeline, temperature usually comes first: divide the logits by $T$, then apply top-k, top-p or min-p, then sample.
The order changes the result for top-p and min-p, because both decide which tokens to keep from the probabilities. With the same six logits, top-p with $p = 0.9$ keeps 2 tokens at $T = 0.5$, 4 tokens at $T = 1.0$ and 5 tokens at $T = 1.5$. A lower temperature puts more probability on the top tokens, so fewer of them are needed to reach 0.9.
Top-k keeps the $k$ highest logits, and temperature does not change their order, so top-k keeps the same tokens at any temperature. Temperature still changes how often each kept token is drawn.
When to use a low or high temperature
- Low temperature (below 1) fits answers that need to stay on track, such as factual questions and short completions. The output is steadier and closer to greedy decoding.
- Temperature 1 samples from the model's own probabilities unchanged.
- High temperature (above 1) fits brainstorming and creative text, where varied output is the goal. The cost is more rare tokens and more text that drifts.
When the same output on every run is the goal, use greedy decoding, since sampling at any temperature can still give different text from run to run.
Common mistakes
- Setting $T = 0$ to get greedy output. Dividing by 0 turns the logits into
infand-inf, softmax returnsnan, andtorch.multinomialraises aRuntimeError. Code that wants greedy output callsargmaxon the logits instead. - Dividing the probabilities instead of the logits. Temperature goes inside softmax. Dividing the probabilities by $T$ after softmax only rescales them, and renormalizing brings back the same distribution.
- Expecting temperature to change greedy output. The top token stays on top at any positive temperature, so greedy decoding gives the same result.
- Applying temperature after top-p or min-p. The filter then keeps the pool it chose at $T = 1$, and the temperature only reshapes the probabilities inside that pool.
Related concepts
Decoding strategies covers the rules that run after temperature: greedy decoding, random sampling, top-k, top-p and min-p. Beam search is a different way to decode that keeps several partial sequences at once and compares their total scores. How language models are trained covers where the logits come from in the first place.
QuiddityML teaches temperature as its own concept in the language models unit of its NLP track. The exercises include matching the temperature code to its softmax equation, spotting the bug in a broken sample_with_temperature, and writing it from scratch (QuiddityML).