Concepts / Predicting the Next Token in Language Models

Predicting the Next Token in Language Models

Entropy measures how much information, on average, a token in a language carries.

  • Programming

Guessing the Next Word

Imagine someone describing a location to you one word at a time, while you try to anticipate each word before it arrives. Sometimes the next word is almost obvious. At other times you have many plausible options, and any of them could show up. This article is about putting a number on that difference. Language-modeling work uses a quantity called entropy for exactly this purpose.

The way to understand entropy is to notice what changes when a language offers more or fewer possible tokens to choose from. The rest of this article follows that idea step by step: first by tracing a small language, then by reading the number that describes it, and finally by working through a count of tokens.

Tracing a Square-Position Language

Consider a small invented language whose only job is to name positions inside a square. Each token in the language tells you where something sits. The design choice that matters most is how many tokens the language contains, because that choice controls how specific each token can be.

A language that names only two broad regions gives coarse information: each token says whether a position is in the upper or lower part of the square. A language that names four finer regions gives more specific information, because each token pins down a more precise position. The four-token language is the one that carries more information per token. It also asks more of the system that stores and transmits those tokens.

Upper halftoken 1 of 2Upper lefttoken 1 of 4Lower halftoken 2 of 2Upper righttoken 2 of 4Lower lefttoken 3 of 4Lower righttoken 4 of 4
Read the columns side by side. The two-token language names broad halves of the square; the four-token language names finer quadrants. Entropy is 1 for the two-token language and 2 for the four-token language.

The source states the difference directly: the tokens in the richer language carry more information, and they also need more bits to represent them. Keep this pairing in mind, because the rest of the article depends on it.

What Entropy Averages Over

Definition

Entropy measures how much information, on average, a token in a language carries.

The word average is doing real work. A language is made of several possible tokens, and each one may carry a different amount of information. Entropy condenses that collection into one number that describes the typical token. It is a summary of the whole language, not a property of any single token.

Entropy links two ideas that might otherwise seem separate. The first is how much a token tells you. The second is how many bits a token costs to represent. The source gives two consequences: a higher entropy means each token carries more information, and a higher entropy means more bits are needed to represent a token.

Predictability and Entropy

Entropy can also be read as a measure of difficulty. The source describes it as a measure of how difficult it is to predict what comes next in a language. Under this reading, the two-token language and the four-token language differ in how hard it is to guess the next token. Choosing among two possibilities is easier than choosing among four.

A lower-entropy language is therefore more predictable, and a higher-entropy language is harder to predict. The key picture is the shape of the probability spread across possible next tokens. When one token dominates, the spread is peaked and the next token is easy to anticipate. When many tokens are about equally likely, the spread is flat and the next token is hard to anticipate.

Token Aprobability 0.90Token Aprobability 0.25Token Bprobability 0.05Token Bprobability 0.25Token Cprobability 0.03Token Cprobability 0.25Token Dprobability 0.02Token Dprobability 0.25
In the peaked column one token almost always appears next, so the next token is easy to predict and entropy is low. In the flat column every token is equally likely, so the next token is hard to predict and entropy is high. The probabilities here are illustrative values chosen for this example.

Counting Tokens to Find Entropy

The square example gives a concrete pattern: two tokens give entropy 1, and four tokens give entropy 2. The source does not spell out a general rule, but for tokens that are equally likely, the entropy in bits equals the base-2 logarithm of the token count. This rule reproduces both numbers from the example, so it is a useful way to read the pattern.

Formula, step by step
  1. 1
    H is the entropy in bits, the average information carried by a token.
  2. 2
    N is the number of equally likely tokens in the language.
  3. 3
    log base 2 of N asks how many yes-or-no questions are needed to single out one token from N.
Worked example

Entropy and bits for the square languages

A language has N equally likely tokens. Find its entropy in bits, and the bits needed to represent each token, for N equal to 2 and N equal to 4.

  1. 1
    Two-token language

    With N equal to 2, the entropy is $\log_2 2 = 1$. Each token needs 1 bit.

  2. 2
    Four-token language

    With N equal to 4, the entropy is $\log_2 4 = 2$. Each token needs 2 bits.

  3. 3
    Compare

    The four-token language has higher entropy, so each token carries more information and needs more bits, matching the source's statement.

Result

Entropy is 1 for the two-token language and 2 for the four-token language. The four-token language carries more information per token and needs more bits per token.

Worked example

Applying the count to a larger language

Suppose a generated language has 8 equally likely tokens. Find its entropy and the bits per token.

  1. 1
    Apply the rule

    With N equal to 8, the entropy is $\log_2 8 = 3$.

  2. 2
    Read the result

    Each token carries 3 bits of information on average and needs 3 bits to represent.

Result

The 8-token language has entropy 3. It carries more information per token than the 2-token or 4-token languages, and it is harder to predict.

Mistakes Learners Make with Entropy

  • Common mistake

    Treating entropy as the information of one specific token

    Why it fails

    Entropy is an average over the language's tokens. It describes the typical token, not any single one.

    Do this instead

    Describe entropy as the average information per token across the whole language.

  • Common mistake

    Reading a higher entropy as a more predictable language

    What it looks like

    Comparing a 2-token language (entropy 1) with a 4-token language (entropy 2), a learner might think the 4-token language is easier to guess.

    Why it fails

    The source says lower entropy means more predictable. Higher entropy means harder to predict.

    Do this instead

    Higher entropy means harder to predict and more information per token.

  • Common mistake

    Assuming the number of tokens alone decides entropy

    Why it fails

    The worked counts apply to equally likely tokens. When one token dominates, the spread is peaked and the entropy is lower even if many tokens exist.

    Do this instead

    Consider how probability is spread across the possible next tokens, not just how many there are.

Practice Predicting Entropy

EASY

A language has 16 equally likely tokens. Find its entropy in bits, and say whether it is more or less predictable than the four-token square language.

Hints
  • Use the rule that entropy equals the base-2 logarithm of the token count for equally likely tokens.
  • Remember that lower entropy means more predictable.

Wrapping Up the Idea

  1. 1
    Entropy measures how much information, on average, a token in a language carries.
  2. 2
    Higher entropy means each token carries more information and needs more bits to represent it.
  3. 3
    Lower entropy means a language is more predictable, and a perfectly predictable token carries no new information.
  4. 4
    For equally likely tokens, entropy in bits equals the base-2 logarithm of the number of tokens, which reproduces the square example's values of 1 and 2.

Key Takeaways

  • Entropy measures how much information, on average, a token in a language carries.
  • Higher entropy means each token carries more information and needs more bits to represent it.
  • Lower entropy means a language is more predictable; a perfectly predictable token carries no new information.
  • For equally likely tokens, entropy in bits equals log base 2 of the token count, matching the square example's values of 1 and 2.

More concepts starting with P