Part IV ends with a trained model. Given a context it produces a probability distribution over the next token, which is Chapter 10’s task at transformer scale.
A distribution is not an answer. Something has to choose.
That choice is decoding, and it is made outside the model, after training is over. Nothing in this chapter changes a single weight.
Two levers exist at that point. Decoding decides how a token is picked from the distribution. Prompting decides what the distribution is conditioned on.
Both are cheap, both are reversible, and both change the output more than most people expect.
The obvious rule is to take the highest-probability token at every step.
It fails, and the failure is easy to construct. Take a tiny model whose whole next-token tree is written out.
| prefix | choices | greedy takes |
|---|---|---|
| (start) | the 0.40, a 0.35, one 0.25 | the |
| the | cat 0.55, dog 0.45 | cat |
| the cat | sat 0.60, ran 0.40 | sat |
Greedy returns the cat sat, with probability .
Now rank every three-word sequence the model can produce.
| rank | sequence | probability |
|---|---|---|
| 1 | a bird sang | 0.2992 |
| 2 | the cat sat | 0.1320 |
| 3 | the dog sat | 0.0900 |
| 4 | the dog ran | 0.0900 |
| 5 | the cat ran | 0.0880 |
The best sequence is more than twice as likely as the one greedy found.
Greedy went wrong at the first step. It took the at over a at , and could not see that a leads to bird at and then sang at .
That is the general shape of the failure. A high-probability token can sit in front of a low-probability continuation, and a rule that looks one step ahead cannot know.
Beam search keeps the best partial sequences at every step instead of one.
| width | best found | probability | optimal? |
|---|---|---|---|
| 1 | the cat sat | 0.1320 | no |
| 2 | a bird sang | 0.2992 | yes |
| 3 | a bird sang | 0.2992 | yes |
| 5 | a bird sang | 0.2992 | yes |
Width is greedy by definition. Width already finds the optimum here, and wider beams cost more for nothing.
Beam search is not guaranteed to find the best sequence. It searches more of the space than greedy and less than all of it, and that is the honest description.
It also has a known bias. Because it optimises total probability, it favours short, safe, generic output. That is acceptable in translation, where there is a right answer. It is a problem in open-ended generation, where the safest continuation is usually the dullest.
Sampling introduces variety, and temperature controls how much.
Divide the logits by before the softmax, which is the same as raising each probability to the power and renormalising.
| the | a | one | some | any | entropy | |
|---|---|---|---|---|---|---|
| 0.0 | 1.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 | 0.0000 |
| 0.5 | 0.9903 | 0.0067 | 0.0022 | 0.0005 | 0.0001 | 0.0904 |
| 0.8 | 0.9239 | 0.0408 | 0.0202 | 0.0085 | 0.0036 | 0.5235 |
| 1.0 | 0.8500 | 0.0700 | 0.0400 | 0.0200 | 0.0100 | 0.9094 |
| 1.5 | 0.6585 | 0.1246 | 0.0858 | 0.0541 | 0.0341 | 1.7069 |
| 2.0 | 0.5211 | 0.1495 | 0.1130 | 0.0799 | 0.0565 | 2.1524 |
leaves the model’s own distribution alone. Below it sharpens, and at it is greedy, with zero entropy because nothing is left to choose. Above it flattens.
Both ends have a characteristic failure. Low temperature gives repetitive, safe text. High temperature gives varied text that drifts off topic and eventually stops making sense.
Sampling from the full distribution has a problem. A vocabulary of tokens holds a great many implausible options, and their combined tail probability is not negligible.
Top- keeps the most likely tokens and renormalises. Top-, or nucleus sampling, keeps the smallest set whose probability mass reaches .
The difference shows the moment you try both on two distributions of different shape.
| distribution | top four | entropy |
|---|---|---|
| peaked | the 0.85, a 0.07, one 0.04, some 0.02 | 0.9094 |
| flat | red 0.14, blue 0.13, green 0.13, black 0.12 | 2.9977 |
Now apply a fixed to both.
| peaked: kept | mass kept | flat: kept | mass kept | |
|---|---|---|---|---|
| 1 | 1 | 0.8500 | 1 | 0.1400 |
| 2 | 2 | 0.9200 | 2 | 0.2700 |
| 5 | 5 | 0.9900 | 5 | 0.6400 |
| 8 | 7 | 1.0000 | 8 | 1.0000 |
Read . On the peaked distribution it admits four tokens the model had all but ruled out. On the flat one it discards three tokens that were as good as the ones it kept.
The same is too loose on one shape and too tight on the other, because cannot see the shape.
Top- can.
| peaked: kept | mass | flat: kept | mass | |
|---|---|---|---|---|
| 0.50 | 1 | 0.8500 | 4 | 0.5200 |
| 0.90 | 2 | 0.9200 | 8 | 1.0000 |
| 0.95 | 3 | 0.9600 | 8 | 1.0000 |
| 0.99 | 5 | 0.9900 | 8 | 1.0000 |
At the peaked distribution keeps two tokens and the flat one keeps eight. The candidate set now follows the model’s own confidence, which is exactly what a fixed could not do.
That is why nucleus sampling became the default.
| strategy | the | a | tokens kept | entropy |
|---|---|---|---|---|
| greedy | 1.000 | 0.000 | 1 | 0.0000 |
| temperature 0.7 | 0.954 | 0.027 | 7 | 0.3467 |
| pure sampling, | 0.850 | 0.070 | 7 | 0.9094 |
| temperature 1.5 | 0.658 | 0.125 | 7 | 1.7069 |
| top-, | 0.885 | 0.073 | 3 | 0.6219 |
| top-, | 0.924 | 0.076 | 2 | 0.3882 |
Every row is the same model, and none of them changed a weight.
Which row to use follows from the task. A factual answer wants a low-entropy row. A story wants one of the middle ones.
Try it yourself.
code/worked_examples/decoding.pyproduces every table here.--beamshows greedy losing and beam recovering,--tempsweeps the temperature,--truncatecontrasts top- with top- on both shapes, and--compareprints the last table.
The second lever changes what the model is conditioned on.
Chapter 17 noted that at sufficient scale a model can perform a task from a description in its input, with no gradient update . That capability is what makes prompting a technique rather than a formatting detail.
Zero-shot gives an instruction and nothing else. Few-shot adds a handful of worked examples in the prompt. Chain-of-thought asks the model to produce its reasoning before its answer .
The third is the interesting one, and its mechanism is not mysterious.
A model produces one token at a time, each conditioned on what came before. Asking for an answer directly gives it one forward pass to reach the conclusion. Asking for the steps first gives it many, and each step becomes context for the next.
So chain-of-thought is not the model thinking harder. It is the model being given room to compute.
Four things, in rough order of reliability.
Be specific about the output. Name the format, the length and the allowed values. Most prompt failures are underspecification, not model failure.
Give examples rather than adjectives. Two demonstrations of the format you want beat a paragraph describing it.
Ask for steps on multi-step problems. Free, and it helps most where the task is arithmetic or logical.
Put the constraints last. The instruction nearest the generation point tends to dominate, which is a property of the architecture rather than a rule of style.
Prompting selects from what the model already has. It does not add knowledge the model lacks, and it does not reliably remove behaviour the model learned.
Two consequences follow, and they set up the next three chapters.
If the model does not know a fact, no prompt will supply it. That is Chapter 20.
If the model’s default behaviour is wrong, prompting patches it case by case and never fixes it. That is Chapter 19.
Four ways to change what a model does, cheapest first.
| method | changes | cost | when |
|---|---|---|---|
| decoding | the sampling rule | none | always available |
| prompting | the conditioning | none | first thing to try |
| retrieval | the context | low | missing knowledge |
| fine-tuning | the weights | high | wrong default behaviour |
Work down that list, not up. A great deal of expensive fine-tuning has been spent on problems a decoding parameter would have solved.
The order also has a diagnostic use. If prompting cannot get close, the problem is usually knowledge or behaviour rather than phrasing. That tells you which chapter to read next.
report in-context learning. introduce chain-of-thought prompting. is the nucleus sampling paper. Its account of why pure sampling degenerates is the clearest available. cover decoding alongside machine translation, where beam search has its longest history.
Break greedy. Construct a next-token tree of depth three in which greedy decoding is at least three times worse than the optimal sequence. State the general property your construction relies on.
Beam width. On the tree in this chapter, find the smallest beam width that recovers the optimum. Then modify the tree so that width is not enough, and say what you had to change.
Temperature by hand. Take the distribution and compute the result at , and . Confirm each sums to one, and compute the entropy of each.
Where fails. Construct one distribution where is far too permissive and another where it is far too restrictive. Then find a single value of that behaves sensibly on both, and explain why one parameter can serve both shapes.
Chain-of-thought, mechanically. Explain why asking a model for its reasoning before its answer changes what it can compute, in terms of tokens and forward passes. Then name a task where it should not help, and say why.
Pick a level. For each of these, name the cheapest level of the steering hierarchy that could work, and justify it: (a) output is too repetitive; (b) the model does not know your company’s refund policy; (c) the model answers in American spelling and you need British; (d) the model refuses a legitimate request in your domain.