Decoding Temperature › Temperature Selection Guidelines
Language AI HandbookAll chapters Decoding Temperature › Temperature Selection GuidelinesOne section · Sections in this chapter6 / 11 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Choosing the right temperature depends on your application. The key insight is that temperature controls the trade-off between coherence and creativity. Lower temperatures produce safer, more predictable text by sharpening the model's probability distribution around its most confident predictions. Higher temperatures introduce novelty by flattening the distribution and giving less probable tokens meaningful chances of being selected. Neither extreme is universally correct: the right setting depends entirely on what you want from the generation.
A useful mental model: think of temperature as adjusting how conservative or adventurous the model's token choices are, not as adding external noise. The model's learned representations are encoded in the logits; temperature only adjusts how boldly the model acts on its preferences. Low temperature means "trust your top choices absolutely." High temperature means "consider your alternatives more seriously."
One practical heuristic: start with as a general-purpose default, which most practitioners find provides a good balance of quality and variety. Then adjust upward if outputs feel repetitive or templated, and adjust downward if outputs feel incoherent or off-topic. Small adjustments of 0.1 to 0.2 units can have significant perceptible effects, so tune incrementally.
Task-Based Recommendations
Different generation tasks call for different temperature settings. This reflects the varying priorities of accuracy versus creativity in each domain. The right temperature for code generation is very different from the right temperature for poetry, because the definition of "good output" differs substantially across tasks.
For tasks where correctness is binary (the code either runs or it does not, the fact is either right or wrong), temperature near zero is almost always preferable. Every step away from the most probable token is a risk of introducing an error. For tasks where quality is subjective and diversity is valuable (creative writing, brainstorming, generating multiple options to choose from), higher temperatures enable the model to produce a broader range of plausible completions. The table below summarizes common task-temperature pairings:
| Task | Recommended T | Rationale |
|---|---|---|
| Code generation | 0.0 - 0.3 | Correctness matters; creativity can introduce bugs |
| Factual Q&A | 0.0 - 0.5 | Accuracy over variety; want the most likely correct answer |
| Translation | 0.3 - 0.7 | Balance fluency with fidelity to source meaning |
| Creative writing | 0.7 - 1.2 | Encourage unexpected but coherent word choices |
| Brainstorming | 1.0 - 1.5 | Explore diverse ideas; some randomness is beneficial |
| Poetry/experimental | 1.2 - 2.0 | Prioritize novelty and surprise |
For code and factual tasks, you often want temperature near zero. A slight temperature (0.1 to 0.2) can prevent the model from getting stuck in repetitive loops while still strongly favoring high-probability outputs. At (pure greedy decoding), the model can get trapped generating the same phrase indefinitely if the context makes that phrase highly probable. A small temperature provides enough stochasticity to escape these loops without much loss in output quality. For creative tasks, temperatures between 0.7 and 1.2 typically produce the best balance of quality and variety, where the model feels expressive rather than robotic without creating nonsensical text.
The Quality-Diversity Trade-off
Temperature creates an inherent trade-off between output quality and output diversity. As you increase temperature, you gain:
- Lexical diversity: More varied word choices, less repetition of the same phrases
- Idea exploration: Access to less probable but potentially interesting continuations
- Reduced mode collapse: Less tendency to repeat the same high-probability phrases and structures
But you also risk:
- Coherence degradation: Sentences that do not follow logically from the preceding context
- Factual errors: Lower-probability (and potentially wrong) claims and statements
- Grammatical mistakes: Unusual token sequences that violate syntactic constraints the model learned
The sweet spot depends on how much you value diversity versus correctness. For a customer service chatbot, coherence and accuracy dominate: use low temperature. For a creative writing assistant, moderate temperature encourages the unexpected turns that make prose interesting. For a coding assistant, deterministic output is almost always preferable: bugs introduced by high-temperature sampling are much more costly than style variation.

The conceptual curves illustrate why practitioners often settle on the 0.7 to 1.2 range. Below 0.7, quality is high but diversity is low: outputs feel correct but robotic, using the same phrases repeatedly. Above 1.2, diversity continues to increase but quality degrades: grammatical errors and factual slips become more common. The optimal zone represents a region where both curves are at acceptable levels. Note that this is a conceptual illustration; the exact curves depend on the model, the task, and how "quality" and "diversity" are defined.
Dynamic Temperature
Some applications benefit from varying temperature during generation. You might start with low temperature to establish a coherent beginning, then increase temperature to introduce variation, then decrease again to conclude coherently. This technique requires careful tuning but can produce text that is both well-structured and creatively varied.
Dynamic temperature is particularly useful for long-form generation tasks like story writing. The opening sentences of a story usually need to be coherent and establish setting and character. The middle can experiment more freely. The conclusion benefits from returning to confident, direct language. By scheduling temperature to match these phases, you can produce output that feels purposeful throughout rather than uniformly cautious or uniformly random.
def dynamic_temperature(
position: int,
total_length: int,
t_start: float = 0.5,
t_peak: float = 1.2,
t_end: float = 0.6,
) -> float:
"""Compute temperature that varies across generation.
Uses a simple curve: starts low, peaks in the middle,
then decreases toward the end.
"""
# Normalized position [0, 1]
p = position / total_length
# Parabolic curve peaking at p=0.5
if p < 0.5:
# Rise from t_start to t_peak
return t_start + (t_peak - t_start) * (2 * p)
else:
# Fall from t_peak to t_end
return t_peak - (t_peak - t_end) * (2 * (p - 0.5))Dynamic temperature across 100-token generation: Position 0 (start): T = 0.50 Position 25: T = 0.85 Position 50 (middle): T = 1.20 Position 75: T = 0.90 Position 99 (end): T = 0.61
