Tokenizers, Explained Visually

📅 16 Jul 2026 ⏱ 12 min read Full research article ↗

Every large language model starts with a step nobody draws: converting text into token IDs. That step determines cost, fairness, and whether the model can do arithmetic at all. Here's the full picture — with live animations, not equations.

Watch BPE Learn

Byte Pair Encoding starts from individual characters and iteratively merges the most frequent adjacent pair. Step through the classic example below — each click performs one merge operation.

Interactive — BPE Merge Steps
Starting vocabulary: individual characters
Step 0 / 8

Text: "low lower newest widest" — the textbook BPE example (Sennrich et al. 2016)

Notice how each step collapses one frequent pair into a new token. By step 4, l·o·w is a single unit. By step 8, low·er is one token because it appeared multiple times in the corpus. The algorithm never looks ahead — it greedily takes the highest-frequency pair at each step. That greedy choice is, it turns out, optimal BPE is NP-complete, so greedy is all we have.

Three Algorithms, Three Philosophies

Side-by-Side — BPE vs WordPiece vs Unigram LM
BPE
GPT-4o · LLaMA 3 · Qwen · DeepSeek
1
Start with every character as its own token
2
Count every adjacent pair across all training text
3
Merge the most frequent pair into a new token
4
Repeat until vocab size is reached
merge = argmax freq(pair)
WordPiece
BERT · DistilBERT · older Google
1
Start with every character as its own token
2
Count how often pairs appear relative to chance
3
Merge the pair with highest pointwise mutual information
4
Repeat until vocab size is reached
score = freq(AB) / (freq(A)·freq(B))
Unigram LM
Gemma · Gemini · T5 · LLaMA 1/2
1
Start with a huge over-complete vocabulary
2
Score each token by its log-probability in a unigram LM
3
Remove the token whose removal hurts likelihood least
4
Repeat until vocab size is reached
remove = argmin ΔlogP(corpus)

BPE and WordPiece build up from characters. Unigram LM prunes down from a large set.

The Vocabulary Size Race

Every major lab expanded their vocabulary in 2024–2025. More tokens = better compression = fewer tokens per sentence = faster inference. The tradeoff: embedding tables grow linearly with vocabulary size. At 4096 embedding dimensions, going from 32K to 256K tokens adds about 1B parameters before any transformer layer.

Vocabulary Size by Model (scroll to animate)
GPT-2 · 2019
50K
BERT · 2019
30K
LLaMA 2 · 2023
32K
GPT-4 · 2023
100K
LLaMA 3 · 2024
128K
Qwen 2/3 · 2024
152K
GPT-4o · 2024
200K
Gemma/Gemini
256K ← largest

Bar widths are proportional to vocabulary size. Gemini's 256K = 5× more tokens than BERT.

The Token Tax — Same Meaning, Different Cost

The phrase "I love India" (or its equivalent) tokenizes into very different numbers of tokens depending on the language. With GPT-4's cl100k_base tokenizer, Arabic text costs 3.3× more per word than English. Every non-English user pays a silent tax on every API call.

Token Tax — GPT-4 cl100k_base (click a language to reveal)

Colors represent different tokens. More boxes = more tokens = higher compute cost and API bill.

The 4o upgrade (o200k_base, 200K vocabulary) improved CJK by ~40%. But Arabic, Bengali, and Tamil remain penalized. The vocabulary budget went wherever the training data was most concentrated — which was English.

Fertility — Measuring Tokenizer Fairness

Fertility = tokens produced ÷ meaningful units in the original text. A perfect tokenizer would score 1.0 for every language. The multilingual BPE tokenizer I built for ERA v5 coursework achieves between 1.258 and 1.291 — a spread of just 0.034 across four scripts.

Fertility Scores — ERA v5 Multilingual Tokenizer (10K vocab)

Lower fertility = better compression. The spread (max − min) = 0.034 determines the score.

What the spread means concretely: for every 100 meaningful units of text, the worst-served language (Telugu, 1.291) produces only 3.4 more tokens than the best-served language (Bengali, 1.258). The reference solution has a spread of 0.154 — 15.4 extra tokens per 100 units. We're 4.3× more equitable.

How BLT Works — Entropy-Based Dynamic Patching

Meta's Byte Latent Transformer (ACL 2025) is the first byte-level model to match tokenized LLMs at fixed compute. The key insight: allocate more compute where text is complex (high entropy), and use long, cheap patches where text is predictable.

BLT vs BPE — Dynamic vs Fixed Tokenization

Bar height = byte-level entropy (how uncertain the model is about the next byte). BLT places patch boundaries at entropy peaks. BPE cuts at fixed vocabulary-learned positions regardless of difficulty.

High entropy (complex — short patch, more compute)
Medium entropy
Low entropy (predictable — long patch, less compute)

Result: 8B params / 4T bytes — matches tokenized LLM performance at fixed FLOP cost (Meta AI, ACL 2025)

Why LLMs Fail at Arithmetic

The number 12345 doesn't have a consistent token representation. Depending on what surrounds it, it could split three different ways. The model never "sees" digits — it sees opaque numeric blobs. There's no place-value structure a neural network could learn arithmetic from.

How "12345" Tokenizes — Inconsistency Visualized
GPT-4o · o200k_base · in context A
123 45
2 tokens. No digit structure. Model cannot learn that "1" is in the ten-thousands place.
GPT-4o · o200k_base · in context B
12 345
Same number, different split. The model sees a completely different representation.
Gemma · split-digit tokenizer
1 2 3 4 5
Always 5 tokens. Consistent place-value structure. Model can learn arithmetic.
Production workaround (everyone else)
Python eval()
Route arithmetic to a code interpreter. Don't ask the LLM to do digit-level computation.

"Say Anything but This" (arXiv 2601.14658) proved arithmetic failures are systematic, not random — caused by tokenizer inconsistency.

The Byte-Level Model Family

Evolution of Byte-Level Architectures
ByT5 · 2022
Sequence length: 6×
Baseline. Raw UTF-8 bytes. Works, but slow due to long sequences.
MrT5 · 2025
Sequence length: 1.2× (after deletion)
Learned delete gate removes 80% of tokens mid-encoder. Byte input, token-level cost.
BLT · 2025
Dynamic: 1.0–3.0× (content-dependent)
Entropy-based patching. Hard text gets short patches; easy text gets long ones. Matches tokenized LLMs at 8B / 4T bytes.
Tokenized LLM
Sequence length: 1.0× (baseline)
Current production. BLT now matches this at fixed compute. BoundlessBPE adds 15% better compression by merging across word boundaries.

MrT5 (ICLR 2025) and BLT (ACL 2025) are the two credible paths to production byte-level models.

The Single Most Surprising Result

Tokenization is NP-complete. Whittington, Bachmann, and Pimentel proved this at ACL 2025 (arXiv:2412.15210) using a reduction from set cover. Both forms of the problem — choosing which vocabulary to build, and encoding text given a fixed vocabulary — are computationally intractable in general.

This means every tokenizer you've ever used — GPT-4o, LLaMA 3, Gemma — is a greedy approximation to a problem that cannot be solved optimally. There is no formula for the best vocabulary. There is no closed-form for the best corpus weights. Grid search is not a shortcut. It is provably the correct approach.

The landscape is not smooth: in building the multilingual tokenizer for ERA v5, increasing Telugu corpus weight from ×8 to ×9 widened the fertility spread from 0.034 to 0.108 in a single step — a 3× regression from one parameter change. This is not a bug. It's what NP-hard optimization landscapes look like.

Read the full research review →