Temperature, Top-p and Sampling: How LLMs Choose the Next Token
How LLM sampling works: probability distributions, temperature, top-p (nucleus) and top-k, determinism and recommended settings by task.
Published December 1, 2026
At every step, a language model produces a probability for every token in its vocabulary. Sampling parameters decide how the next token is picked from that distribution. Temperature, top-p and top-k are the knobs you will see in almost every LLM API, and setting them sensibly makes the difference between reliable structured output and rambling variety.
The concept
Temperature reshapes the distribution before sampling. At low temperature, high-probability tokens become even more likely, making output focused and repetitive; at temperature 0 (or near it), the model nearly always picks the top token. Higher temperatures flatten the distribution, so less likely tokens get picked more often, increasing variety and also the risk of errors.
Top-p (nucleus sampling) keeps only the smallest set of tokens whose cumulative probability reaches p (for example 0.9) and samples from those. Top-k keeps only the k most likely tokens. These truncate the unlikely tail. Most providers recommend adjusting either temperature or top-p, not both aggressively. Note that even temperature 0 is not guaranteed to be perfectly deterministic across calls on some providers.
What temperature does
The sampling pipeline
- Model outputs logits for every vocabulary token
- Divide logits by temperature, apply softmax → probabilities
- Optionally keep top-k tokens or the top-p nucleus
- Sample one token from what remains
- Append it and repeat for the next token
Practical settings
| Task | Temperature | Why |
|---|---|---|
| Data extraction, classification, JSON | 0 - 0.2 | Consistency matters most |
| Code generation | 0 - 0.4 | Correctness over variety |
| Q&A over documents (RAG) | 0 - 0.3 | Stay close to the sources |
| Brainstorming, marketing copy | 0.7 - 1.0 | Variety is the goal |
| Fiction, poetry | 0.8 - 1.2 | Creativity; review output |
Starting points only; some reasoning models fix or ignore these parameters.
Practical examples
Setting sampling parameters in an API call
Parameter names are similar across providers.
response = client.chat.completions.create(
model=MODEL,
messages=[{"role": "user", "content": "Extract the invoice number and total as JSON."}],
temperature=0,
max_tokens=200,
)Temperature by hand
How temperature changes a softmax distribution.
import numpy as np
logits = np.array([4.0, 2.5, 1.0])
for t in (0.2, 1.0, 2.0):
p = np.exp(logits / t); p /= p.sum()
print(t, p.round(3))Self-consistency: sample several, pick the majority
Use some temperature to get diverse reasoning, then vote.
answers = [ask(question, temperature=0.7) for _ in range(5)]
final = max(set(answers), key=answers.count)Common mistakes
- High temperature for structured output, causing malformed JSON.
- Cranking both temperature and top-p together and getting chaotic results.
- Assuming temperature 0 makes outputs perfectly reproducible.
- Using temperature to "fix" hallucinations instead of improving context.
Key takeaways
- Models output a probability distribution; sampling picks the next token.
- Temperature sharpens or flattens the distribution.
- Top-p / top-k cut off the unlikely tail.
- Low temperature for extraction and code; higher for creative work.
FAQ
Does temperature 0 eliminate hallucinations?
No. It makes the most likely answer more consistent, but if the model lacks the facts, the most likely answer can still be wrong.
What is a seed parameter?
Some APIs accept a seed to make sampling more reproducible, but results may still vary across model versions or infrastructure.
Why do some models ignore temperature?
Some reasoning-focused models use fixed internal sampling and restrict these parameters.