LLM Kitchen
Explanations

Part I · Prediction

Ch 2. Probability and sampling

The model doesn’t choose

A language model’s output isn’t a word. It’s a : a chance for every possible next piece of text, all adding up to 100%. Choosing the word is a separate step, called , and it has its own settings.

Below are GPT-2’s ten most likely next words after The cat sat on the. Spin the wheel, then move the sliders and watch the bars change.

Try it

Temperature, top-k, and top-p

Recorded · GPT-2 small

Temperature 1: the model's own probabilities.

Chance of each next word with these settings
Next tokenProbability
·floor 18%
·bed 15%
·couch 13%
·ground 12%
·edge 11%
·bench 7.4%
·table 7.4%
·sofa 6.8%
·other 4.7%
·back 4.5%
·floor·bed·couch·ground·edge
  1. ·floor 18%
  2. ·bed 15%
  3. ·couch 13%
  4. ·ground 12%
  5. ·edge 11%
  6. ·bench 7.4%
  7. ·table 7.4%
  8. ·sofa 6.8%
  9. ·other 4.7%
  10. ·back 4.5%

Simplified: to keep the picture readable, the widget only uses GPT-2’s ten most likely next words and rescales them to add up to 100%. The real model chooses from all 50,257 options.

Temperature

The simplest way to choose is to always take the most likely word. That gives the same text every time, and with big models it tends to be dull and to repeat itself.[1]

Temperature controls how adventurous the pick is. A low temperature makes the likely words even likelier. A high one evens out the chances, so unusual words get picked more often. At 0, the model always takes the top word.

The simplest choice is to always take the most likely word. That’s called greedy decoding, and it gives the same output every time. With large models, always picking the top word tends to produce dull text that can get stuck repeating itself.[1][1]

Temperature controls how adventurous the pick is. Below 1, it makes the likely words even likelier. Above 1, it evens the chances out, so unusual words get picked more often. At 0 it becomes greedy: always the top word.

Under the hood, the model gives every word a raw score, and the scores are divided by the temperature before they become probabilities. Try it:

Try it

Divide by the temperature

Recorded · GPT-2 small
Each word's score, divided by the temperature, and the probability that results
WordScore÷ 1.00Probability
·floor−2.57−2.57 23%
·bed−2.73−2.73 20%
·couch−2.92−2.92 17%
·ground−2.95−2.95 16%
·edge−3.04−3.04 15%
·bench−3.45−3.45 9.7%

The top two scores are 0.16 apart. Divided by 1.00, the gap becomes 0.16, so ·floor is 1.17× as likely as ·bed.

Each divided score s becomes es, and those are shared out to add up to 100%. A small temperature stretches the gaps, so the top word pulls ahead; a large one shrinks them, so the chances even out. Scores here are the logs of GPT-2’s probabilities, using only its top 6 words. Its real raw scores differ from these by the same amount for every word, which doesn’t change the result.

Temperature, written down

Each candidate token i gets a raw score zi, called a logit. Softmax turns the scores into probabilities after dividing each one by the temperature T:

pi=ezi/T∑jezj/T

where e is the constant 2.718… and the sum runs over all 50,257 tokens, so the pi add up to 1. For any two tokens, pi/pj=e(zi−zj)/T: a gap in scores counts for more at a low T and for less at a high one. At T=1 the model’s own probabilities come out unchanged. As T gets close to 0, all the probability goes to the top score, which is why 0 means greedy. Adding the same number to every score changes nothing; only the gaps matter.

The recorded runs further down divide GPT-2’s raw scores by T and draw from all 50,257 tokens; the widget’s cut to ten is only for the picture. Two other controls, next, cut off the long tail of unlikely tokens instead of flattening it. In the widget they apply after the temperature, top-k first, and what’s left is rescaled to add up to 100%. So the order matters: a higher temperature flattens the chances first, and top-p then needs more words to reach p. OpenAI’s documentation suggests changing the temperature or top-p, not both, since each already decides how far down the list a pick can reach.[2]

Top-k and top-p

Top-k is a simpler control: only the k most likely words are allowed, and the rest are set to zero.[2][2][3] It stops the model from ever picking something very unlikely, which is where nonsense tends to come from.

Top-p, also called nucleus sampling, cuts by chance instead of by count: it keeps the most likely words until their chances add up to p, and drops the rest.[1][1][1] So it keeps a few words when the model is sure and many when it isn’t. In the widget, top-p 0.5 keeps four words, floor, bed, couch, and ground, whose chances add up to 58%.

Written down, top-p keeps the smallest set V of most likely tokens with ∑i∈Vpi≥p, then divides each kept chance by that sum so they add up to 1.

The same prompt, three ways

Here is GPT-2 continuing the same prompt for 16 more pieces of text, at three temperatures:

Recorded runs

Same prompt, three temperatures

Recorded · GPT-2 small
Temperature 0

The cat sat on the floor, and the cat was still asleep. "I'm sorry,…

Temperature 0.7

The cat sat on the bench, his fur slicked back, his brow furrowed, and he…

Temperature 1.5

The cat sat on the shoulders quite forward towards Ten and his friends checked up beautifully steHot Also Wand Lake…

Run the three temperatures again, or on your own beginning, with GPT-2 small live. Choose a model tier to run a real model in your browser.

At temperature 0 the text is sensible but predictable, and run again on the same computer it comes out the same every time. At 0.7 it varies but stays readable. At 1.5 it drifts into nonsense, because unlikely words keep getting picked and each one makes the next guess harder.

Temperature 0 on a busy server

On a hosted model, temperature 0 isn’t always repeatable. When researchers asked Qwen3-235B for the same 1,000-token answer 1,000 times at temperature 0, they got 80 different answers, all identical for the first 102 tokens. The server groups many people’s requests into one batch, and the size of the batch changes the order in which the model’s numbers are added up. With rounding, a different order gives slightly different scores, so when two tokens are almost tied, a different one can come out on top.[4]

Recipe card

Recap

  • A model outputs a probability distribution; sampling picks the actual word.
  • Temperature sharpens (below 1) or flattens (above 1) the distribution. Temperature 0 always picks the top word.
  • Top-k only allows the k most likely words.
  • Top-p only allows the most likely words until their chances add up to p.
  • pi=ezi/T/∑jezj/T: temperature divides the raw scores before softmax.

Next question: a computer can’t read. So how does a model get from letters on a page to something it can calculate with?

Sources

  1. Ari Holtzman et al. (2020). The Curious Case of Neural Text Degeneration. ICLR 2020.
  2. Angela Fan et al. (2018). Hierarchical Neural Story Generation. ACL 2018.

Sources

  1. Ari Holtzman et al. (2020). The Curious Case of Neural Text Degeneration. ICLR 2020.
  2. Angela Fan et al. (2018). Hierarchical Neural Story Generation. ACL 2018.

Sources

  1. Ari Holtzman et al. (2020). The Curious Case of Neural Text Degeneration. ICLR 2020.
  2. OpenAI (2026). API reference: Create chat completion. OpenAI. Accessed October 26, 2026.
  3. Angela Fan et al. (2018). Hierarchical Neural Story Generation. ACL 2018.
  4. Horace He and Thinking Machines Lab (2025). Defeating Nondeterminism in LLM Inference. Thinking Machines Lab: Connectionism. Accessed October 25, 2026.