
Ask an AI assistant for five product names, then ask again. The second list may differ even when the request is unchanged. One reason is sampling: the system can choose among several possible continuations rather than always taking the highest-scoring one.
Temperature, top-k and top-p shape those choices. They are useful controls, but none is a truthfulness dial. Understanding that distinction helps you experiment without mistaking a confident-sounding answer for a verified one.
Prediction and selection are different steps
Our next-token explainer describes how a language model produces scores for possible continuations. Here, the focus is what happens after those scores exist.
A decoder selects a token, appends it to the context, and repeats. Greedy selection takes the highest-scoring option at each step. Sampling instead draws from a probability distribution. A less likely option can therefore appear without being the model’s first choice.
Hugging Face’s generation reference separates sampling controls from other settings, including beam search and output length. That separation matters: changing a temperature setting is not the same as changing the model’s learned weights or giving it more information.
Temperature changes the balance between options
For a positive temperature, the usual calculation divides each raw score, called a logit, by the temperature before converting scores to probabilities. Below 1, the distribution becomes more concentrated around high-scoring options. Above 1, it becomes flatter. A temperature of 1 leaves this scaling unchanged. See the Transformers temperature documentation.
“Flatter” does not mean every token becomes equally likely at an ordinary setting. It means the gap between more and less likely options becomes smaller. Nor does temperature change the order of unequal scores by itself: it changes their relative probabilities.
A three-option example
Consider a deliberately simplified, invented vocabulary of three possible next tokens: A, B and C. At temperature 1, suppose their probabilities are 60%, 30% and 10%. These are teaching numbers, not measurements from a particular model.
We can calculate another temperature from these starting probabilities by raising each to the power 1/T and dividing by the sum. This is equivalent to scaling the underlying logits when no other filters intervene; the Transformers implementation shows the score-scaling operation.
- T = 0.5: A becomes about 78.26%, B 19.57%, and C 2.17%.
- T = 1: A remains 60%, B 30%, and C 10%.
- T = 2: A becomes about 47.27%, B 33.43%, and C 19.30%.
At 0.5, the unnormalized numbers are simply 0.6 squared, 0.3 squared and 0.1 squared: 0.36, 0.09 and 0.01. Their sum is 0.46. Dividing 0.36 by 0.46 gives A’s 78.26% share.
A still leads in every row. But sampling can select B or C, including at the lower temperature. One draw cannot demonstrate a distribution: a 10% option can occur on the first attempt, and a few repeated attempts need not match the percentages neatly.

Top-k and top-p narrow the candidate pool
Temperature reshapes probabilities. Top-k retains a fixed number of leading candidates. Top-p, also called nucleus sampling, retains a small leading set whose cumulative probability reaches the chosen threshold. The original nucleus-sampling research explored this adaptive shortlist as a way to improve open-ended generation while excluding the distribution’s less reliable tail.
Using our original 60/30/10 distribution, top-k of 2 keeps A and B. After normalization, they have probabilities of about 66.67% and 33.33%. C cannot be selected in that step.
Top-p of 0.8 also keeps A and B here: A alone totals 0.6, while adding B reaches 0.9 and crosses the threshold. On another step with probabilities 90/7/3, a threshold of 0.8 could keep only A. Unlike top-k, its shortlist size can change.
When controls are combined, processing order and implementation details matter. Do not calculate top-p from the original probabilities if the implementation applies temperature first. Check the documentation for the exact model and service.
Why “temperature zero” is not a reliability guarantee
Some interfaces use zero as shorthand for greedy selection; others require a strictly positive temperature and expose a separate sampling switch. In Transformers, temperature affects sampling only when sampling is enabled. A zero setting is therefore not a portable instruction across every library.
Repeatability also involves the software and hardware stack. PyTorch’s reproducibility guidance warns that identical results are not guaranteed across releases, platforms, or CPU and GPU execution. Record the model version and settings when comparing runs rather than assuming one number freezes the entire system.
Most importantly, choosing the leading token consistently does not prove the resulting claim. If a fictional assistant has no reliable evidence for a delivery date, lowering its temperature does not supply that evidence. It may merely repeat the same unsupported date more often.
A practical experiment
Start with a small set of representative requests and the model’s documented defaults. Change one supported control at a time. For a naming exercise, compare useful variety and instruction-following. For extracting an invoice total, compare the output with the actual document and reject unsupported values.
Keep the prompt, source material and evaluation rules fixed. Run each setting several times, retain failures as well as attractive outputs, and record unsupported or ignored parameters. A setting that produces one excellent answer has not yet demonstrated reliable performance.
Use sampling controls to manage variation. Use evidence, validation and task-specific tests to judge correctness. Those are separate jobs, and a dependable application needs both.