Tokenization

Four-panel comic showing the tokenization trade-off: 'Too Small' (a robot tediously sorting individual letters), 'Too Big' (a robot struggling to stuff 'CRYPTOCURRENCY' through a vocabulary door), and 'Just Right' (a robot neatly slicing 'UNHAPPINESS' into 'UN' and 'HAPPINESS')
What You’ll Learn

You’ll learn why character-level and word-level tokenization each break down, and how subword tokenization finds a useful middle ground. You’ll also see how tokenizers turn text into integer IDs and why that choice affects what the model can learn.

Before a model can process text, it needs to break it into pieces, called tokens. Each token gets mapped to an integer ID. An ID is a vocabulary address, not a measure of meaning.

Clear the 3 tokenizer puzzles below (· marks a leading space):

Level 1 · The Hidden Space Trick Add a single space at the very start of the text and watch the first token’s ID change!

Loading the GPT-2 tokenizer…

Quick presets:

The Tokenization Trade-off

There are three strategies, each with a fundamental trade-off:

Character-Level

“hello” → ["h", "e", "l", "l", "o"] → [104, 101, 108, 108, 111] — 5 tokens

ProsCons
Tiny vocabulary (~100 characters, or 256 bytes)Very long sequences
Can represent any text if it uses bytes or full Unicode coverageHard to learn word-level meaning
No out-of-vocabulary wordsSlow training and inference

Word-Level

“hello world” → ["hello", "world"] → [4821, 1037] — 2 tokens

ProsCons
Short sequencesHuge vocabulary (100K+)
Natural semantic unitsRare or new words collapse to [UNK]
”cryptocurrency” → [UNK]

Subword: The Sweet Spot

What if we could get the best of both? Subword tokenization splits text into pieces that are often larger than characters but smaller than words. The tokenizer learns useful pieces from training data.

“unhappiness” → ["un", "happiness"] — the model learns that “un” means negation!

InputTokensWhy it works
”unhappiness”["un", "happiness"]Learns morphemes
”playing”["play", "ing"]Shares verb stems
”cryptocurrency”["crypt", "o", "currency"]Handles novel words

Subword tokenization gives us a moderate vocabulary, usually tens of thousands of tokens, with reasonable sequence lengths. Many tokenizers can represent any text by falling back to bytes or characters; others still keep an [UNK] token as a fallback. This family of methods powers GPT, BERT, LLaMA, T5, and most modern language models.

The key insight: common words often stay whole (“the”, “hello”, “cat” may be single tokens), while rare or novel words get split into familiar pieces.

Quick Check
Why is subword tokenization preferred over word-level or character-level?
Your turn
Choose an answer to check your understanding.

The Complete Pipeline

Regardless of which tokenization strategy you use, the pipeline is always the same:

"I love cats" → tokenize → ["I", "love", "cats"] → lookup → [42, 891, 3017]
  1. Split the text into tokens (subwords, in practice)
  2. Map each token to an integer ID using a fixed vocabulary table
  3. Feed those IDs into the model (next stop: Embeddings)

That’s it. For understanding how transformers work, this is all you need to know about tokenization: text goes in, a sequence of integer IDs comes out, and each ID will get looked up in an embedding table.

Fill in the Blanks
Subword tokenization splits rare words into pieces while keeping common words as tokens.
Fill in every blank, then check your answer.

Quick Summary

ConceptWhat HappensIntuition
Character-levelSplit into individual charactersTiny vocab, but sequences are very long and meaning is lost
Word-levelSplit on whitespace/punctuationNatural units, but rare words need [UNK]
SubwordSplit into learned pieces between characters and wordsCommon words stay whole; rare words split into known pieces
Token IDsEach token maps to an integerThe model only sees numbers, not text

Going Deeper

The sections below are optional. Everything above is sufficient for understanding the rest of the course. But if you’re curious about how subword tokenizers actually learn their vocabularies, read on.

Learning Checklist
  • I can explain the trade-offs between character-level, word-level, and subword tokenization
  • I understand that tokenization converts text into a sequence of integer IDs
  • I know that common words often stay as single tokens while rare words get split into subword pieces
  • I can explain why subword tokenization is common in modern language models

Next Steps

Now we know how text becomes a sequence of token IDs. But a bare integer like 3017 doesn’t tell the model anything about what “cats” means. Next, we’ll see how each token ID gets transformed into a rich vector that captures semantic meaning: Embeddings →.