Topic 2 of 5 · 5 min read
Temperature, top-p and top-k settings
Goal: Predict how changing temperature or top-p changes a next-token probability table and the outputs it produces.
Temperature, top-p and top-k do not change what the model knows. They change how boldly it picks from the list of likely next tokens.
Check yourself · 1 of 3
What does lowering the temperature do to next-token probabilities?
Why it matters
These are the knobs you will see in API tools, playgrounds and some apps. Set them blindly and you get answers that are either dull and repetitive or loose and strange. Knowing what each does lets you make a calm change instead of a random one.
The three knobs
- Temperature reshapes the whole probability list. A low value (near 0) makes the top token even more likely, so output becomes steady and predictable. At 1 the list is used as the model produced it. A value above 1 flattens the list, so unlikely tokens get a real chance. At exactly 0 most services simply take the top token every time, called greedy choice.
- Top-p (nucleus sampling) cuts the tail. Sort tokens from most to least likely, keep the smallest group whose probabilities add up to p, then drop the rest and pick from that group. With top-p 0.9 you ignore the long tail of odd tokens. The group size changes with the situation: few tokens when the model is sure, many when it is not.
- Top-k keeps only the k most likely tokens, a fixed count. Not every provider offers it.
Temperature squeezes or stretches the list. Top-p and top-k remove entries from it. Most guides advise changing one at a time, not both.
A worked table
Probabilities for the word after "The sky is":
- blue: 60%, clear: 20%, grey: 10%, falling: 6%, plum: 4%.
At low temperature, blue climbs to perhaps 80% and plum almost vanishes. At high temperature the numbers move toward even, so "falling" and "plum" show up often. With top-p 0.8, the kept group is blue and clear (they add to 80%). Grey, falling and plum are removed, even though they were possible a moment ago. With top-k 2, you also keep blue and clear.
Before and after
Before: "The answers are boring and repeat each other, so I will set temperature to 2 and top-p to 1."
After: "I will raise temperature from 0.3 to 0.8, leave top-p alone, and run it five times. If it turns strange, I will go back to a middle value."
What goes wrong
- Very high temperature makes text drift, mix languages or invent facts, because rare tokens are now likely.
- Changing temperature and top-p together makes it hard to see which one caused the change.
- Assuming a higher value means a smarter or more creative model. It only means more randomness. Quality of ideas still comes from the model and the prompt.
- Copying settings between providers. Ranges and defaults differ. Check the API reference of the one you use.
Examples
One table, three settings
Next word after "The sky is": blue 60%, clear 20%, grey 10%, falling 6%, plum 4%.
- Low temperature: blue rises, plum nearly disappears. Output is steady.
- High temperature: the list flattens, so "falling" and "plum" appear often.
- Top-p 0.8: only blue and clear stay, because together they reach 80%.
Fixing a repetitive tagline tool
A tool gives the same three taglines every time. Raising temperature from 0.2 to 0.9 gives more variety. If it then starts producing nonsense words, step back to 0.6 instead of adding other changes.
Key points
- Temperature reshapes all next-token probabilities: low makes the top choice dominate, high flattens the list.
- Top-p keeps the smallest set of tokens whose probabilities add up to p and drops the rest.
- Top-k keeps a fixed number of the most likely tokens and is not offered by every provider.
- Temperature stretches or squeezes the list, while top-p and top-k remove entries from it.
- Change one knob at a time and rerun several times to see the effect.
Common mistakes
- Setting temperature very high to get creativity and getting nonsense.
- Changing temperature and top-p at once, so the cause is unclear.
- Believing temperature adds knowledge or intelligence.
- Reusing one provider's values on another without reading its docs.
Check yourself · 2 of 3
Next-word probabilities: blue 60%, clear 20%, grey 10%, falling 6%, plum 4%. With top-p set to 0.8, which words can be picked?
Check yourself · 3 of 3
A writing tool gives the same few phrases every time. What is the most careful first change?
Practice items, not counted towards your mastery.
Start here
- How to generate text: decoding methods (Hugging Face) (opens in a new tab)ArticleFree
Greedy search, beam search, temperature, top-k and top-p explained with worked examples.
- Defeating Nondeterminism in LLM Inference (Thinking Machines Lab) (opens in a new tab)ArticleFree
Why temperature zero can still give different outputs on a server, and what it takes to fix it.
- Self-Consistency Improves Chain of Thought Reasoning (opens in a new tab)PaperFree
The paper behind majority voting over several sampled answers.
More for this topic
- The Curious Case of Neural Text Degeneration (opens in a new tab)PaperFree
The paper that introduced top-p (nucleus) sampling and why plain likelihood decoding gives dull text.
- Messages API reference (Anthropic) (opens in a new tab)DocsFree
How temperature, top_p and top_k are set on a real API and what the defaults are.