📌 The Tokenization Compromise
In our earlier lesson on Tokens, we learned that AI models don't read words; they read mathematical Tokens. But how exactly does the AI decide where to draw the line to create a Token?
- Option 1 (Whole Words): If we create a unique Token for every single word in the English language, the AI's dictionary becomes massive (millions of tokens), requiring too much compute power.
- Option 2 (Single Letters): If we create a Token for every single letter (a, b, c), the dictionary is tiny (26 tokens), but the AI loses all context because the single letter "e" has no semantic meaning.
The industry solution is the "Sub-word Compromise," powered by an algorithm called Byte Pair Encoding (BPE).
📌 How BPE Works (The Merging Algorithm)
Byte Pair Encoding is the exact mathematical algorithm used by GPT-4 and Llama 3 to build their Token dictionaries. It is purely statistical.

Here is how it works during the AI's training phase:
- The algorithm starts by breaking the entire training dataset down into single characters:
[ 'e', 'r', 's', 't' ... ] - It runs a massive statistical scan across the text: "What are the two most frequently adjacent characters in this book?"
- It mathematically discovers that
eandrappear next to each other constantly. It permanently merges them into a brand new Token:[er]. - It scans again, finds
eands, and merges them into[es]. - It repeats this process thousands of times, eventually building a highly efficient vocabulary of common prefixes and suffixes (like
[ing],[est],[pre],[tion]).
📌 Handling Rare Words
The true brilliance of BPE is how it handles completely made-up or rare words without crashing.

If you feed an old-school Whole-Word Tokenizer the made-up word "superflurgal," the Tokenizer will crash and output an <UNK> (Unknown) error, because that word is not in its dictionary.
If you feed "superflurgal" to a modern BPE Tokenizer, it simply falls back to its merged sub-words! It smoothly slices the unknown word into recognizable chunks: [super], [fl], [ur], [gal]. The AI can now process the text without crashing, and it even understands the prefix [super]!
Quick Knowledge Check
Test what you just learned about the BPE Algorithm
Question 1 of 1
Why does the Byte Pair Encoding (BPE) algorithm merge the letters "i", "n", and "g" into a single [ing] token, rather than just keeping them as separate letters?
Loading results...