Kolkata ·  
AI engineering
foundations
deep · 6 min

Sampling

Picking one token from the probability list: always the top one (greedy), or at random by weight.

updated 23 Sept 2026

Ask a model the same question twice and you'll often get two different answers. Why? Nothing about the model changed between the two calls. Same weights, same prompt.

The answer is in a step that comes after the model has done its work.

The model hands over a list

Each step of next-token prediction ends with a score for every token in the vocabulary, and softmax turns those scores into probabilities. That's where the model stops. Something else has to pick one token from that list, and the rule it uses is called sampling (or decoding). Someone chooses that rule: you, or, on many newer models, the provider.

Let's use a toy list. After "The", say the model gives "nice" 0.5, "dog" 0.4 and "car" 0.1. How should we pick?

Just take the top one?

That's the obvious first idea. It's called greedy decoding: take the most likely token every time. Here, "nice". It's simple, and in theory the same prompt always gives the same text.

But look one step further. Suppose after "The nice" the best word is "woman" at 0.4, and after "The dog" the best is "has" at 0.9. Multiply along each path. "The nice woman" comes to 0.5 × 0.4 = 0.2. "The dog has" comes to 0.4 × 0.9 = 0.36. Greedy took "nice" at step one and never saw the better sentence.

There's a worse problem. Run greedy on GPT-2 with "I enjoy walking with my cute dog" and you get:

I enjoy walking with my cute dog, but I'm not sure if I'll ever be able to walk with my dog. I'm not sure if I'll ever be able to walk with my dog.

That isn't bad luck. Once a phrase has shown up, the model rates it as more likely to come again, and every repeat makes the next one likelier still. It loops.

Then look further ahead

Beam search fixes the first problem. It keeps a few of the most likely partial sentences alive at each step (the "beams") and picks the best whole sequence at the end. On our toy tree it finds "The dog has".

It doesn't fix the loops, though, because it's still chasing the most probable text. It works well when the output is tied closely to the input, like translating or summarising. For stories and chat it repeats.

So maybe the goal is wrong. When researchers measured this on GPT-2 Large in 2019, they found that human writing isn't the most probable text at all. People pick less obvious words all the time. Text that maximises probability comes out bland and loopy, nothing like what a person writes. For open-ended text, hunting for the likeliest output is the wrong target.

Roll a weighted die

The other option is to pick at random, weighted by the probabilities. On our list, "nice" comes up half the time, "dog" 40% of the time and "car" 10%.

And there's the answer to the opening question. The model's probabilities are the same on both calls. The die roll isn't.

Pure random picking stops the loops but brings its own problem: the text drifts into nonsense. Tens of thousands of tokens each have a tiny chance, and together those tiny chances add up. Every so often one gets picked, the text goes somewhere odd, and the model now has to carry on from there. It often can't find its way back.

Here's how the methods compared in that GPT-2 study, across 5,000 generated passages. Perplexity is roughly how surprising the text is.

MethodPassages stuck repeatingPerplexity
Human text0.28%12.38
Greedy73.66%1.50
Beam search (16 beams)28.94%1.48
Pure sampling0.22%22.73
Sampling with the tail cut off (top-p 0.95)0.36%13.13

Greedy and beam search are far too predictable and loop all the time. Pure sampling is more surprising than people are, which in practice means it stops making sense. Only the last row, which drops the long tail and then rolls the die, sits near the human numbers on both. (These are GPT-2 numbers from 2019, on story-like text. Today's chat models have extra training on top, post-training, so read the pattern, not the exact percentages.)

Taming the die

So in practice we roll, but we change the list first. There are two ways.

We can reshape it. Temperature makes the list sharper, so the top tokens win more, or flatter, so the tail gets more chances. Near zero it turns into greedy.

Or we can cut the tail off. Top-p keeps the smallest set of top tokens that covers, say, 95% of the probability. Top-k keeps a fixed number of them.

Both aim at the same spot from the table: random enough not to loop, careful enough to stay on topic.

Is the decoder really to blame for the loops, though? Not everyone thinks so. Another line of research puts repetition down to how the models are trained, and points out that top-k and top-p still repeat sometimes. Both could be partly right. No method wins everywhere.

What this means for an app

The randomness has a real cost. In three months of support requests at one AI startup, about a fifth came from users confused that the model didn't behave the same way each time. That's fine for creative writing and a headache for anything that needs to be consistent.

It can also help. Ask several times and take the most common answer, and accuracy can go up. You pay for it, though: two answers cost about twice as much as one.

And these days you often don't get to set any of this. As of 2026-09, Claude Sonnet 5, and Opus 4.7 and later, reject any non-default temperature, top-p or top-k with an error, and Anthropic's docs point you to instructions in the system prompt instead. On OpenAI's GPT-6 you're told to remove temperature and top-p whenever reasoning is on. The provider picks how to sample, and you steer with the prompt. (What you can still read back about the probabilities is in logprobs.)

So turn it to zero?

If randomness is the reason for different answers, temperature 0 should make them identical. That's the natural fix, and it doesn't fully work.

In 2025, a team ran one open model 1,000 times at temperature 0 on "Tell me about Richard Feynman". They got 80 different completions. All 1,000 agreed for the first 102 tokens. At token 103, 992 runs said "Queens, New York" and 8 said "New York City".

No die was rolled here. What changed was the server. Your request gets batched with other people's, and the arithmetic comes out very slightly different depending on how big the batch is. Batch size depends on how busy the server is, and you can't see that. Every so often the tiny difference is enough to flip which token is on top, and from there the two texts go their own ways. The team made it fully repeatable by rewriting the server's math to give the same numbers at any batch size, which made it slower. That's a change only whoever runs the server can make.

So the full answer to "why did I get a different answer?" has two parts. Usually it's the die. And even with the die put away, it can be the other people sharing the server with you. Write your tests so they don't expect one exact string.

Further reading