AI Engineer Roadmap All lessons Interactive Quiz MernStackDev GitHub

Temperature

Play the interactive lesson: TemperatureRead on GitHub
On this page
  1. Explain it like I'm new
  2. Watch it
  3. How it works
  4. Code example
  5. When to use it / when not to
  6. Common mistakes
  7. Recap
  8. Interview questions
  9. Go deeper

Level: Beginner · Reading time: 7 min · Phase: 3 · LLM Concepts

In one line: Temperature is a number that controls how random the model's word choice is: low values make it pick the most likely token, high values spread the choice out.

Before you start: Decoder-only models (they produce one token at a time, which is the step temperature controls).

Words to know:

Explain it like I'm new

Imagine choosing dinner. On a cautious day (low temperature) you always order your favourite. On an adventurous day (high temperature) you might try the third or fourth item on the menu. The menu has not changed; only your willingness to take a chance has.

At each step, an LLM outputs a raw score called a logit for every possible next token. The softmax function turns these scores into probabilities that add up to 1. Temperature changes the scores before softmax:

Temperature does not make the model smarter or more knowledgeable. It only changes how it picks among options it already scored.

Watch it

Temperature: A dial for randomness: low is predictable, high is varied.
Temperature: A dial for randomness: low is predictable, high is varied.

It plays on a loop. Read the caption in the box, one step at a time. Want to go at your own pace? Step through it with Previous and Next.

How it works

p_i = exp(logit_i / T) / sum_j exp(logit_j / T)
  1. Get logits from the model.
  2. Divide every logit by the temperature T.
  3. Apply softmax.
  4. Sample a token from the resulting probabilities (often after Top-k or Top-p filtering).

Worked example. After "The capital of France is", logits are Paris 5.0, London 3.2, Rome 2.9, banana 0.5, the 1.0 (from sampling_demo.py):

TParisLondonRomebananathe
0.21.000.000.000.000.00
0.50.960.030.010.000.00
1.00.760.130.090.010.01
2.00.500.200.180.050.07

As T → 0, sampling becomes greedy (always the top token). Many APIs treat temperature=0 as "as deterministic as possible", but tiny run-to-run differences can still happen because of how servers batch and compute.

Worked example (by hand, two tokens)

Take just two tokens with logits A = 2 and B = 0. Use e^2 ≈ 7.39 and e^0 = 1.

  1. T = 1: divide by 1, so the scores stay 2 and 0. Exponentials: 7.39 and 1. Total 8.39. Probabilities: A = 7.39 / 8.39 ≈ 0.88, B = 1 / 8.39 ≈ 0.12.
  2. T = 2: divide by 2, so the scores become 1 and 0. Exponentials: e^1 ≈ 2.72 and 1. Total 3.72. Probabilities: A = 2.72 / 3.72 ≈ 0.73, B = 1 / 3.72 ≈ 0.27.
  3. The same two logits gave A 0.88 at T=1 but only 0.73 at T=2. Higher temperature moved probability from the favourite to the underdog.

The five-token table above shows the same effect on real numbers.

flowchart LR
    L[logits] --> D["divide by T"] --> S[softmax] --> F[optional top-k / top-p] --> P[sample token]

Code example

From sampling_demo.py (run python 03-llm-concepts/code/sampling_demo.py):

import math

TOKENS = ["Paris", "London", "Rome", "banana", "the"]
LOGITS = [5.0, 3.2, 2.9, 0.5, 1.0]

def softmax(logits, temperature=1.0):
    if temperature <= 0:
        raise ValueError("temperature must be > 0")
    scaled = [x / temperature for x in logits]
    m = max(scaled)                       # subtract max for numerical stability
    exps = [math.exp(x - m) for x in scaled]
    return [e / sum(exps) for e in exps]

for t in (0.2, 1.0, 2.0):
    print(t, [round(p, 2) for p in softmax(LOGITS, t)])

In real projects temperature is a request parameter:

import anthropic
client = anthropic.Anthropic()
msg = client.messages.create(
    model="claude-sonnet-4-5", max_tokens=200, temperature=0.2,
    messages=[{"role": "user", "content": "Extract the invoice total from: ..."}],
)

Check your provider's docs for the allowed range (for example Anthropic accepts 0-1, OpenAI 0-2).

When to use it / when not to

TaskSuggested temperature
Extraction, classification, code, SQL, tool argumentslow (0-0.3)
General Q&A, summariesmedium (around 0.3-0.7)
Brainstorming, creative writing, varied test datahigher (0.8-1.0+)

Common mistakes

Recap

If you remember only three things:

Interview questions

1. What does temperature do mathematically? <details><summary>Answer</summary> It divides the logits by T before softmax. T below 1 sharpens the distribution toward the top token; T above 1 flattens it. </details>

2. What happens as temperature approaches 0? <details><summary>Answer</summary> The probability of the highest-scoring token approaches 1, so sampling becomes greedy decoding. </details>

3. Which temperature would you use for an agent's tool calls and why? <details><summary>Answer</summary> Low, often 0 or close to it. Tool arguments must be exact and valid; randomness only adds errors. </details>

Go deeper

Prev: Context Window · Next: Top-k · Back to phase: Phase 3