Inferensys

Glossary

Temperature

A hyperparameter that controls the randomness of a language model's predictions by scaling the logits before applying the softmax function.
ML engineer managing model versions on laptop, version history visible, technical Git-like workflow.
STOCHASTIC DECODING CONTROL

What is Temperature?

Temperature is a hyperparameter that controls the randomness of a language model's predictions by scaling the logits before applying the softmax function, directly influencing the balance between deterministic coherence and creative variability in generated text.

In the self-hosted LLM inference pipeline, temperature is applied during the decoding phase. Before token probabilities are computed, the raw output scores—known as logits—are divided by the temperature value. A setting of 0 effectively collapses the distribution, forcing the model to always select the single highest-probability token, resulting in fully deterministic, repeatable outputs ideal for factual extraction or code generation.

Conversely, increasing the temperature above 1.0 flattens the probability distribution, giving lower-ranked tokens a greater chance of being selected. This introduces stochasticity that can produce more varied and seemingly creative text. However, excessively high values amplify noise, often leading to incoherent or nonsensical output. In practice, temperature is tuned alongside Top-P sampling to precisely calibrate the entropy of generated sequences for specific enterprise applications.

Stochastic Decoding Control

Key Characteristics of Temperature

Temperature is a critical hyperparameter that modulates the entropy of a language model's output distribution, directly controlling the balance between deterministic precision and creative variability in generated text.

01

Logit Scaling Mechanism

Temperature operates by dividing the raw logit scores by the temperature value before the softmax function is applied. A temperature of T=1.0 leaves the original distribution unchanged. Values approaching T=0 amplify the differences between logits, making high-probability tokens overwhelmingly likely and producing near-deterministic outputs. Values above T=1.0 compress the differences, flattening the distribution and giving lower-probability tokens a greater chance of being selected. This scaling directly controls the sharpness of the probability mass function.

T=0.0
Deterministic (Greedy)
T=1.0
Default Neutral
02

Entropy and Creativity Trade-off

Adjusting temperature is fundamentally an entropy control mechanism. Low temperature (e.g., T=0.2) minimizes variance, making the model consistently choose the most statistically likely token. This is ideal for factual extraction, code generation, and translation where accuracy is paramount. High temperature (e.g., T=1.5) injects noise, enabling the model to escape repetitive loops and produce diverse, unexpected, and creative text. However, excessive temperature leads to incoherence, as the model begins sampling from the long tail of irrelevant tokens.

T=0.0-0.3
Factual/Precise Range
T=0.7-1.2
Creative Range
03

Interaction with Sampling Strategies

Temperature is applied before other sampling constraints like Top-P (nucleus sampling) and Top-K. The order of operations is critical:

  • Step 1: Logits are divided by the temperature value.
  • Step 2: The resulting scaled distribution is filtered by Top-K (keeping only the K highest-probability tokens).
  • Step 3: Top-P further truncates the set to the smallest group whose cumulative probability exceeds P. A high temperature flattens the distribution, which can cause Top-P to include a wider variety of tokens, while a low temperature concentrates probability mass into a very small set, often overriding the effect of Top-P.
Logits
Input to Scaling
Softmax
After Scaling
04

Practical Calibration Examples

Effective temperature settings are highly task-dependent and model-specific:

  • Structured JSON Generation: Use T=0.0 (greedy decoding) or T=0.1 to guarantee valid syntax and prevent hallucinated keys.
  • Conversational Chatbots: A range of T=0.7 to T=0.9 provides a natural, engaging tone without becoming unhinged.
  • Creative Storytelling: Values between T=1.0 and T=1.5 encourage novel plot twists and descriptive language.
  • Mathematical Proofs: Set to T=0.0 to enforce strict logical step-by-step derivation without random deviation. Note that heavily quantized models may exhibit different sensitivity to temperature changes due to altered weight distributions.
T=0.0
Code & Math
T=0.8
General Chat
05

Relationship to Top-P and Top-K

Temperature, Top-P, and Top-K form a triad of distribution control. While temperature reshapes the entire distribution, Top-K imposes a hard cutoff on the number of candidates, and Top-P imposes a dynamic cutoff based on cumulative probability mass. A common misconfiguration is setting high temperature with low Top-P, which creates a contradictory state: the flattened distribution is immediately truncated, often resulting in bland or repetitive text despite the high temperature setting. Best practice is to tune temperature first, then apply Top-P to trim the noisy tail.

T=0.9, P=0.95
Balanced Creative
T=0.3, P=0.9
Focused Factual
06

Impact on Hallucination Mitigation

Lowering temperature is a primary hallucination suppression technique for enterprise RAG applications. By reducing the sampling variance, the model is forced to rely heavily on the most statistically probable tokens, which in a grounded context are those drawn from the retrieved documents. Setting T=0.0 or T=0.1 ensures the model acts as a near-deterministic summarizer of the provided context rather than an improvisational generator. However, this can also cause the model to get stuck in repetitive loops if the context itself is ambiguous, requiring a balanced approach with structured output constraints.

T=0.0
Maximum Grounding
T=1.0+
High Fabrication Risk
MODEL BEHAVIOR CONTROL

Frequently Asked Questions

Understanding how the temperature hyperparameter governs the balance between deterministic precision and creative variability in large language model outputs.

Temperature is a hyperparameter that controls the randomness of a language model's predictions by scaling the logits before applying the softmax function. Mathematically, it divides the raw output scores by a constant T, where lower values sharpen the probability distribution toward the most likely token and higher values flatten it to give less probable tokens a greater chance of selection. This mechanism directly governs the trade-off between deterministic, focused outputs and diverse, exploratory generations. Logits represent the unnormalized raw scores output by the final linear layer, and softmax converts them into a probability distribution. Without temperature scaling, the model would always select the highest-probability token, producing identical outputs for identical prompts.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.