Unit 1 · Foundations

Lesson 02 · Available to read

Tokens and Tokenization: How Machines Read Text

How text becomes tokens: sub-word vocabularies, Byte-Pair Encoding, and why tokenization explains many LLM quirks.

Before a model can predict anything, text has to become numbers. Tokenization is that first step: chop text into pieces called tokens, and replace each piece with its ID in a fixed vocabulary. The model only ever sees those IDs.

Three ways to cut a sentence

There are three obvious choices for the unit of text:

The word 'unhappiness' as 11 character tokens, 1 word token, or 3 sub-word tokens.
The word 'unhappiness' as 11 character tokens, 1 word token, or 3 sub-word tokens.
  • Characters — tiny vocabulary (a few hundred symbols), never an unknown word, but sequences become very long and each token carries little meaning.
  • Whole words — meaningful units, but the vocabulary explodes (every name, typo, plural and verb form) and any new word becomes an unknown [UNK].
  • Sub-words — the compromise every modern LLM uses. Common words are a single token (" the", " model"); rare words are built from pieces (" token" + "ization").

Sub-words give a manageable vocabulary — typically 32,000 to 200,000+ tokens — while still being able to spell out any string, even emoji or a made-up word, from smaller pieces down to raw bytes.

Byte-Pair Encoding (BPE): learning the vocabulary

The most common way to build a sub-word vocabulary is Byte-Pair Encoding. The algorithm is surprisingly simple:

  1. Start with a vocabulary of single characters (or bytes).
  2. Count every pair of adjacent symbols in the training corpus.
  3. Merge the most frequent pair into a new symbol and add it to the vocabulary.
  4. Repeat until the vocabulary reaches the target size.

Here is BPE on a tiny corpus where cat appears 6 times, cats 4 times and hat 5 times. The pair a+t occurs 15 times (6 + 4 + 5), so it is merged first:

Byte-Pair Encoding repeatedly merges the most frequent adjacent pair, building tokens like 'at', 'cat' and 'hat'.
Byte-Pair Encoding repeatedly merges the most frequent adjacent pair, building tokens like 'at', 'cat' and 'hat'.

After a few merges, frequent chunks like "at" and "cat" become single tokens. The order of merges is saved, and at tokenization time the same merges are applied to new text. Variants such as WordPiece and Unigram (SentencePiece) differ in how they choose merges, but the idea is the same.

From tokens to IDs

Each token maps to an integer. A sentence might become something like:

"Tokenization is fun!" → ["Token", "ization", " is", " fun", "!"] → [30642, 2065, 374, 1257, 0] (illustrative IDs)

The exact IDs depend on the tokenizer — every model family has its own. Notice that the space usually belongs to the following token: " is" and "is" are different tokens with different IDs.

Tokenizers also reserve special tokens that never appear in normal text, such as an end-of-text marker, or markers that separate the system, user and assistant turns in a chat.

Rules of thumb

For English text with typical modern tokenizers:

  • 1 token ≈ 4 characters ≈ ¾ of a word.
  • 100 tokens ≈ 75 words; a 1,500-word article ≈ 2,000 tokens.

These matter because models are priced, limited and slowed by tokens, not words: context windows, API prices and generation speed are all measured in tokens.

Languages that were less common in the tokenizer's training data often need more tokens for the same meaning — so the same message can cost more and use more of the context window in, say, Amharic or Burmese than in English.

Quirks explained by tokenization

Many famous LLM blunders come straight from this step:

  • Counting letters. Asked how many r's are in "strawberry", a model sees something like ["str", "aw", "berry"] — it never directly sees the individual letters, so it has to know the spelling rather than look at it.
  • Arithmetic. A number like 1234567 may be split as "123", "456", "7" — awkward chunks for digit-by-digit maths. Some tokenizers split numbers into single digits for this reason.
  • Leading spaces and capitals. "Paris", " Paris" and " PARIS" are unrelated IDs; the model has to learn they're connected.
  • Typos and rare words explode into many small tokens, which makes them harder to process.
  • Code depends on how whitespace and indentation are tokenized; modern tokenizers merge runs of spaces to save tokens.

Why this matters for everything else

Every later lesson operates on token IDs: embeddings (Lesson 3) give each ID a vector, attention (Lessons 5–6) relates tokens to each other, and the context window (Lesson 7) is a limit on how many tokens fit. When you hear "a 200K context window" or "$3 per million tokens", this lesson is what's being counted.

The takeaway

LLMs never see letters or words — they see token IDs from a fixed sub-word vocabulary learned by merging frequent character pairs, and many quirks (letter counting, arithmetic, non-English cost) trace back to that choice.

Practice

0 of 7 attempted

  1. 1Recall

    Name the three granularities for splitting text into tokens and give one drawback of each of the first two.

    Saved in this browser

  2. 2Calculate

    Using the rule of thumb (1 token ≈ ¾ word), roughly how many tokens is a 3,000-word report? If an API charges $3 per million input tokens, what does it cost to send that report 1,000 times?

    Saved in this browser

  3. 3Apply

    Run BPE by hand. Corpus: "hug" ×10, "pug" ×5, "hugs" ×5, "bun" ×4. Start from characters. Which pair is merged first? Which is merged second? (Count pair frequencies, weighting by word counts.)

    Saved in this browser

  4. 4Multiple choice

    Why do sub-word tokenizers never produce an 'unknown word' token?

  5. 5Explain in your own words

    In your own words, explain why an LLM might miscount the letter 'r' in "strawberry", even though it can write the word perfectly.

    Saved in this browser

  6. 6Explain in your own words

    Why might a user writing in a less widely represented language pay more and get less context for the same message?

    Saved in this browser

  7. 7Recall

    What is a special token? Give two examples of what special tokens are used for.

    Saved in this browser

See what unlocked