Skip to main content

Entropy: uncertainty over the next token

Reading an output distribution without confusing it with truth

Lesson objective. Compare the entropy of two distributions over the same vocabulary and distinguish that average from one observed token’s surprisal.

Lesson skeleton

See the path

Review this lesson

On the first turn of the Roman example, “They” gets 60%. How can we summarize the spread of probabilities across all candidates? Entropy measures how spread out the distribution obtained from the logits is for that prefix, before a token is selected or observed. Factual correctness and model reliability require separate evaluation.

TipThe one-minute mental model
  • Predictive entropy: \(H(p)=-\sum_v p(v)\log p(v)\) summarizes the spread of the entire next-token distribution for a given prefix.
  • Observed surprise: \(-\log p(x_t\mid x_{<t})\) concerns the token that was actually observed; it is not the entropy of the distribution.
  • Bounds: for a finite vocabulary of size \(V\), \(0\leq H(p)\leq\log V\) nats (natural logarithm).
  • Interpretation: low entropy guarantees neither factual correctness nor calibration; tokenizer, context, and temperature affect the quantity being measured.

Quick question: can you distinguish uncertainty before observing a token from surprise after observing a target?

Predictive distributionp(next token | context)

Two different questions depending on whether a token has been observed

Before observation · Entropy H(p)Spread of the full distribution: predictive uncertainty

After observation · Surprise −log p(y)Score of the observed token; see likelihood

Given a target distribution q: cross-entropy H(q, p) · Compare prediction and target

For the prefix “What have the Romans ever done for us?”, entropy describes how spread out the distribution is over candidate next tokens. It does not measure whether a continuation is true, or the probability of the entire response to come.

Entropy of the next-token distribution

Given a prefix \(x_{<t}\), the model produces a categorical distribution over its vocabulary:

\[ p_\theta(v\mid x_{<t}),\qquad v\in\mathcal V. \]

Its entropy, using natural logarithms, is (with the convention \(0\log 0=0\)):

\[ H_t=-\sum_{v\in\mathcal V}p_\theta(v\mid x_{<t}) \log p_\theta(v\mid x_{<t}). \]

A distribution concentrated on a few tokens generally has lower entropy than a spread-out distribution. For a vocabulary of size \(V\), entropy lies between 0 and \(\log V\) nats. This bound assumes a normalized discrete distribution over a finite vocabulary.

Entropy depends on the context and on the distribution being measured. The same prefix can lead to different distributions depending on the model, tokenizer, or temperature applied to the logits.

Numerical example: the Roman question’s next-token distribution

For the Roman question, consider this illustrative distribution over four candidate next tokens:

Candidate token Probability
“They” 0.6
“Aqueducts” 0.2
“Nothing” 0.1
“Well” 0.1

Using natural logarithms, entropy is:

\[ \begin{aligned} H(p)&=-\sum_v p(v)\ln p(v)\\ &=-(0.6\ln0.6+0.2\ln0.2 +2\times0.1\ln0.1)\\ &\approx 1.089\text{ nats}. \end{aligned} \]

The maximum for four outcomes is \(\ln4\approx1.386\) nats, so this distribution reaches about 79% of that maximum. These illustrative values let us follow the same example through likelihood, cross-entropy, and gradients.

Three distributions over four tokens. The concentrated distribution (100%, 0%, 0%, 0%) has entropy 0 nats. The Roman-question example (60%, 20%, 10%, 10%) has entropy 1.089 nats. The uniform distribution (25% each) reaches the maximum of 1.386 nats. The bars show probabilities; entropy summarizes the spread of the whole distribution.

Three distributions over four tokens, from most concentrated to most uniform, and their respective entropies.

The key identity is:

\[ H(p)=\mathbb E_{X\sim p}[-\ln p(X)]. \]

In other words, entropy is the expected surprisal when the outcome is drawn from \(p\). It is a property of the whole distribution, not the surprisal of one particular outcome.

Predictive entropy and the observed token are different questions

Entropy \(H(p)\) describes expected surprisal before the next token is known. Once the target token \(x_t\) is observed, its surprisal under the model is:

\[ -\log p_\theta(x_t\mid x_{<t}). \]

This quantity is the observed token’s surprisal. With a one-hot target, it is also that token’s loss contribution and its negative log-likelihood (NLL): \(-\ln p_\theta(x_t\mid x_{<t})\). These names refer here to the same quantity, emphasizing its information-theoretic interpretation, its role in the objective, or its connection to likelihood.

A model can be very confident and still be wrong: if \(p=(0.99;0.01)\), its entropy is low (about \(0.056\) nats), but observing the second token gives surprisal \(-\ln0.01\approx4.605\) nats. Observing the first token gives only \(-\ln0.99\approx0.010\) nats. Entropy is the average of these possible surprisals, weighted by their probabilities.

Summing token-level NLLs gives the sequence NLL, using the autoregressive factorization derived on the canonical Likelihood page.

Follow a case where the selected token differs from the observed target.

Entropy, temperature, and generation

Temperature \(\tau>0\) transforms logits \(z\) into a distribution:

\[ p_i^{(\tau)}=\operatorname{softmax}(z/\tau)_i. \]

Lower temperature tends to concentrate the distribution; higher temperature tends to flatten it. For \(\tau>0\), logit ordering and argmax remain unchanged, but probabilities and entropy change. Entropy after temperature scaling should not be presented as calibrated model uncertainty without empirical evaluation. The temperature mechanism and its other decoding effects are developed in advanced decoding strategies.

What entropy does not establish

  • Low entropy does not guarantee that the selected token is correct: a model can be confident and wrong.
  • High entropy does not mean the output is false; multiple continuations may be plausible.
  • Normalized probabilities do not guarantee calibration.
  • Perplexity summarizes mean NLL per token; its definition and comparison conventions are covered in the canonical Likelihood lesson.
  • Cross-entropy compares a target distribution q with a predicted distribution p; its definition, connection to NLL, and KL divergence are developed in the dedicated Cross-entropy lesson.

Return to the generation loop for the Roman example. When greedy decoding selects “They”, that token joins the prefix and the model computes a new distribution for the second turn.

Practice

The questions and exercises for this lesson are collected on its dedicated practice page.

Supplementary resources Experiment, explore further, and practise the ideas from this lesson.

Further reading

For predictive compression, the conditions for lossless coding, and the distinction from model compression, see the Compression and LLMs revision sheet. The entropy, KL, and predictive-information experiment develops the numerical checks, V-information, and the limits of these experiments.

References

Revision sheet — Compression and LLMs: review the connection between conditional probabilities, code length, model compression, and runtime memory in one place.

Experiment — Entropy, KL, and predictive information: verify identities on synthetic distributions and explore V-information.