At each step the model outputs a probability for every token in its vocabulary, and decoding picks one. The settings temperature, top_p and top_k decide how random or greedy that pick is.
Temperature, top_p and top_k settings
Temperature reshapes the probability distribution. Near 0, the model almost always takes the highest-probability token. As temperature rises, the distribution flattens and less likely tokens get picked more often, which gives more varied output.
top_p, also called nucleus sampling, restricts sampling to the smallest set of tokens whose cumulative probability passes p. With p at 0.9, the model samples only from the tokens that together hold 90% of the probability mass.
top_k restricts sampling to the k most likely tokens. With k at 50, only the 50 top candidates are considered at each step.
- Raise temperature for randomness, lower it for determinism.
- Use
top_pto cut off the unlikely tail while keeping some diversity. - Use
top_kto cap the number of options.
Choosing temperature by task
| Task | Temperature |
|---|---|
| Extraction, classification, code generation | 0 to 0.3 |
| Brainstorming, marketing copy | 0.7 to 1.0 |
Providers such as OpenAI advise changing temperature or top_p, not both. Pick the one that fits the task and leave the other at its default.
Why greedy decoding falls short
Always taking the top token looks safe, but it can produce repetitive, flat text. The model can get stuck in loops and return degenerate output because it never explores other plausible tokens. Controlled sampling gives varied output while limiting the unlikely tokens that produce nonsense.
Temperature zero is not reproducible
It is tempting to treat temperature 0 like a fixed random seed that always returns the same text. It does not. Output is still not guaranteed to match bit for bit, because GPU floating-point non-determinism and input batching can introduce small variations. Do not build tests or audit trails that assume identical text from identical prompts.
A temperature change in production
Example scenario: after a change, output quality and variety shift sharply. Suppose 95% of outputs are identical. The number is illustrative.
Moving temperature in either direction has a cost. Too low and outputs collapse into near copies. Too high and responses become less coherent and less aligned with what users expect, which can lower engagement.
- Check the current temperature setting and its recent changes.
- Measure coherence with human review or a model-based grader. BLEU and ROUGE only measure overlap with a reference text.
- Compare user engagement before and after the adjustment.
The fix is to return to the last known good value (for example 0.7) and then move in smaller steps. Lower temperatures give more coherent output at some cost to variety. Watch engagement and coherence scores after each change.