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 probability distributionprobability distribution: A list of possible outcomes with how likely each one is. The chances add up to 1. Ch 2: a chance for every possible next piece of text, all adding up to 100%. Choosing the word is a separate step, called samplingsampling: Picking one outcome at random, where likelier outcomes get picked more often. Ch 2, 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.
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:
Temperature, written down
Each candidate token gets a raw score , called a logit. Softmax turns the scores into probabilities after dividing each one by the temperature :
where is the constant 2.718… and the sum runs over all 50,257 tokens, so the add up to 1. For any two tokens, : a gap in scores counts for more at a low and for less at a high one. At the model’s own probabilities come out unchanged. As 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 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 of most likely tokens with , 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:
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.
- : 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
- Ari Holtzman et al. (2020). The Curious Case of Neural Text Degeneration. ICLR 2020.
- Angela Fan et al. (2018). Hierarchical Neural Story Generation. ACL 2018.
Sources
- Ari Holtzman et al. (2020). The Curious Case of Neural Text Degeneration. ICLR 2020.
- Angela Fan et al. (2018). Hierarchical Neural Story Generation. ACL 2018.
Sources
- Ari Holtzman et al. (2020). The Curious Case of Neural Text Degeneration. ICLR 2020.
- OpenAI (2026). API reference: Create chat completion. OpenAI. Accessed October 26, 2026.
- Angela Fan et al. (2018). Hierarchical Neural Story Generation. ACL 2018.
- Horace He and Thinking Machines Lab (2025). Defeating Nondeterminism in LLM Inference. Thinking Machines Lab: Connectionism. Accessed October 25, 2026.