GenLucid

LLM · STEP 5

How Does Next-Token Prediction Work?

After a token's vector has passed through every layer (see what transformer layers are), the model's last step converts that final vector into one probability for every token in its entire vocabulary, tens of thousands of candidates scored at once, then samples the actual next token from that distribution rather than always taking the top score.

One-pager titled "Prediction": the prompt "The capital of France is" followed by a question mark, next to a bar chart of candidate tokens (Paris, now, the, a, home) with Paris holding the large majority of probability, beside a panel of the sampling dials, with a REMEMBER band summarizing the mechanism.
One-pager: the context "The capital of France is" scored across candidate next tokens, with Paris winning.

One vector becomes one score per possible token

The final layer's output vector for the last position in the sequence is multiplied against the model's full vocabulary matrix, producing one raw score per token in the vocabulary. Those scores are converted into a proper probability distribution, values between 0 and 1 that all sum to 1, via a softmax function. For the prompt "The capital of France is", a well-trained model puts the overwhelming majority of that probability mass on "Paris", with a long tail of small probabilities spread across everything else.

Sampling, not just taking the top score

Always picking the single highest-probability token (greedy decoding) makes output deterministic but often flat and repetitive. Instead, generation typically samples from the distribution, weighted by those probabilities, so the top candidate is most likely to be chosen but not guaranteed every time. Two controls shape how that sampling behaves: temperature reshapes the distribution before sampling, low temperature sharpens it toward the top candidates (more predictable, more repetitive at the extreme), high temperature flattens it (more varied, more prone to picking a weak candidate). Top-p (nucleus) sampling instead restricts sampling to the smallest set of top candidates whose probabilities sum past a threshold, cutting off the long tail of implausible options regardless of how the distribution is shaped.

Why a confident, wrong answer still happens

The model has no separate mechanism for checking truth; it only ever asks "which token is statistically likely to come next, given everything the training data showed for similar contexts." When the true answer was well-represented in training, that statistical likelihood and factual correctness line up closely. When it wasn't, the model still produces a probability distribution and still samples fluently from it, which is exactly why hallucinated text so often reads as confident and grammatically clean: fluency and correctness are computed by two different things, and only one of them is what this step actually optimizes.

FAQ

Why does the same prompt sometimes give a different answer each time?
Unless temperature is set to zero (pure greedy decoding), generation samples from a probability distribution rather than always taking the top-scored token, so a lower-probability but still plausible candidate can get picked, producing a different output from the same prompt on a different run.
Does the model consider the whole rest of its answer before picking the next token?
No. Each token is predicted independently, based only on everything generated so far, with no lookahead into what comes after. That's the mechanism behind the generation loop (see how the generation loop works): the model commits to one token at a time and only sees the consequence of that choice on the next pass.

Sources

Related

Last updated 2026-09-16