Temperature & Sampling
Temperature is a knob that controls the randomness of generation: turn it down to make the model conservative and predictable, turn it up to make it creative and erratic.
Why Does This Exist?
When an LLM finishes its forward pass, it outputs a list of raw, unnormalized numbers called logits—one for every word in its vocabulary. To pick the next word, we run these logits through a function called the softmax, which squashes them into a proper probability distribution that sums to 1.0 (e.g., "apple" is 80% likely, "banana" is 15% likely, "car" is 5% likely).
If we sample from this distribution natively, we get whatever balance of predictability and randomness the model naturally learned during training. But what if we want the model to write a highly creative, unpredictable poem? Or what if we want it to output strict, predictable JSON code?
Temperature exists to give the user a dial to override the model's natural confidence. By applying a simple mathematical division before the softmax, we can artificially sharpen the distribution (making the model fiercely confident in its top choices) or flatten it (forcing the model to gamble on unlikely words).
Think of It Like This
A volume knob for confidence
Imagine a group of friends trying to decide where to eat.
- Bob says, "I'm 60% sure I want pizza, 30% burgers, 10% sushi."
If you turn the Temperature Down (e.g., 0.1), it's like injecting Bob with an extreme dose of stubbornness. The 60% lead becomes overwhelming. Bob now says, "I am 99% sure I want pizza. Nothing else." (Conservative, predictable, safe).
If you turn the Temperature Up (e.g., 2.0), it's like Bob drank five margaritas. His preferences flatten out completely. He says, "You know what? 34% pizza, 33% burgers, 33% sushi. Let's roll the dice!" (Creative, erratic, random).
How It Actually Works
The Math:
The mathematical implementation of temperature is remarkably simple. We take the raw logits () and divide them by the temperature () before feeding them into the standard exponential softmax function.
: Sharpening the distribution (Cold)
If you divide a set of numbers by a decimal like 0.5, you are effectively multiplying them by 2. This stretches the distance between the highest logit and the rest of the pack. When these stretched numbers are exponentiated in the softmax, the leading token dominates entirely, often jumping to a 99% probability. The generation behaves almost exactly like greedy decoding, reliably outputting the single safest answer.
Use cases: Code generation, data extraction, math, factual Q&A.
: Natively calibrated (Neutral)
Dividing by 1 leaves the logits unchanged. You get exactly what the model learned during training.
: Flattening the distribution (Hot)
If you divide the logits by a large number like 2.0, you shrink the distance between them. The highest logit and the lowest logit are pulled closer to zero, making them mathematically closer together. When exponentiated, the resulting probabilities flatten out toward a uniform distribution. A token that was natively a 1% chance might suddenly become a 15% chance. The model will frequently choose strange, unlikely words. If pushed too high (e.g., T=5), the model devolves into complete gibberish.
Use cases: Brainstorming, creative writing, breaking out of repetitive loops.
Show Me the Code
You can see exactly how the temperature parameter reshapes a set of raw logits before sampling.
import numpy as np
def temperature_softmax(logits: np.ndarray, temperature: float) -> np.ndarray: # Divide the logits by the temperature scaled_logits = logits / temperature # Standard softmax math (subtracting max for numerical stability) e = np.exp(scaled_logits - np.max(scaled_logits)) return e / np.sum(e)
# Raw output from the model: Token 0 is the favorite, Token 1 is second.logits = np.array([4.0, 2.0, -1.0, -3.0])
# T = 1.0 (Standard)print(f"T=1.0 : {temperature_softmax(logits, 1.0).round(3)}")# -> [0.88 0.119 0.006 0.001] (Token 0 has a commanding 88% lead)
# T = 0.2 (Cold/Greedy-like)print(f"T=0.2 : {temperature_softmax(logits, 0.2).round(3)}")# -> [1. 0. 0. 0.] (Token 0 is now 100% certain. Token 1 is eradicated)
# T = 3.0 (Hot/Creative)print(f"T=3.0 : {temperature_softmax(logits, 3.0).round(3)}")# -> [0.575 0.295 0.108 0.056] (Token 0 dropped to 57%. The rare tokens now have a real chance)Notice how T=0.2 entirely destroys the chance of drawing anything other than the highest-ranked token, while T=3.0 gives even the deeply negative logits a fighting chance.
Watch Out For
Using Temperature without Top-P
High temperatures (T > 1.0) make text much more creative, but they also elevate the probability of completely broken, nonsensical tokens (the "long tail"). To get the benefits of high temperature without the model devolving into gibberish, it must almost always be paired with Top-P (nucleus) sampling (upcoming). Top-P acts as a safety net, cutting off the absolute worst tokens before the flattened temperature distribution can select them.
Setting Temperature to exactly 0
As the math shows, dividing logits by 0 will trigger a division-by-zero error in your code. When a hosted API offers a "Temperature = 0" setting, it is not actually doing math with zero; it is a convenience flag that switches the backend engine from a sampling algorithm to an entirely different, deterministic greedy decoding algorithm.
The Quick Version
- Temperature is a mathematical dial that reshapes a model's confidence before it chooses the next word.
- The raw logits are divided by the temperature value prior to being passed into the softmax function.
- A low temperature (T < 1) stretches the logits, making the top choice overwhelmingly likely (predictable, safe).
- A high temperature (T > 1) compresses the logits, flattening the probabilities and giving rare words a chance (creative, erratic).
- Most APIs map a temperature of exactly
0to a purely deterministic greedy decoding path.
What to Read Next
- Top-K Sampling explains another method to control randomness, typically applied after temperature.
- Decoding Strategies gives the overarching view of how all these sampling techniques compare to greedy and beam search.
- Top-P Sampling (upcoming) is the dynamic safety net that is almost universally paired with high temperatures to prevent gibberish.