Interactive Explainer
Generate, Token by Token
A language model does not write a sentence — it predicts one next token, samples it, sticks it on the end, and does the whole thing again. Press Step to run one round of that loop over a tiny vocabulary, and turn the temperature, top-k and top-p knobs to watch the next-token distribution sharpen, narrow, and change what gets sampled.
Autoregression is a loop
Give the model a prefix of tokens $x_{1:t}$. It runs one forward pass — embed, causal self-attention, feed-forward — and emits a vector of logits, one score per vocabulary word. A softmax turns those into a probability for the next token:
Then the sampled token is appended and the loop repeats with $x_{1:t+1}$. Nothing about the model changes between steps — only the context grows. The interesting choices all live in that last arrow, $x_{t+1}\sim p$: how we turn a distribution into a single word. Temperature $T$, top-k and top-p (nucleus) are three knobs that reshape $p$ before we draw from it.
Run the decoding loop
Pick a prefix, set the knobs, and press Step. Each press shows the next-token distribution over a fixed 12-word vocabulary, samples one token (highlighted), and appends it. The logits are fixed and seeded — a small stand-in model — so the distribution is fully determined by the context and your knob settings.
Three knobs, one trade-off
Every decoding knob is negotiating the same tension: diversity (explore more of the distribution) against focus (stay on the high-probability words the model is confident about). They just cut the distribution differently.
- Temperature rescales the logits before softmax. $T<1$ sharpens toward the top token; $T>1$ flattens toward uniform. It reshapes every probability but removes nothing.
- Top-k keeps only the $k$ highest-probability tokens and zeroes the rest — a hard cap on how many candidates survive, regardless of how the mass is spread.
- Top-p (nucleus) keeps the smallest set of tokens whose probabilities sum to at least $p$. The cut adapts to the shape: a confident step keeps one or two words, an uncertain step keeps many.