In this lesson: how a language model picks each next token. It doesn't have one answer ready; it has a chance for every token in its vocabulary, and settings such as temperature, top-k and top-p decide how one is drawn. You'll see why the same model can get stuck in a loop or drift into nonsense depending only on those settings. Read the lesson and take the 5-question quiz: each question has a hint if you get stuck, and its explanation shows once you get it right (all of them at full marks). Pass the quiz to finish the lesson and earn its badge.
1. Same start, two settings
Ask a chatbot the same question twice and you may get two different answers. Before you read on, guess: if we make a model always take its top choice, will two runs differ?
We gave distilgpt2 (a small model that learned only English; more in section 6) the start "After the last exam, my class decided to" and let it write 20 tokens, twice with temperature 0 and twice with temperature 1.2. At temperature 0 it takes the top token at every step. At 1.2 it draws each token at random, by its chance, from its whole vocabulary: we switched off the cut the software makes by default, which keeps only the top 50 tokens (more in section 5). We also fixed the random draws, so the two 1.2 runs below come out the same each time we repeat them; left to chance, every 1.2 run is different. It wrote:
| Setting | Run | What it wrote |
|---|
| Temperature 0 | 1 | take the exam. I was very excited about it. I was very excited about it. I was |
| Temperature 0 | 2 | take the exam. I was very excited about it. I was very excited about it. I was |
| Temperature 1.2 | 1 | skip each seven questions pretty much... is "tree legs diffuse the spill that flows far and wide?", |
| Temperature 1.2 | 2 | upset my debt. I was happy for them but needed to cede my assistance. I took plenty |
At temperature 0 both runs are exactly the same, stuck in a loop. At 1.2 they are varied, and soon not making sense. Same model, same start; only the way of picking changed.
Temperature 0 loops, temperature 1.2 drifts
2. Scores for every token: logits
At each step the model looks at all the tokens so far and outputs one number for every token in its vocabulary: 50,257 numbers. These are logits, raw scores. For "The school canteen ran out of", " food" gets about -55.8 and " money" -55.9. They aren't probabilities (they're negative!); only the differences between them matter.
A raw score for each of 50,257 tokens
3. From scores to chances: softmax
Softmax turns logits into probabilities (chances) that add up to 1: raise e (a fixed number, about 2.718) to the power of each score, then divide by the total. With a temperature T (next section):
pi=∑jezj/Tezi/T
Here zi is the logit of token i, and the sum at the bottom runs over all 50,257 tokens, so the chances add up to 1. For the canteen sentence at T=1: " food" 0.148, " money" 0.142, " cash" 0.101, " water" 0.034, " gas" 0.029. Even the top token is under 15 %. The model has no single next word ready, only a spread of guesses.
We got these numbers by running the model once on the canteen sentence, reading its 50,257 scores for the next token and putting them through this formula.
Softmax turns scores into chances that add to 1
4. Temperature
Dividing the logits by a temperature T before softmax reshapes the spread. T below 1 sharpens it: at 0.5, " food" rises from 0.148 to 0.385, " money" to 0.357, " cash" to 0.181, and everything below them shrinks (" water" 0.034 → 0.020). T above 1 flattens it, so unlikely tokens get picked more often. That is variety, not a smarter model: it knows nothing more at 1.2 than at 0.5.
The order never changes, only the gaps. Dividing every logit by the same positive number can't swap two of them. So a line you might read, "lowering the temperature changes which token is ranked first", is wrong: at 0.5 the top token is still " food". Temperature 0 is the limit: always take the top token. That is called greedy decoding (the software we used refuses a temperature of exactly 0 while sampling, so you switch sampling off instead).
Temperature changes the gaps, not the order
5. Cutting the tail: top-k and top-p
Thousands of tokens each have a tiny chance, and together they add up. For the canteen sentence the top 5 tokens hold only 0.455 of the chance, the top 100 hold 0.759, and you need 671 tokens to reach 0.9. Flattening with 1.2 and drawing from all of them, with no cut, is how section 1 drifted into nonsense. Two common cuts:
- Top-k: keep only the k most likely tokens, share their chances out again, sample from those. Top-k = 5 always keeps 5; the software we used keeps 50 unless you change it.
- Top-p (also called nucleus sampling: it keeps only the likely core): take the most likely tokens in order until their chances add up to at least p, say 0.9; keep those, share their chances out again, sample. How many that is depends on how sure the model is.
So "top-p 0.9 means it always keeps exactly 9 tokens" is wrong twice: a count of tokens is top-k, and top-p's count changes. For distilgpt2, a small model that is rarely sure, top-p 0.9 kept 331 tokens after "After the last exam, my class decided to", 671 after the canteen sentence and 844 after "The capital of France is". Even after "Two plus two equals" it kept 700, and gave " four" only 1 % (17th place): a small model is rarely sure, even of a sum.
Top-k cuts by count, top-p by chance
6. Why greedy repeats, and which setting to use
Greedy always takes the top token. Once "I was very excited about it." is written, writing it again becomes likely, so it does, again and again. Sampling breaks the loop, and so does a repetition penalty that lowers the scores of tokens already used.
That gives you the two failures to plan for when you build. A vocabulary helper that writes one example sentence per word wants to be reliable: a low temperature (or greedy with a repetition penalty) keeps it to the likeliest words, but likeliest isn't always right, so check its sentences; its other risk is dull, repeated ones. A silly story for the class party wants variety: a higher temperature with top-p, and its risk is drifting into nonsense, so read it before you share it.
A note on the model: distilgpt2 is small and learned only English, so we prompt it in English. Chat model services often offer the same kind of settings: DeepSeek's chat API, for example, accepts a temperature and a top-p setting, though depending on the model and mode it may ignore them (in thinking mode, where the model reasons step by step before it answers, the temperature has no effect).
Greedy loops, sampling drifts: pick per task