What are temperature, top-k, and top-p in LLM sampling?
Sampling is how a language model turns its scores for the next token into one chosen token. At each step the model outputs a logit for every token in its vocabulary, softmax turns the logits into probabilities, and a decoding rule picks one. Temperature reshapes those probabilities, while top-k and top-p remove unlikely tokens before the draw.
Every chat assistant, code completion tool, and text generator exposes some of these settings, usually as temperature, top_k, and top_p in an API or a config file. Low temperature suits code, extraction, and factual answers where one output is correct. Higher temperature with a top-p filter suits chat and creative writing, where varied wording helps and repetition hurts. The same model can feel precise or inventive depending only on these few numbers.
Key ideas
- A language model outputs a probability for every vocabulary token, and a decoding rule picks one of them.
- Greedy decoding always takes the top token, so it is deterministic and can fall into repetition loops.
- Temperature divides the logits by T: below 1 it sharpens the distribution, above 1 it flattens it.
- Top-k keeps a fixed number of tokens, which is too many for confident predictions and too few for open ones.
- Top-p keeps the smallest set of tokens that covers probability p, so its size adapts to each step.
- Libraries apply these steps in different orders, and a fixed random seed makes sampled output reproducible.
Short takes on this topic
Common questions
Does temperature 0 make an LLM fully deterministic?
In principle temperature 0 means greedy decoding, so the same prompt gives the same output. In practice, hosted models can still vary slightly because of floating-point differences in batched GPU computation. Two tokens with nearly equal logits can swap order between runs.
Should I change temperature and top-p at the same time?
Both control randomness, so changing both makes the effect hard to predict. A common approach is to set top-p once, for example to 0.9, and tune temperature. Some API providers recommend adjusting only one of the two.
What temperature should I use for code generation?
Low values, from 0 to about 0.3, work well when one correct answer is wanted. When generating many candidate programs and testing each, a higher temperature such as 0.8 gives more diverse attempts, which raises the chance that at least one passes.
Why do LLMs repeat themselves?
Once a phrase appears in the context, the model often assigns a high probability to continuing it, and greedy or low-temperature decoding follows that path every time. Sampling with top-p reduces this. Repetition penalties and frequency penalties lower the logits of tokens that already appeared.
Related explainers
- Softmax, Logits & Temperature — Turning raw scores into probabilities
- Language Modeling — Predict the next token — the objective that powers GPT, BERT, and modern NLP
- Decoder-Only Transformers — The architecture behind GPT and modern LLMs
- Entropy & Cross-Entropy — Measuring surprise, uncertainty, and prediction quality
- KV Cache — Why LLM generation caches keys and values, and what it costs
- Quantization — Shrinking model weights to 8 and 4 bits
References
- Holtzman et al. (2019), "The Curious Case of Neural Text Degeneration"
- Fan, Lewis, and Dauphin (2018), "Hierarchical Neural Story Generation"
- Nguyen et al. (2024), "Turning Up the Heat: Min-p Sampling for Creative and Coherent LLM Outputs"
Last updated