Every LLM API exposes temperature and top-p, most people set both to "feel right," and much of the advice online is vague. Here's what each one actually changes about the model's next-token choice — and why tuning both at once fights itself.
Open any LLM playground and you're met with two sliders — temperature and top-p — usually with a vague tooltip about "creativity." So people nudge both, get output that feels a bit different, and move on without knowing what they changed. That's a shame, because these two parameters are precise, they do different jobs, and setting both aggressively at the same time produces behaviour that's hard to reason about. Below, both are worked through on one small set of numbers, so you can see exactly what each does.
What the model is doing at each step
A language model doesn't decide on a whole sentence. At every step it produces a score (a "logit") for every possible next token, and those scores are turned into probabilities — tens of thousands of numbers saying "given everything so far, here's how likely each token is to come next." Sampling is the act of picking one token from that distribution. Temperature and top-p are two different ways of shaping how that pick happens. Neither changes what the model knows; they change how boldly it commits to its own top guesses.
Our running example
Imagine the text so far is "The sky was" and, to keep it readable, the model only considers five next tokens. These logits are invented:
| Token | blue | clear | grey | falling | purple |
|---|---|---|---|---|---|
| Logit | 3.0 | 2.2 | 1.5 | 0.3 | −1.0 |
Temperature: reshaping the whole distribution
Temperature divides every logit before the softmax function turns them into probabilities. Wikipedia describes softmax and the effect of temperature this way:
"The softmax function [...] converts a tuple of K real numbers into a probability distribution over K possible outcomes. [...] A higher temperature results in a more uniform output distribution (i.e. with higher entropy; it is "more random"), while a lower temperature results in a sharper output distribution, usually with one value dominating."
— Wikipedia, "Softmax function" (CC BY-SA 4.0)
Here's what that does to our five tokens (percentages rounded, so a row may not sum to exactly 100):
| Temperature | blue | clear | grey | falling | purple |
|---|---|---|---|---|---|
| 0.2 | 98.1% | 1.8% | 0.1% | 0.0% | 0.0% |
| 0.7 | 68.5% | 21.8% | 8.0% | 1.4% | 0.2% |
| 1.0 | 56.9% | 25.6% | 12.7% | 3.8% | 1.0% |
| 1.5 | 45.7% | 26.8% | 16.8% | 7.6% | 3.2% |
At 0.2 the model says "blue" almost every time. At 1.5, "purple" — the odd one — goes from effectively never to about one draw in thirty. That's the whole mechanism: low temperature concentrates probability on the top token; high temperature spreads it out, and it touches every token, including the long tail.
A note on temperature 0: dividing by zero isn't defined, so APIs treat it as "take the top token" or something close. OpenAI's API reference describes low values like 0.2 as making output "more focused and deterministic"; in practice even very low settings don't guarantee byte-identical output across calls, so don't build anything that depends on it.
Top-p: trimming the tail before you sample
Top-p — nucleus sampling, introduced by Holtzman et al. (arXiv 1904.09751) — takes a different approach. Sort the tokens by probability, keep adding them to a candidate pool until their probabilities sum to at least p, discard the rest, and sample from the pool after rescaling it to 100%. The paper describes this as sampling from "the dynamic nucleus of the probability distribution", truncating "the less reliable tail".
On our example at temperature 1.0 with top-p 0.9: blue + clear + grey reach 95.1%, so those three stay and "falling" and "purple" can't be chosen at all. Rescaled, the pool is blue 59.8%, clear 26.9%, grey 13.3%. With top-p 0.5, blue alone (56.9%) clears the bar and the output is fixed.
The adaptive part is what makes top-p useful. When the model is confident — one token holding most of the probability — reaching 0.9 may take a single token. When it's genuinely uncertain across many plausible words, it may take five or more. The pool shrinks and grows with context, while a fixed rule like "always keep the top 3" can't.
Why tuning both at once fights itself
Now combine them. At temperature 1.5 with top-p 0.9, the flatter distribution means it takes four tokens to reach 90%, so "falling" is back in the pool at 7.8% after rescaling. Raising temperature made the tail fatter, and the same top-p setting then let more of it through. Change both and you can no longer say which knob caused what you see.
That's why the advice from at least one major provider is to choose one. OpenAI's API reference says of temperature: "We generally recommend altering this or top_p but not both." Its defaults there are 1 for both, with temperature ranging from 0 to 2. Other providers' ranges and defaults can differ, so check the reference for the API you're using. For most work, holding top-p at its default and moving temperature alone gives you one interpretable dial: lower for extraction, classification and structured output; higher for brainstorming and drafting.
When the knobs don't help
- Wrong facts. Sampling controls randomness, not knowledge. If the model is confidently wrong, lowering temperature makes it wrong more consistently.
- Format failures. If JSON keeps breaking, the fix is usually a clearer instruction or a structured-output feature, not a lower temperature.
- Repetition. Very low temperature can make loops more likely, because the same top token keeps winning. That's a reason to raise it slightly, not lower it.
- Ambiguous prompts. What looks like randomness is often a prompt that permits several readings.
FAQ
Is top-p the same as top-k? No. Top-k keeps a fixed number of tokens; top-p keeps however many it takes to reach a probability total.
Should creative writing use a high temperature? Higher than for extraction, usually — but past a point the tail tokens start winning often enough to hurt coherence. Test rather than assume.
Does temperature change cost? Not per token, but outputs can change length, which does.
Test the effect instead of guessing
The honest way to tune these is empirically — change one knob, hold everything else, and compare. Run the same prompt at two temperatures and put the results side by side with a prompt diff to see how much the wording actually shifted. Watch length and cost while you experiment with a token counter. And before you blame the sampler for inconsistent behaviour, run your instructions through a system prompt linter. Set one knob deliberately, measure the difference, and the two sliders become two precise tools.
← All articles