How does the temperature parameter reshape the sampling distribution?
Temperature divides the logits before softmax; `T<1` sharpens toward the top token, `T>1` flattens toward the tail, `T=0` is greedy argmax.
Imagine the model's raw output as a row of heights, one per possible next word. The highest height is the model's favorite token. Temperature is a knob that scales those heights before they get turned into probabilities. Turn the knob below 1 and the gaps between heights grow, so the favorite towers over everyone else and almost always wins; the output gets predictable, sometimes repetitive. Turn it above 1 and the gaps shrink, so unlikely tokens get a real chance and the output gets more diverse, sometimes off the rails. Set it to 1 and nothing changes; you sample from the trained distribution as-is. Set it to 0 and you skip sampling entirely, just pick the tallest, every time.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Temperature is the simplest sampling knob and the one that most often gets misunderstood. Engineers talk about it as if it controls creativity or randomness in some abstract sense; under the hood it is a single scalar that divides the logits before softmax. That mechanical fact carries surprising power, because softmax is non-linear, and small temperature changes produce large probability changes once you cross certain thresholds.
This deep dive starts with the math, walks through what happens at each interesting value, covers the three special cases (T = 1, T = 0, T → ∞), explains how temperature stacks with the other sampling parameters, and ends with production realities like determinism and cross-model calibration.
The math: logits in, distribution out
The forward pass of an LLM produces a vector of unnormalized scores called logits, one per token in the vocabulary. The vocabulary is large, typically 50k to 200k tokens. To sample the next token, the decoder turns these logits into a probability distribution via softmax, then draws a token from that distribution.
The canonical softmax is p_i = exp(logits_i) / Σ_j exp(logits_j). Temperature inserts a scaling step before softmax:
Dividing by T < 1 amplifies the logits, so the largest one looms even larger after exponentiation. Dividing by T > 1 shrinks them, so the gap between large and small narrows. The crucial property is that the rank order of tokens does not change; only the probability mass each one receives changes.
It matters that this is logit-space scaling, not probability-space rescaling. If you tried to sharpen the distribution by raising every probability to a power and renormalizing, you would get a similar shape but the calibration would differ from what temperature produces. Temperature is the operation that respects the geometry of the model's confidence; doing the math in logit space is what the framework expects.
Situations where this technique stops working.
2–4 min · Everything important, quickly.
Real products, models, and research that use this idea.
- OpenAI's Chat Completions API accepts `temperature` from 0.0 to 2.0 and defaults to 1.0; code-gen and structured-output use cases commonly set 0.0 to 0.2.
- Anthropic's Messages API takes `temperature` from 0.0 to 1.0 with a default near 1.0, and tool-use prompts often pin it to 0 for deterministic argument selection.
What an interviewer would ask next. Try answering before peeking at the approach.
QWhy does temperature 0 not guarantee deterministic output on a GPU?
Argmax over floating-point logits is deterministic only if the logits themselves are bit-identical run to run. GPU non-determinism in fused attention kernels, atomic reductions in matmul, and library version drift can perturb logits at the LSB level, occasionally flipping the argmax on near-ties.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Describing temperature as scaling probabilities directly. It scales logits before softmax, which is non-linear in token probability and far more aggressive than a linear rescale.
60 second bullets to scan on the way to the call.
Write the temperature formula: divide logits by T before softmax.
Explain what happens at T < 1, T = 1, and T > 1.
Primary sources. Browse if you want the original framing.
Same topic, related formats. Practice these next.