4.2.1 · D3Tokenization & Language Modeling

Worked examples — Tokenization fundamentals

3,466 words16 min readBack to topic

Two phases, never confuse them

Before any example, pin down the two distinct phases most confusion comes from mixing:


The scenario matrix

Now list every case class tokenization can throw at us. Think of this like listing every quadrant before doing angle problems — we want to hit each cell at least once.

# Case class What is unusual about it Covered by
A Common word, fully in vocab Greedy path, 1 word → 1 token Ex 1
B Rare / long word Falls back to several subword pieces Ex 2
C Out-of-vocabulary chunk No merge helps; drops to characters/bytes Ex 3
D Whitespace & leading-space marker The Ġ/ space encoding, reversibility Ex 4
E Empty string / single char (degenerate) The zero-length and length-one edge cases Ex 5
F BPE tie-break (two pairs equal frequency) Which merge wins, and why order matters Ex 6
G Real-world word problem Cost/sequence-length estimate for a batch Ex 7
H Multilingual / emoji bytes One "character" = many byte-tokens (fertility blow-up) Ex 8
I Exam twist: decode ambiguity Why decode is not just "look up and concat" Ex 9

Every worked example below is tagged with its cell letter.


Setup: a tiny tokenizer we can fully hand-run

To make examples checkable, we fix a small learned tokenizer. It came from the parent note's corpus ["low","lower","newest","newest","widest"] plus a couple of extras. Its merge list (in learned order — order is sacred, first merge applies first) is:

The rule for applying merges to a single pre-token: start from its characters, then scan the merge list in order; whenever the current merge's pair appears, glue every occurrence, then move to the next merge. Never reach across into a neighbouring pre-token. This is exactly how BPE encodes at inference (see 4.2.02-BPE-algorithm).











Recall Self-test (reveal after guessing)

Encoding low gives how many tokens? ::: 1 (it is a single learned merge chain → ["low"]) What is fertility, in one line? ::: tokens ÷ words — the average number of BPE tokens each word costs. A pre-token and a BPE token — what's the difference? ::: A pre-token is a whitespace/punctuation chunk (a boundary); BPE runs inside it and may output one or several BPE tokens; BPE never merges across pre-token boundaries. Why does byte-level BPE have no OOV? ::: Every byte 0–255 is in the base vocab, so any text degrades into byte tokens instead of <unk>. In a true frequency tie during BPE training, what resolves it? ::: A fixed deterministic tie-break (e.g. lexicographically smallest pair) so training is reproducible. How many tokens does 🌍 cost in byte-level BPE with no learned merge? ::: 4 (its UTF-8 encoding is 4 bytes). Why isn't decode just lookup-and-concat? ::: After lookup+concat, a cleanup step must replace every /Ġ marker with a real space.


Where this connects

  • The merge mechanics: 4.2.02-BPE-algorithm
  • Likelihood-based cousins: 4.2.03-WordPiece-and-SentencePiece
  • What happens to token IDs next: 4.3.01-Word-embedings
  • OOV theory behind Ex 3/Ex 8: 4.1.05-Vocabulary-and-OV, 4.2.08-Multilingual-tokenization
  • Why sequence length (Ex 7) is expensive: 5.1.02-Transformer-architecture, 3.4.03-Sequence-length-and-padding
Figure — Tokenization fundamentals

The figure above shows Ex 2 as a merge tree: characters at the bottom fuse upward along the learned merges into new and est.

Figure — Tokenization fundamentals

This second figure shows fertility across our examples — how many tokens each input costs — making the "rare = more tokens" pattern visible at a glance.