Skip to content

Temperature

Summary

The temperature parameter sets the degree of randomness in selecting the next token: low values make the output deterministic, high values make it varied.

Definition

Temperature is a hyperparameter of decoding that scales logits before applying softmax. Formally:

logits_i / T

where T is the temperature. When T = 1, the distribution remains unchanged; when T < 1, the distribution becomes sharper (probability of the most probable token increases); when T > 1, it becomes flatter (all tokens get comparable chances).

Impact on Generation

T Behavior Example Tasks
0.0–0.3 Almost deterministic Code, fact extraction, classification
0.5–0.8 Moderate variability Dialogues, summarization
1.0–1.5 High creativity Idea generation, storytelling
> 1.5 Risk of incoherence Experiments

Relation to Other Parameters

Temperature is used alongside top-p and top-k. In practice, a common combination is: T = 0.7, top-p = 0.9. When T = 0, the sampling becomes greedy (greedy decoding), equivalent to greedy-decoding.

Practical Recommendations

  • For tasks with a definitive answer, set T = 0.1–0.3.
  • For creative tasks, set T = 0.8–1.2.
  • Do not use T > 1.5 unless necessary — the text loses coherence.
  • Remember that temperature does not affect the model's 'knowledge', only the style of selection.

Example Code

from openai import OpenAI

client = OpenAI()
response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Come up with a title for an article"}],
    temperature=0.9
)

Workflow Diagram

graph LR
    A[Logits] -->|division by T| B[Scaled logits]
    B -->|softmax| C[Probability distribution]
    C -->|sampling| D[Token]