Course map
Stage 1: Foundations
Stage 2: Machine Learning
Stage 3: Deep Learning
Stage 4: LLMs & Generative AI
Stage 5: Production & Specialization

Part 13 / Topic 1 of 6

Tokens and tokenization

LLMs read tokens — subword pieces, not words or letters. Byte-pair encoding (BPE) builds the vocabulary by repeatedly merging the most frequent adjacent pair:

# examples/01_byte_pair_encoding_in_miniature_merging.py (on a 6-word corpus)
# merge 1: 'we' (seen 4 times)
# merge 2: 'lo' (seen 3 times)
# merge 3: 'r</w>' (seen 3 times)
# merge 4: 'lowe' (seen 2 times)
# merge 5: 'st' (seen 2 times)
# tokens for 'lowest': ['lowe', 'st', '</w>']

Five merges and "lowest" is already two meaningful pieces. The payoff: a fixed vocabulary (~100k tokens) covers any text — rare words just split into more pieces. Engineering consequences: you pay per token, code and non-English text tokenize less efficiently, and character-level tasks ("count the r's") are genuinely hard for models that never see characters.

🧪 Quick quiz

  1. Why do LLMs use subword tokens such as BPE?
Show the answer

A fixed vocabulary can cover any word by combining frequent pieces — after 5 merges, "lowest" became ['lowe', 'st', ''].

Done here? The practice set for Part 13 — 4 exercises with solutions — is on the part page.

Search the guide