Inference

Token decoding

A model does not produce an answer, it produces a distribution over next tokens. Move temperature, top-k and top-p and watch the fan of possible continuations widen or collapse.

Preset
Mode
Temp 0.70
Top-p 0.90
Top-k off
NEXT TOKENThe cat sat on the ...mat83%floor17%sofacutwindowsillcutroofcutkeyboardcutquantumcut200 SAMPLED CONTINUATIONSmat.ItThe,ItThefloor.ItThe
8/ 200
Distinct futures
44%
Most common
0.66bits
Entropy
5of 7
Tokens cut

Temperature 0.7 with nucleus sampling. Varied but still coherent, which is why it is the common default.

Ask why a model gave you a different answer the second time and the honest reply is that it never had one answer. At every step it produces a probability distribution over the whole vocabulary, and something has to turn that distribution into a single token. That something is the sampler, and it is configuration, not intelligence.

The left panel is one such distribution. The right panel is what happens when you sample it two hundred times and follow each continuation forward. Greedy decoding collapses the fan to a single line: the most likely token, every time, which is why it reads as competent and dull. Raising temperature flattens the distribution, so tokens that were nearly impossible become merely unlikely, and the fan spreads.

Top-p is the control that stops that becoming nonsense. It keeps only the smallest set of tokens whose probability sums to p, then renormalises. Watch the cut tokens on the left as you drag it: at 0.9 the absurd tail is gone, and the model can be varied without being unhinged. Set top-p back to 1.0 at temperature 2 to see the alternative.

The order matters and is easy to get backwards. Temperature is applied first, then top-k, then the nucleus is taken from the already-flattened distribution. That makes top-p a self-adjusting brake rather than a fixed one: raising temperature moves mass into the tail, so top-p admits more tokens, not fewer. The "tokens cut" counter drops as you heat things up, which is the opposite of what most descriptions imply, and it is why a temperature increase usually needs a tighter p to stay in the same place.

Top-k is the blunter sibling. It keeps a fixed number of candidates regardless of how confident the model is, which means it cuts too little when the distribution is flat and too much when the model is certain. That is why nucleus sampling largely replaced it, and why the two are applied in that order when both are on.

The logits here are hand-authored to make the tail obviously ridiculous rather than merely unlikely. The behaviour they demonstrate is not: any distribution with a heavy head and a long tail responds this way, which is why these two knobs appear in every inference API you will use.

The maths

Softmax with temperature
pi  =  exp(zi/T)jexp(zj/T)p_i \;=\; \frac{\exp(z_i / T)}{\sum_j \exp(z_j / T)}

The temperature slider is T. Dividing the logits by a larger T pushes them closer together before exponentiating, so the distribution flattens. As T approaches zero the largest logit dominates completely and sampling becomes argmax.

Nucleus (top-p) set
Vp  =  argminS  Ss.t.iSpi    pV_p \;=\; \arg\min_{S} \; |S| \quad \text{s.t.} \quad \sum_{i \in S} p_i \;\ge\; p

The smallest set of tokens whose probability sums to at least p. Because it is taken after temperature has been applied, raising T moves mass into the tail and the set gets larger, not smaller.

Renormalisation after masking
pi  =  pi1[iVp]jVppjp'_i \;=\; \frac{p_i \, \mathbb{1}[i \in V_p]}{\sum_{j \in V_p} p_j}

Cut tokens get probability zero and the survivors are rescaled to sum to one again. This is what the solid bars show; the faint bars behind them are the distribution before the cut.

Entropy of the sampling distribution
H  =  ipilog2piH \;=\; -\sum_i p'_i \log_2 p'_i

The one-number summary of how undecided the model is, in bits. Zero means one token is certain; log2(n) means all n candidates are equally likely.

Related terms

More visualisations

Building with language models?

These explainers come out of the work. If you want the same thinking applied to your own system, that is what I do.

See how I can help