A language model reads tokens, not text, and the choice of token sets a basic trade-off. Word tokens give short sequences but a huge vocabulary that still misses new words (out-of-vocabulary, OOV). Character tokens never miss a word but make sequences several times longer, which is expensive for attention and makes each step carry less meaning. Subword tokens (BPE, WordPiece, Unigram) sit in between: frequent words stay whole, rare words split into reusable pieces, and nothing is unknown. This page covers the three granularities, OOV handling, fertility (tokens per word), vocabulary size versus sequence length, and their costs in embeddings and attention. In the exercises you write the three tokenisers, then compare them on text built from stems and suffixes. Even the full 2224-word training vocabulary missed 4.1% of test words, and a 500-word vocabulary missed 20%. Characters needed 7.0 tokens per word. BPE with 1500 merges had no unknown tokens at 1.13 tokens per word, and split unseen words into about two pieces.
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.
Choosing the pieces a model reads
A model can read text letter by letter, word by word, or in pieces in between. Whole words are efficient, but no dictionary has every word: new names, typos and words like “unlaunchable” will be missing. Letters never go missing, but sentences become very long. Subword pieces such as “launch” + “able” get the best of both: common words stay whole, and rare words are spelled from familiar parts. The demo counts how many tokens one sentence needs under each choice.
Language lab
"the newest rockets flew lower": 29 characters, 5 words, 20 subword tokens after 6 BPE merges (t·h·e ne·we·st r·o·c·k·e·t·s f·l·e·w lo·we·r). Subwords sit between: no unknown words, shorter than characters.
Why engineers care
Tokenisation decides what a model can even see: part numbers, call signs, chemical formulas and code identifiers are often split into odd pieces, which affects accuracy and the cost of running the model.
📝 Quick check (Level 0)
0/3 answered
1. Word-level tokenisers struggle with…
2. Character-level tokenisation makes sequences…
3. Subword tokenisers split rare words into…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.
By hand
Word level: vocabulary = the V most frequent training words; everything else becomes <unk>. Zipf’s law means a long tail of rare words, so OOV never reaches zero.
Character (or byte) level: tiny vocabulary, no OOV, but about 5–7 tokens per English word. Self-attention cost grows with the square of length, so 6× longer sequences cost about 36× more attention.
Subword level: a vocabulary of 30k–250k pieces learned from data. Frequent words are single tokens, rare words split, e.g. “unlaunchable” → “un”, “launch”, “able”.
Fertility: tokens per word, a measure of efficiency (about 1.3 for English with GPT-style tokenisers, often 2–4 for under-represented languages).
Costs: embedding and output layers scale with vocabulary size (50k × 768 = 38.4M parameters); sequence length drives compute and context usage.
📝 Quick check (Level 1)
0/3 answered
1. OOV rate with one unknown token in four:
2. Attention cost of 6× longer sequences is about…
3. Embedding parameters for 50,000 tokens × 768 dims:
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.
Tokenisers in code
Exercise 1.
Implement character and word tokenisers.
Implement greedy longest-match subword segmentation, which never produces an unknown token.
Practice.
Use the tokenizer that ships with a pretrained model (AutoTokenizer.from_pretrained), never a different one.
Train your own with Hugging Face tokenizers or SentencePiece when building a model from scratch or for a very different domain.
Measure fertility on your own data; domain terms (part numbers, formulas) can split badly.
Count costs in tokens, not words or characters: context limits and API prices are per token.
from transformers import AutoTokenizertok = AutoTokenizer.from_pretrained("gpt2")ids = tok("The rocket was unlaunchable.")["input_ids"]print(len(ids), tok.convert_ids_to_tokens(ids)) # subword pieces, 'Ġ' marks a leading space
🐍 Exercise 1: three tokenisers
Implement character, word and greedy longest-match subword tokenisers.
📝 Quick check (Level 2)
0/3 answered
1. With a pretrained model, use…
2. Context limits and prices are counted in…
3. A library for training tokenizers:
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.
Theory
The trade-off. For a corpus of N characters, total tokens fall as the vocabulary grows, while the vocabulary-dependent parameters and the sparsity of rare tokens grow. Subword vocabularies of 30k–100k are a compromise found empirically to work well.
Why subwords generalise. Languages build words from reusable parts (stems, affixes, compounds). A subword vocabulary learned by compression (BPE) or likelihood (Unigram) tends to recover such parts, so an unseen word is composed from pieces the model has learned. This is the open-vocabulary property Sennrich and colleagues introduced for translation in 2016.
Byte fallback. Even subword vocabularies can meet unseen characters (new scripts, emoji). Byte-level tokenisers (F2-4) start from the 256 bytes, so every string is representable.
📝 Quick check (Level 3)
0/3 answered
1. Larger vocabularies mean…
2. Subwords generalise because words are built from…
3. Open-vocabulary subword translation was introduced by…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.
How do word, character and subword vocabularies trade coverage against length?
History.
R. Sennrich, B. Haddow and A. Birch, “Neural machine translation of rare words with subword units” (ACL 2016).
Y. Wu et al., “Google’s neural machine translation system” (2016), with WordPiece.
Exercise 2.
Data: words formed from about 400 stems (Zipfian) plus one of 10 suffixes (−, s, ed, ing, er, ers, ly, ness, able, less); 20,000 training tokens (2224 word types) and 5000 test tokens.
Tokenisers:
word level, keeping the 500, 2000 or all training words;
character level;
BPE with 50, 300 or 1500 merges.
tokeniser
vocabulary
test OOV rate
tokens per word
tokens per unseen word
words, top 500
500
0.201
1.00
—
words, top 2000
2000
0.055
1.00
—
words, all
2224
0.041
1.00
—
characters (+ end marker)
22
0
7.00
7.00
BPE, 50 merges
72
0
2.93
5.00
BPE, 300 merges
322
0
1.66
3.15
BPE, 1500 merges
1522
0
1.13
2.07
Word vocabularies never close the gap: even keeping every training word leaves 4.1% of test words unknown, because rare stem–suffix combinations keep appearing.
Characters cover everything but cost 7 tokens per word, about 38× more attention than BPE with 1500 merges.
BPE gets both: with a vocabulary smaller than the word list it has no OOV and only 1.13 tokens per word. Unseen words split into about two pieces, which is what stem + suffix would give.
Seeds: seed 1 gave the same pattern (OOV 0.040 for all words, 1.14 tokens per word for BPE with 1500 merges).
🐍 Exercise 2 (research): how do word, character and subword vocabularies trade coverage against length?
Compare word vocabularies of several sizes, characters and BPE with several merge counts on held-out text: OOV rate and tokens per word.
Tokenisation in AI, robotics and aerospace
Technical text: checking how part numbers, units and call signs are split before fine-tuning a model on maintenance or ATC data.
Multilingual operations: fertility differs by language, which affects cost and context use.
Design lesson: measure tokens per word on your own data, and remember that every token costs compute.
References
R. Sennrich, B. Haddow and A. Birch (2016). “Neural machine translation of rare words with subword units.” Proc. ACL.
Y. Wu et al. (2016). “Google’s neural machine translation system: bridging the gap between human and machine translation.” arXiv:1609.08144.
S. J. Mielke et al. (2021). “Between words and characters: a brief history of open-vocabulary modeling and tokenization in NLP.” arXiv:2112.10508.
Year subword units for translation were published at ACL?
2016.
Level 1
Problem 4Level 1
Embedding parameters for a 50,000-token vocabulary at 768 dims?
38400000.
Problem 5Level 1
BPE vocabulary from 256 bytes and 49,744 merges?
50000.
Problem 6Level 1
Attention cost ratio for 7.0 vs 1.13 tokens per word (2 decimals)?
38.37.
Problem 7Level 1
Characters + end marker for the word ‘rocket’?
7.
Level 2
Problem 8Level 2
Test OOV rate with all 2224 training words?
0.041.
Problem 9Level 2
Test OOV rate with the top 500 words?
0.201.
Problem 10Level 2
Tokens per word at character level?
7.
Problem 11Level 2
Tokens per word with 1500 BPE merges?
1.13.
Problem 12Level 2
Tokens per unseen word with 1500 BPE merges?
2.07.
Problem 13Level 2
Vocabulary size with 300 BPE merges?
322.
Level 3
Problem 14Level 3
Explain why the word-level OOV rate never reaches zero.
Word frequencies follow a Zipf-like law: a long tail of rare words and combinations. Any finite training sample misses part of the tail, and new text keeps producing unseen types (Heaps’ law: the vocabulary grows like Nᵝ with β ≈ 0.5). So a fixed word vocabulary always has unknown words. ∎
Problem 15Level 3
Explain why unseen words took about two BPE tokens.
Unseen words are new combinations of a known stem and a known suffix. With 1500 merges, frequent stems and all suffixes have become single tokens, so most unseen words split exactly into stem + suffix: two pieces, plus occasional extra pieces for rarer stems. ∎
Problem 16Level 3
Compare character-level and subword-level models.
Character models have no OOV and tiny embedding tables and are robust to typos, but sequences are 4–7× longer, so attention and context are costlier and each step carries less information. Subword models are efficient and generalise to new words through pieces, but can split rare or domain terms badly and are sensitive to spacing and casing. ∎
Problem 17Level 3
Advise on tokenising maintenance logs with many part numbers.
Keep the pretrained model’s tokenizer if fine-tuning, but measure how part numbers split. If they explode into many pieces, consider normalising their format, or adding frequent part-number patterns as special tokens (resizing the embeddings). Training a new tokenizer is justified only when training from scratch. ∎
Level 4
Problem 18Level 4
Test OOV rate with the top 2000 words?
0.055.
Problem 19Level 4
Tokens per word with 50 BPE merges?
2.93.
Problem 20Level 4
Training word types?
2224.
Coding practice
🐍 Code 1 (Level 0): OOV rate
Fraction of unknown tokens.
🐍 Code 2 (Level 0): fertility
Tokens per word.
🐍 Code 3 (Level 0): attention cost
Square of the length ratio.
🐍 Code 4 (Level 0): embedding size
V · d.
🐍 Code 5 (Level 0): BPE vocabulary
Base + merges.
Topic test
A timed test drawn at random from this topic's question bank. Each attempt gets new questions. For a longer test across the whole chapter, see the chapter test, or mix topics in the question bag.
Character, Word and Subword Tokenization: topic test
10 questions, drawn at random from a bank of 21. Every attempt is different.
⏱ Time limit: 15 minutes. The test submits itself when time runs out.
Levels covered: 0, 1, 2, 3, 4. No hints or solutions until you submit.