Part A · Chapter 2 · The pieces

2. Tokens

Models read numbers, not letters. A tokenizer cuts text into tokens and gives each one an id; byte-pair encoding decides where the cuts go, and the count of tokens is what you pay for.

From a sentence to tokens to pieces
Sentence
Or type your own
Playback speed

First the sentence as our models read it: one token per word or punctuation mark, each with its id, its row number in the vocabulary. A word the models never saw becomes <unk>, id 3. Then byte-pair encoding, learned from our training sentences, starts from single letters and merges the most frequent pair of neighbours, one merge per step. A counter shows the pieces against the characters.

The idea

A model cannot read letters. Everything it does is arithmetic on numbers, so the first step of every model is a tokenizer: it cuts the text into tokens and replaces each with its token id, the token's row number in the model's vocabulary.

Our two models use the simplest cut there is: one token per word or punctuation mark. Their shared vocabulary has 201 entries: English words, German words, punctuation and four special tokens, such as <s> for the start and </s> for the end. It is easy to read, but a word that is not on the list cannot be read at all: it becomes <unk>, the unknown token.

The paper did something cleverer, called byte-pair encoding. Start from single letters. Count which two neighbouring pieces stand together most often in the training text, and glue that pair into one new piece. Repeat a few thousand times. Common words end up as one piece; rare words break into a few familiar ones; and no word is ever unknown, because in the worst case it is spelled out letter by letter.

The animation runs our own byte-pair encoding, learned from our English training sentences with 60 merges. Watch the most frequent pairs go first: the merges follow how often letters stand together, not what they mean.

A worked example: “She eats apples .”

In our sentences the most frequent pair of neighbours is “h” + “e”, seen 1088 times, so it is merged first. The second merge, “t” + “he”, already builds on the first: pieces grow out of pieces.

“She eats apples .” is 4 tokens for our models, with the ids 52, 103, 71, 5. Byte-pair encoding turns its 14 characters into 10 pieces: S · he · ea · t · s · ap · p · l · es · ..

“The zebra eats a pizza .” has two words our models never saw, so for them it is The <unk> eats a <unk> .. Byte-pair encoding still reads it, as The · z · e · b · r · a · ea · t · s · a · p · i · z · z · a · ..

The formula

Each merge takes the pair of neighbouring pieces that occurs most often in the training text:

(a, b)^{*} = \underset{(a,\,b)}{\arg\max}\ \operatorname{count}(a\,b)
\(a,\ b\)
two pieces that stand next to each other, at first single letters
\(\operatorname{count}(a\,b)\)
how often that pair occurs in the training text
\(\arg\max\)
the pair with the largest count: it becomes one new piece, ab

The paper writes a sentence as a sequence of symbols, \((x_1, \dots, x_n)\). After tokenizing, \(n\) is the number of tokens and each \(x_i\) is one token id. Everything that follows in the model, and every price list, counts in tokens, not in words or characters.

Why it matters

LLM APIs charge per token and limit the context window in tokens. The same text can be a different number of tokens for different models, because each has its own learned pieces. Text the tokenizer saw often in training, such as common English words, packs into few tokens; product names, code identifiers, long numbers and many other languages break into more, so they cost more and fill the context faster. When an estimate matters, count with the provider's own tokenizer rather than counting words.

Odd splits explain odd behaviour too. A model that sees a rare product name as four unrelated pieces has to learn what those pieces mean together, and spelling a word or counting its letters is hard for a model that never sees the letters.

Where it fails

  • Whole-word tokens break on new words. Our models know only the words of their training sentences: “zebra” is <unk> to them, and the translation playground refuses it rather than guess.
  • Merges follow frequency, not meaning. Our encoding cuts “She” into “S” and “he”, because “he” is so common in our sentences. Real tokenizers make the same kind of cut on a much larger scale.
  • A tokenizer favours the text it learned from. One learned mostly from English packs English best; the same sentence in another language can take many more tokens, and costs more.
Remember Models read token ids, not text. Byte-pair encoding builds its pieces from the most frequent pairs of neighbours, so common words are one token and rare ones several, and you pay, and fill the context, per token.

Exercises

Try each one before you open a hint. The lab's engine checks your answer against the same models the chapter uses.

Count the tokens

How many tokens does our models' tokenizer make of “Today , the old man reads a letter .”? Do not count the end marker </s> that the encoder adds.

Your answer
A whole number

Which pair merges first?

Our byte-pair encoding learned from the English training sentences. Which pair of letters did it merge first?

Pieces of a new word

Our models cannot read “zebra”. Into how many pieces does our byte-pair encoding cut it?

Your answer
A whole number

Characters per piece

“Today , the old man reads a letter .” has 28 characters, spaces not counted, and our byte-pair encoding makes 15 pieces of it. How many characters per piece is that? Two decimals.

Your answer
Within 0.01 of the answer

In the paper

Section 5.1 says that the paper's English–German sentences “were encoded using byte-pair encoding”, with “a shared source-target vocabulary of about 37000 tokens”. That sentence is this chapter: the same merging, run over millions of sentences, so that English and German share one list of pieces.

In the paper: section 5.1

Open the Theory tab
An unhandled error has occurred. Reload 🗙

Reconnecting…

Your connection to the Transformers and Attention Lab dropped, probably a Wi-Fi blip. Hold on while it reconnects.

The connection could not be restored. The page will reload in a few seconds.