Skip to content

Predicting the Next Token

Score every token in the vocabulary, turn the scores into a distribution, and sample the next one

Quick recap
  • A language model assigns a probability to the next token given the tokens before it.
  • Logits become probabilities through softmax; cross-entropy scores the target token.
  • Temperature changes the sharpness of the distribution, not whether the sentence is true.

This lesson follows Representation Learning & Embeddings and leads into Attention. The probability tools live in Probability & Statistics.

1. The next-token game

Read this: the cat sat on the ____. Most people expect mat, floor, or sofa. A language model plays the same game, but it does not return one word. It returns a probability for every token in its vocabulary.

That vocabulary can contain tens of thousands to millions of tokens. A token is not always a word; it may be a word piece, a punctuation mark, or a byte sequence. The model conditions on the tokens it has already seen:

The conditional distribution

The model is not retrieving a sentence from a database. It computes a representation of the context, then scores every candidate token. The next-token game is simple to state and broad enough to train on ordinary text without hand-written labels.

The animation shows only three candidates. A real model scores the whole vocabulary, including tokens that would be absurd here. The distribution is normalized across all of them.

2. Scores to probabilities

The final layer produces one raw score, called a logit, per token. Logits can be any real number. Softmax exponentiates them and divides by the total, so every output is positive and the outputs sum to 1:

Softmax with temperature

z_i is the logit for token i, T is the temperature, and p_i is the resulting probability. Adding a constant to every logit does not change the result, because softmax only cares about differences.

Training uses cross-entropy: the negative log probability assigned to the correct next token. A confident wrong prediction is punished hard; a confident correct one is cheap. If the target token has probability 0.9, the loss is −ln 0.9 ≈ 0.105:

Cross-entropy for one token

The loss is low when the model gives the target a high probability. It is not a measure of whether the sentence is true, original, or safe.

3. Temperature

Temperature divides the logits before softmax. A temperature below 1 makes the distribution sharper; a temperature above 1 makes it flatter. At T = 1 the model uses its raw scores.

With logits (2, 1, 0), temperature 0.5 pushes the top candidate to 86.7%, while temperature 2 lowers it to 50.6%:

Cold
Warm

Temperature is a decoding control, not a truth control. It changes how often a model chooses a less likely token. It does not add evidence, check a fact, or make a bad model reliable.

Try it

If a model is wrong at temperature 1, will lowering the temperature make it correct?

4. Worked example

Use three candidates, mat, floor, and moon, with logits 2, 1, and 0. The base distribution is 66.5%, 24.5%, and 9.0%.

Logits
Probabilities

Worked example: probabilities and loss

tokenlogitT = 0.5T = 1T = 2−log p at T = 1
mat286.7%66.5%50.6%0.408
floor111.7%24.5%30.7%1.408
moon01.6%9.0%18.6%2.408

If the true next token is mat, the loss is 0.408. If it is floor, the loss is 1.408. The second is larger because the model gave that token only 24.5% probability:

The temperature columns all sum to 100%. Cooling concentrates mass on the top token; warming spreads it out. Neither column checks whether moon is a sensible thing to sit on.

5. Training

Training data are just text. At every position, the model sees the preceding tokens and is asked for the next one. The correct token is known from the text itself, so no human labels are needed. This is self-supervised learning.

For a sequence, the model computes a distribution at every position and averages the cross-entropy. Gradients update the embeddings, attention weights, and the output layer together. The model gets better at the training text; whether that transfers to a new task is a separate question.

During training the model can see the true previous tokens at every position. During generation it sees its own previous choices. That difference is one reason a model can look better in teacher-forced training than in a long free-running generation.

6. Sampling and limits

Given a distribution, generation needs a choice. Greedy decoding takes the highest-probability token every time. Sampling draws from the distribution, so a lower-probability token can be chosen. Top-k and top-p restrict the pool before sampling.

Greedy text often repeats itself; sampling often wanders. Neither is “the model’s answer” in a metaphysical sense. Both are decoding procedures applied to the same conditional distribution.

A language model can produce fluent falsehoods because fluency and truth are different objectives. It can also repeat training data, leak memorized text, or reflect the biases of its corpus. A low perplexity means the model predicts held-out text well under its own distribution; it does not prove factual reliability.

The next lesson opens the context-mixing step. Attention is how a token decides which earlier tokens to read.

Next on the recommended path: Attention

7. Going deeper

Perplexity is the exponential of the average cross-entropy per token. It is useful for comparing models on the same tokenization and data, but it is not comparable across different vocabularies without care, and it is not a user-facing quality score.

Tokenization changes the game. A character-level model has a small vocabulary and long sequences; a subword model trades sequence length for vocabulary size. The same text can become different token sequences in different models.

Scale changes the loss curve, but it does not remove the distribution shift between training text and a new deployment. Retrieval, tool use, and fine-tuning are different ways to add information or behavior after pretraining; none of them is the same as retraining on new facts.

8. Practice

Try it

Why does softmax not change if you add 10 to every logit?

Try it

A model assigns 0.9 to the correct next token. What is the cross-entropy loss?

Try it

Does a temperature of 0.1 make the model more factual?

9. Resources

The MIT chapter connects language modeling to transformers. The Illustrated GPT-2 post shows how a decoder produces one token at a time and how the output distribution is used. Both links were opened and checked for this lesson.

Continue the track

The mathematics behind this topic: Probability & statistics.

All Machine Learning lessons

Site soundtrack