Learning LabExplorable explanations
← All artifacts
Machine Learning

Next-Token Prediction & Sampling

A language model writes one token at a time, drawing each from a probability distribution and feeding its own choice back in. Turn the temperature, top-k, and top-p knobs and watch the same distribution reshape under your hands.

language-modelssamplingsoftmaxautoregressive
LiveInteractive · drag, toggle, run it
Machine Learning

Next-Token Prediction & Sampling

A language model never plans a sentence. It looks at the running text, produces a probability over every word that could come next, picks one, appends it, and then reads its own output back in to predict again. The knobs people argue about, temperature, top-k, and top-p, are just different ways of choosing from that one distribution. Run the loop by hand below and feel each knob bend the odds.

What the model is here. A real language model is a large neural network, far too heavy to ship into a browser. So this page trains a small trigram model on the corpus below: it counts how often each word follows the two words before it, smooths the counts, and turns them into next-token probabilities. The neural net and the trigram differ enormously in how good those probabilities are, but the loop is identical. Predict a distribution, pick a token, append, repeat. The trigram is a tiny stand-in for the part you cannot see; the generation mechanism is the real thing.
corpus
146 tokens trained, 71 distinct words. The model only knows what appears in this text, which is why short corpora go in circles.
the running text
Empty. The context is two start markers. Sample or pick the first token to begin.
0 tokens generatedcontext window: ▁start ▁start → ?
distribution over the next token
71 of 71 words kept
base probability (from counts)after temperature / top-k / top-p
the
29.6%
.
1.0%
a
1.0%
afternoon
1.0%
again
1.0%
against
1.0%
air
1.0%
and
1.0%
at
1.0%
behind
1.0%
beyond
1.0%
bright
1.0%
top word: the at 29.6%survivors sum to 1.00
the knobs
Divide the log-probs by T, then softmax. Below 1 sharpens toward the top word; above 1 flattens toward uniform.
Keep only the k most probable words, drop the rest, renormalize. Zero means keep all.
Keep the smallest set of words whose probabilities add up past p, drop the long tail, renormalize.
run the loop
seed7Same seed plus same knobs plus same corpus reproduces the same text exactly.
what the knobs trade

Temperature rescales confidence. The raw scores are logits, here the log of each count-based probability. Dividing them by a small T before the softmax stretches the gaps, so the top word dominates and the text turns rigid and repetitive. A large T squashes the gaps toward uniform, so rare words slip in and the text drifts into nonsense.

Top-k and top-p both prune the tail before sampling. Top-k keeps a fixed number of candidates; top-p keeps however many it takes to cover a fixed share of the probability mass, so it adapts: a confident step keeps few words, an uncertain step keeps many. After either prune, the survivors are renormalized so they sum back to one.

The whole game is a trade between coherence and diversity. Low temperature with tight pruning is safe and dull; high temperature with no pruning is surprising and often broken. Useful sampling lives in the middle.

trigram counts with add-0.05 smoothing · mulberry32 seeded sampler · the loop, not the model, is the point