When you ask a chatbot something, it doesn’t look the answer up. It writes one, a word at a time, and every word is a roll of loaded dice. Temperature is the dial that decides how loaded the dice are. Ten minutes from now you’ll know what it does, when to touch it, and why the same question can get two different answers.
One word at a time#
A language model does one job, over and over: given the text so far, it gives every possible next word a score. (Strictly, it scores tokens, which are words or pieces of words. Models know tens of thousands of them, often more than a hundred thousand.)
The scores become chances, the model picks one word according to those chances, adds it to the text, and does it all again. A 300-word reply is a few hundred of these picks in a row.
Here is one pick. Press the button a few times and watch which words come out.
Most picks are the favourites, chocolate and vanilla. But not all of them: mint and strawberry turn up too, and very occasionally something stranger. That spread is what makes a model sound natural instead of robotic. It’s also where some of its mistakes come from.
What the temperature dial does#
Before the model turns scores into chances, it divides every score by the temperature. That one division changes the whole shape of the bars:
- Below 1, the gaps between scores grow, so the favourite gets even more likely.
- At 1, the model’s chances are used as they are.
- Above 1, the gaps shrink and the long shots catch up.
Drag the temperature slider in the figure above and watch the bars, and the chance of getting garlic. Then try this:
Predict first
Set the temperature to 0 and press “Pick 20 times”. What do you get?
Watch it write#
One pick looks harmless. But a reply is hundreds of picks, and every pick becomes part of the text that the next one builds on. Here’s a toy model writing a coffee shop tagline five times over, at whatever temperature you choose.
At 0, all five runs come out the same. Around 0.7 you get variety that still makes sense. Push past 1.5 and long shots start slipping in: mildly haunted soup, until the moon falls. That’s the whole trade-off. Lower is more predictable, higher is more varied, and past a point, varied just means wrong.
Top-p: cut off the long tail#
High temperature has a flaw: it raises good surprises and absurd ones together. Top-p, also called nucleus sampling, deals with that by throwing the unlikely words away before the roll.
It keeps the smallest group of top words whose chances add up to p, drops the rest, and scales the survivors back up to 100%. At a top-p of 0.9, the model can only pick from the words that make up the top 90% of the probability. The oddities in the bottom 10% can’t be picked at all.
With top-p at 1 and the temperature at 1.5, garlic comes up about once in every 60 picks. Bring top-p down to 0.85 and it’s gone, along with regret, while mint and pistachio keep their chance. You keep the variety and lose the nonsense.
One rule of thumb, from the providers’ own documentation: adjust temperature or top-p, not both. Each one changes the bars on its own terms, and moving both at once makes it hard to tell which change did what.
Which setting, when#
Two quick calls to check you’ve got it:
Predict first
You’re pulling invoice totals out of PDFs and into JSON. Which temperature?
Predict first
You’ll call the model 20 separate times to get 20 different names for a podcast. Which temperature?
And the rules of thumb, for when you set it yourself:
- Facts, extraction, code, classifying things: 0 to 0.3.
- Everyday chat and writing help: leave the provider’s default, which is usually 1 or a little below.
- Brainstorming and fiction: up to about 1.2, and if the output gets strange, bring top-p down to around 0.9 instead of lowering the temperature.
Try it in code#
The easiest place to play with these settings for real is a model running on your own computer, free, with Ollama. Our Run LLMs Locally with Ollama tutorial gets you set up in about ten minutes. Then run this:
import ollama
for temperature in [0, 0.8, 1.5]:
reply = ollama.chat(
model="llama3.2",
messages=[{"role": "user", "content": "Write a tagline for a coffee shop."}],
options={"temperature": temperature},
)
print(temperature, reply["message"]["content"])Run it three or four times. The line for 0 barely changes between runs. The line for 1.5 never repeats.
What to remember
- A model writes one token at a time, each picked at random from a list of chances it gives every possible next token.
- Temperature reshapes those chances: low makes the favourite win, high gives long shots a chance, and 0 always takes the favourite.
- Top-p cuts the unlikely tail before each pick. Adjust temperature or top-p, not both.