Sampling: temperature, top-k, top-p
A model gives you probabilities, not an answer. Sampling turns them into one token. Shape one real distribution with temperature, top-k and top-p, and see which tokens can still win.
9 minFree
- Scores
- Temperature
- Top-k
- Top-p
- Renormalise
- Draw
Suggest a name for my new dosa café in Chennai:
Suggest a name for my new dosa café in Chennai:
The model gives every token in its vocabulary a score (a logit). Here are the top 12 candidates for the next token. Higher means the model thinks it fits better. Press Play to turn these into one choice.
The candidate tokens and their scores are illustrative. Softmax, temperature, the top-k and top-p cuts, renormalisation and the random draws are the real maths. Libraries differ in the exact order of the steps; this is a common one.
Background: the idea in plain words
An LLM (large language model) writes text one token (a small piece of text) at a time. At every step an LLM scores every token in its vocabulary (the list of all tokens it knows) and turns the scores into probabilities, which are chances that add up to 100% (see What is an LLM?).
But a list of chances is not an answer. Something still has to pick one token. That something is the sampler. If it always took the favourite, the text would be safe but repetitive. If it picked with no limits, odd tokens would sometimes win. Three settings control it:
- Temperature reshapes the probabilities. Low makes the favourite even more likely. High spreads the chances out.
- Top-k keeps only the k most likely tokens.
- Top-p (also called nucleus sampling) keeps the smallest set of top tokens whose probabilities add up to p.
Whatever survives is rescaled to add up to 100%, and one token is drawn at random. The Gemini API reference describes exactly this: top-k "limits the maximum number of tokens to consider", and nucleus sampling "limits the number of tokens based on the cumulative probability".
A tiny example, with made-up numbers. After "My favourite breakfast is", the model gives "dosa" 50%, "idli" 30%, "toast" 15% and "zebra" 5%. With top-k set to 3, "zebra" is removed. The other three are rescaled to add up to 100%, and one is drawn. "dosa" wins most often, but not every time.
Press Play to walk through each step on one distribution. Then switch Try it to Wild and press Draw ×20 a few times, and compare it with the last preset, which uses the same temperature with top-k and top-p switched on.
What to notice
- Temperature changes the shape, not the order. The favourite token stays the favourite at every temperature. What changes is how far ahead it is. At 0.2, "Madras" and "Dosa" take almost everything. At 2, "Zebra" and "Tuesday" get a real chance.
- Top-k is a fixed shortlist. It always keeps k tokens, whether the model is sure or unsure. That's simple, but it can keep junk when the model is confident and cut good options when it isn't.
- Top-p adapts to the model's confidence. When one token dominates, top-p keeps a short list. When many tokens are close, it keeps more.
- The cuts make high temperature safer. At temperature 2 with no cuts, odd names win now and then. Add top-k and top-p and the long tail is gone, so you get variety from good candidates only.
- Temperature 0 means no randomness. The sampler always takes the top token (this is called greedy decoding), and top-k and top-p no longer matter.
| You want | Try |
|---|---|
| The same answer every run (extraction, classification, tool calls) | Low temperature |
| Varied but sensible text (names, ideas, rewrites) | Moderate temperature with top-p around 0.9 |
| Brainstorming where odd ideas are welcome | Higher temperature, keep a top-p cut |
Check your model
Defaults vary by model. The Gemini API docs say that Gemini 4 and Gemini 3.6+ models ignore custom temperature, top_p and top_k values, and recommend removing them from requests. The ideas still explain what the model is doing, but on those models you steer the output with your prompt and other settings instead.
Go deeperWords to know
| Word | Meaning in simple words |
|---|---|
| Sampler | The step that picks one token from the model's list of chances |
| Distribution | The full list of chances, one for each possible next token |
| Temperature | A setting that reshapes the chances: low favours the top token, high spreads them out |
| Top-k | Keep only the k most likely tokens |
| Top-p (nucleus sampling) | Keep the smallest group of top tokens whose chances add up to p |
| Greedy decoding | Always taking the most likely token, with no randomness |
Check your understanding
Sources: the official documentation
This lesson is based on the official documentation. Check it for the latest details.
Something wrong or unclear in this lesson? Tell us, it gets fixed.