Learn · Temperature & Sampling
How temperature shapes creativity
When a language model predicts the next token, it picks from a probability distribution. Temperature, top-k and top-p control how random that choice is — from deterministic and safe to creative and surprising.
Prompt:
The cat sat on the ___
Illustrative candidate tokens and base logits below — the maths is real, the candidates are for demonstration.
Low = focused, predictable. High = random, creative.
Only consider the top k most likely tokens.
Keep smallest set of tokens with cumulative probability ≥ p.
Probability Distribution
Temperature
Divides logits before softmax. Low temperature (→0) sharpens the distribution toward the most likely token; high temperature (→∞) flattens it, making all tokens more equally likely.
Top-k
Keep only the k highest-probability tokens, discard the rest, then renormalise. Reduces long-tail surprises. Setting k=1 is greedy decoding (always pick the most likely).
Top-p (nucleus)
Keep the smallest set of tokens whose cumulative probability is ≥ p. Adaptive: when the model is confident, few tokens remain; when uncertain, more do. Often combined with top-k.
These sampling strategies let you trade off coherence and creativity. Real deployments often use temperature ~0.7–0.9, top-k ~40, top-p ~0.9. For more, see Learn.