Temperature is a scalar parameter that controls the randomness of token selection during LLM generation by scaling the logits before the softmax operation. At temperature 0 the model becomes deterministic, always picking the highest-probability token; as temperature increases the distribution flattens, making lower-probability tokens more likely to be sampled. Understanding what is temperature in ai models is essential for controlling the trade-off between coherence and diversity in generated text.
How temperature works mathematically
The model outputs raw logits — unnormalized scores — for each token in the vocabulary. Temperature $T$ divides these logits before softmax:
def sample_with_temperature(logits: torch.Tensor, temperature: float) -> int:
if temperature == 0:
return int(torch.argmax(logits))
scaled = logits / temperature
probs = torch.softmax(scaled, dim=-1)
return int(torch.multinomial(probs, num_samples=1))
When $T < 1$, the division amplifies differences between logits. The highest logit pulls further ahead, sharpening the distribution. When $T > 1$, differences shrink, flattening the distribution toward uniform. At $T \to \infty$ every token becomes equally likely; at $T \to 0$ the argmax dominates completely.
This is not the same as top-k or top-p (nucleus) sampling, which truncate the vocabulary before sampling. Temperature reshapes the entire distribution. You can combine them — and often should — but they operate at different stages.
Why temperature matters in practice
Temperature directly controls the creativity-coherence spectrum. Low temperatures produce consistent, factual, repetitive output. High temperatures produce varied, surprising, occasionally incoherent output. The right setting depends entirely on the task.
| Temperature range | Typical use case | Behavior |
|---|---|---|
| 0.0 – 0.2 | Code generation, extraction, classification | Near-deterministic, minimal variance |
| 0.3 – 0.6 | General chat, summarization, RAG answers | Balanced coherence with natural variation |
| 0.7 – 1.0 | Creative writing, brainstorming, roleplay | Noticeable diversity, occasional tangents |
| > 1.0 | Experimental, highly exploratory | High entropy, frequent non-sequiturs |
For production systems, temperature is often the single most impactful sampling knob. A change from 0.7 to 0.3 can eliminate hallucinations in a RAG pipeline; a change from 0.2 to 0.8 can unblock a stuck creative task. Treat it as a first-class configuration parameter, not an afterthought.
Concrete example: same prompt, different temperatures
Prompt: Complete this sentence: "The database migration failed because"
Temperature 0.0 (deterministic):
The database migration failed because the schema version mismatch was detected during the pre-flight check.
Temperature 0.3:
The database migration failed because the foreign key constraint violated the referential integrity between the users and orders tables.
Temperature 0.7:
The database migration failed because someone forgot to run the backup script before dropping the production schema — classic Tuesday energy.
Temperature 1.2:
The database migration failed because the quantum butterflies migrated to a parallel universe where SQL speaks fluent haiku.
The low-temperature outputs are plausible technical explanations. The high-temperature outputs drift into metaphor and nonsense. Neither is “wrong” — they serve different purposes. If you’re generating error messages for a runbook, you want 0.0–0.2. If you’re writing a conference talk opener, 0.7–1.0 might spark better ideas.
Interaction with other sampling parameters
Temperature does not exist in isolation. The effective sampling behavior depends on the full pipeline:
def generate(
model,
input_ids,
temperature: float = 0.7,
top_k: int = 50,
top_p: float = 0.9,
min_p: float = 0.0,
) -> torch.Tensor:
logits = model(input_ids).logits[:, -1, :] # last token logits
# Temperature scaling
if temperature > 0:
logits = logits / temperature
# Top-k truncation
if top_k > 0:
top_k_logits, top_k_indices = torch.topk(logits, top_k)
logits = torch.full_like(logits, float('-inf'))
logits.scatter_(1, top_k_indices, top_k_logits)
# Top-p (nucleus) truncation
if top_p < 1.0:
sorted_logits, sorted_indices = torch.sort(logits, descending=True)
cumulative_probs = torch.cumsum(torch.softmax(sorted_logits, dim=-1), dim=-1)
sorted_indices_to_remove = cumulative_probs > top_p
sorted_indices_to_remove[:, 1:] = sorted_indices_to_remove[:, :-1].clone()
sorted_indices_to_remove[:, 0] = False
indices_to_remove = sorted_indices_to_remove.scatter(1, sorted_indices, sorted_indices_to_remove)
logits[indices_to_remove] = float('-inf')
# Min-p truncation (removes tokens below min_p * max_prob)
if min_p > 0:
probs = torch.softmax(logits, dim=-1)
max_prob = probs.max(dim=-1, keepdim=True).values
logits[probs < min_p * max_prob] = float('-inf')
probs = torch.softmax(logits, dim=-1)
return torch.multinomial(probs, num_samples=1)
Order matters. Temperature scaling happens first, then truncation. This means top-p at temperature 0.5 sees a sharper distribution than top-p at temperature 1.0 with the same $p$ value. The effective nucleus size changes.
Min-p is a newer alternative to top-p that scales with the maximum probability token. It’s often more stable across different temperature settings because it adapts to the distribution’s peak.
Common misconceptions
“Temperature 0 means no randomness”
True for a single forward pass with a fixed KV cache. But most production systems involve:
- Non-deterministic GPU kernels (especially with flash attention)
- Floating-point non-associativity across runs
- Race conditions in batched inference
- Provider-side load balancing across model replicas
You will see different outputs at temperature 0 across requests. The variance is small but non-zero. If you need strict determinism, you must also control the seed, the hardware, and the inference engine configuration.
“Higher temperature = more creative”
Higher temperature = more random. Creativity requires structured novelty, not uniform noise. At temperature 1.5 the model often loses narrative coherence, repeats itself, or emits syntactically valid but semantically empty text. The sweet spot for creative tasks is usually 0.7–1.0, not higher.
“Temperature 1.0 is the ‘natural’ model output”
Temperature 1.0 means “use the logits exactly as the model produced them.” But the model was trained with a specific sampling strategy (usually temperature 1.0 with top-p or top-k during training data generation). The training distribution matches temperature 1.0 only if you replicate the exact training-time sampling pipeline. Most inference pipelines differ, so “natural” is ill-defined.
“You should always use temperature 0 for code”
Temperature 0 eliminates syntactic diversity but not semantic ambiguity. For code generation, a small temperature (0.1–0.2) often produces better results because it allows the model to explore alternative valid implementations when the top token leads to a dead end. Pure argmax can get stuck in local optima, especially with longer completions.
“Temperature and top-p are interchangeable”
They solve different problems. Temperature reshapes the probability curve; top-p truncates the tail. Use temperature to control the shape of uncertainty. Use top-p to control the support — preventing the model from sampling from the long tail of garbage tokens. A typical production config: temperature=0.7, top_p=0.9. This keeps the distribution natural but caps the worst outliers.
Practical guidelines for engineers
Start with 0.7 for chat, 0.2 for code, 0.0 for extraction. These are safe defaults that work across most model families. Then tune per task.
Log the temperature used for every generation. When debugging quality issues, you need to know exactly what sampling parameters produced a given output. Include it in your observability pipeline alongside prompt version, model version, and latency.
Expose temperature as a user-facing control for creative tasks. For analytical tasks, hard-code it. Users cannot reliably tune temperature for factual workloads — they’ll crank it up and get hallucinations, or crank it down and get repetitive loops.
Test temperature sensitivity as part of your eval suite. Run your benchmark prompts at 0.0, 0.3, 0.7, 1.0. Plot quality metrics vs. temperature. The curve shape tells you how robust your prompt is. A flat curve means the prompt constrains the task well; a steep cliff means you’re relying on sampling luck.
Don’t forget the seed. If you need reproducibility for testing or debugging, set a seed and temperature 0. But understand that seed determinism is not guaranteed across model versions, quantization changes, or inference engine upgrades.
When to use what is temperature in ai models as a routing signal
Some inference gateways let you specify sampling parameters per-request and route based on them. For example, you might send temperature 0 requests to a smaller, faster model optimized for deterministic tasks, while routing temperature > 0.5 requests to a larger model with better creative range. This only works if your gateway respects client routing directives and doesn’t override sampling parameters silently — a behavior worth verifying in your provider contract.
Summary
Temperature scales logits before softmax, controlling the entropy of the token distribution. Low values sharpen; high values flatten. It is the primary knob for the coherence-diversity trade-off. Combine it with top-p or min-p to truncate the tail. Treat it as a first-class configuration parameter: log it, test it, tune it per task, and don’t confuse it with creativity. The math is simple; the impact on production quality is not.