Lesson 6.1 · 1 of 5 · 4 min read · Reviewed 24 Nov 2026
Temperature
By the end, you can
- Explain what temperature does.
- Pick a temperature for a task.
Lesson 2.4 introduced why outputs vary. This lesson looks at the dial.
Reshaping the distribution
next token after "The weather today is"
low temp (0.2) default (1.0) high temp (1.5)
" sunny" 0.86 0.45 0.30
" cloudy" 0.12 0.30 0.27
" lovely" 0.02 0.15 0.22
" purple" 0.00 0.01 0.06
Low temperatureTemperature: A sampling setting that controls randomness: lower values make output more predictable, higher values more varied. concentrates probability on the top choices. High temperature spreads it out, so unlikely tokensToken: The unit a model reads and writes: a word, part of a word, a space or a symbol, mapped to an integer id. get picked more often.
On Claude
Temperature is a general idea you’ll meet across many model APIs and open-weight modelsOpen-weight model: A model whose trained weights are published so you can download, run and fine-tune it yourself.. Older Claude models accepted a temperature setting (0 to 1, default 1). Current Claude models don’t support temperature, top_p or top_k. Depending on the model and API version, sending one returns an error or is silently ignored (some newer API versions reject them even at default values), so remove them rather than retune them. Check the migration guide for the exact model you use. You get consistency on Claude from clear instructions, examples, structured outputsStructured outputs: Making a model return data that matches a defined schema, such as JSON with required fields and types. and the effort setting (13.6), not from samplingSampling: Choosing the next token from the model’s probability distribution, rather than always taking the most likely one. knobs. Even where temperature exists, 0 doesn’t guarantee identical output every time.
What it doesn’t do
- It doesn’t make answers more correct. A wrong fact at high probability stays wrong at low temperature.
- It doesn’t control length; use
max_tokensand instructions. - It doesn’t replace clear prompts and validation.
Typical choices
| Task | Temperature |
|---|---|
| Extraction, classification, grading | Low (near 0) |
| Factual Q&A, coding | Low to moderate |
| Brainstorming, creative writing | Higher (near the default) |
Key points
- The model produces a probability for every possible next token; temperature reshapes those probabilities before one is picked.
- Low temperature sharpens the distribution toward the most likely tokens; high temperature flattens it, adding variety.
- Temperature changes how predictable output is, not how correct or knowledgeable the model is. Current Claude models no longer accept it; you steer them with prompts, schemas and effort.
Reshape probabilities with temperature
Apply temperature to a next-token distribution and measure how often unlikely tokens get picked.
You will have: A script that prints the reshaped probabilities at three temperatures and the counts from 1,000 seeded samples at each.
Steps
- Run the starter: every temperature prints the same probabilities.
- Fill in with_temperature(): divide each logit by T, then apply softmax.
- Sample 1,000 tokens at T = 0.2, 1.0 and 1.5 with random.Random(7).
- Compare how often ' purple' appears at each temperature.
- Write one sentence on why lowering T does not make a wrong top token right.
Done when
- At T = 1.0 the probabilities match the original ones.
- At T = 0.2 ' sunny' has over 85% of the probability.
- ' purple' is sampled more often at T = 1.5 than at T = 0.2.
Open in the Playground No API key needed.
Starter code (Python)
import math, random
TOKENS = [" sunny", " cloudy", " lovely", " warm", " grey", " purple"]
PROBS = [0.45, 0.30, 0.15, 0.05, 0.04, 0.01]
LOGITS = [math.log(p) for p in PROBS]
def softmax(xs):
m = max(xs)
e = [math.exp(x - m) for x in xs]
return [x / sum(e) for x in e]
def with_temperature(logits, t):
return softmax(logits) # TODO: divide every logit by t first
for t in [0.2, 1.0, 1.5]:
p = with_temperature(LOGITS, t)
rng = random.Random(7)
picks = rng.choices(TOKENS, weights=p, k=1000)
print(f"T={t}: p(sunny)={p[0]:.2f} purple picked {picks.count(' purple')} times")
Show a solution
import math, random
TOKENS = [" sunny", " cloudy", " lovely", " warm", " grey", " purple"]
PROBS = [0.45, 0.30, 0.15, 0.05, 0.04, 0.01]
LOGITS = [math.log(p) for p in PROBS]
def softmax(xs):
m = max(xs)
e = [math.exp(x - m) for x in xs]
return [x / sum(e) for x in e]
def with_temperature(logits, t):
return softmax([x / t for x in logits])
for t in [0.2, 1.0, 1.5]:
p = with_temperature(LOGITS, t)
rng = random.Random(7)
picks = rng.choices(TOKENS, weights=p, k=1000)
print(f"T={t}: p(sunny)={p[0]:.2f} purple picked {picks.count(' purple')} times")