Tokenization Explained: BPE vs WordPiece vs SentencePiece
By TokenMath Editorial & Engineering Team · Reviewed · Methodology
Why can't neural networks just read raw letters? How does a 200,000-token vocabulary compress multilingual documents? Here is how the subword tokenizers behind GPT, Claude, Gemini and Llama actually work.
1. The Problem With Character-Level and Word-Level Processing
Neural language models do not process text strings directly; they compute over vectors of floating-point embeddings indexed by discrete integers. When computer scientists first designed text models, two naive approaches failed:
- Word-level tokenization: Assigning an ID to every unique English word creates an intractable vocabulary (millions of entries), cannot handle typos (
tehinstead ofthe), and fails completely on out-of-vocabulary (OOV) terms. - Character-level tokenization: Using single UTF-8 characters keeps the vocabulary tiny (256 bytes), but blows up sequence length by 4x to 5x. Because transformer self-attention scales quadratically or linearly with sequence length, processing character-by-character is prohibitively expensive.
2. How Byte-Pair Encoding (BPE) Solves the Dilemma
Originally a data compression algorithm published by Philip Gage in 1994, Byte-Pair Encoding was adapted for neural networks by Sennrich et al. in 2016.
BPE begins with a base vocabulary of individual bytes (256 items). It iteratively scans a multi-terabyte training corpus, finds the most frequently co-occurring pair of adjacent tokens, merges them into a new single token, and repeats this process until reaching a predetermined vocabulary budget (e.g. 100,000 or 200,000 tokens).
BPE Tokenization, Step by Step
Take a tiny training corpus with word counts: low ×5, lower ×2, newest ×6, widest ×3. Every word starts split into characters. BPE counts every adjacent pair, weighted by word frequency, and merges the winner:
| Merge | Most frequent pair | Count | New token |
|---|---|---|---|
| 1 | e + s | 9 (newest 6 + widest 3) | es |
| 2 | es + t | 9 | est |
| 3 | l + o | 7 (low 5 + lower 2) | lo |
| 4 | lo + w | 7 | low |
After four merges, newest encodes as n · e · w · est and lower as low · e · r. A production tokenizer repeats this tens of thousands of times over terabytes of text. At inference, the learned merges are replayed in order on new text, so an unseen word like lowest still encodes cleanly as low · est.
Byte-Level BPE and Byte Fallback
Character-based BPE still needs an "unknown" token for characters it never saw. Byte-level BPE (introduced with GPT-2) runs the algorithm over raw UTF-8 bytes, so the 256-byte base vocabulary can encode any string — emoji, Cyrillic, or binary-looking garbage — with no unknown token. OpenAI's cl100k_base and o200k_base work this way. SentencePiece achieves the same guarantee with byte fallback: characters outside the vocabulary are emitted as their individual UTF-8 bytes. The cost is efficiency: a rare character can take 2–4 tokens, which is why non-Latin scripts cost more.
3. WordPiece Tokenization
WordPiece, developed at Google and popularized by BERT, builds its vocabulary the same bottom-up way as BPE but chooses merges differently. Instead of the most frequent pair, it merges the pair with the highest score:
This favors pairs whose parts rarely appear apart, i.e. merges that genuinely increase the likelihood of the training data. WordPiece marks pieces that continue a word with ##, so tokenization becomes token · ##ization. At inference it uses greedy longest-match-first instead of replaying merges. BERT-base uses a 30,522-token WordPiece vocabulary. WordPiece is common in encoder and embedding models but rare in today's generative LLMs.
4. SentencePiece and the Unigram Model
SentencePiece (Kudo & Richardson, 2018) is a tokenizer library, not a single algorithm. Its key idea is to skip language-specific pre-tokenization: it reads raw text as a stream of Unicode characters and treats the space as an ordinary symbol, written ▁. That makes it work equally well for languages without spaces, such as Japanese and Chinese, and makes decoding perfectly reversible.
SentencePiece can train two kinds of models: BPE (used by Llama 1 and 2 with a 32,000-token vocabulary) and Unigram. The Unigram language model (Kudo, 2018) works top-down: it starts from a large set of candidate substrings, assigns each a probability, and repeatedly prunes the tokens whose removal reduces corpus likelihood the least. When encoding, it picks the most probable segmentation, and during training it can sample alternatives ("subword regularization") to make models more robust. Google's Gemini and Gemma models use SentencePiece tokenizers with vocabularies around 256,000 tokens.
BPE vs WordPiece vs Unigram at a Glance
| Algorithm | Builds vocabulary by | Encoding | Used by |
|---|---|---|---|
| BPE | Merging the most frequent pair | Replay merges in order | GPT, Claude, Llama 3/4, Mistral Tekken, DeepSeek, Qwen |
| WordPiece | Merging the pair that most raises likelihood | Greedy longest match, ## prefixes | BERT, DistilBERT, many embedding models |
| Unigram | Pruning a large candidate set | Most probable segmentation | T5, ALBERT, Gemini/Gemma (via SentencePiece) |
5. Frontier Tokenizer Architectures Compared
Today, different frontier AI labs utilize specialized tokenizer implementations tailored to their training data and target languages:
| Model Family | Tokenizer Architecture | Vocabulary | Key Characteristics |
|---|---|---|---|
OpenAI gpt-6-astra, gpt-5.6-sol, o3 | o200k_base (Tiktoken) | 200,000 | High compression on non-English scripts; fewer tokens per word. |
Anthropic Claude 5 claude-sonnet-5-20260901, claude-opus-5-20260815 | Claude BPE | ~65,000 | Optimized for source code syntax and technical reasoning. |
Google Gemini 3.8 gemini-3.8-flash-001, gemini-3.1-pro-001 | SentencePiece Unigram | 256,000 | Massive vocabulary with native whitespace treatment. |
DeepSeek deepseek-flash, deepseek-v4-1-flash | Byte-Level BPE | 100,000 | Multi-token prediction support with high code density. |
Meta / Together AI llama-4-scout, llama-4-maverick | Tiktoken Llama BPE | 128,000 | Optimized for instruction tuning and extreme 10M contexts. |
Mistral AI mistral-small-2603, mistral-large-2512 | Tekken Tokenizer | 131,072 | Optimized for multilingual efficiency and native 256k contexts. |
6. Why Tokenization Matters for Your API Bill
Because AI providers charge per 1M tokens rather than per word or character, tokenizer efficiency directly dictates cost. If Model A uses 1.1 tokens per word on your multilingual codebase while Model B requires 1.8 tokens per word, Model B effectively charges a 63% hidden premium even if their posted per-token rates appear identical.
See how many words fit in a token budget in the tokens to words guide, compare real counts across tokenizers in the tokenizer benchmark, or count your own text with the GPT, Claude and Gemini token counters.
Tokenization FAQ
What is SentencePiece?
SentencePiece is an open-source tokenizer library from Google (Kudo & Richardson, 2018) that trains directly on raw text without splitting on spaces first. It treats whitespace as a normal symbol (▁) and can train either a BPE or a Unigram model. Gemini, Gemma, T5 and Llama 1/2 use SentencePiece tokenizers.
What is the difference between BPE and WordPiece?
Both build a vocabulary by merging symbol pairs. BPE merges the most frequent pair; WordPiece merges the pair that most increases training-data likelihood (pair frequency divided by the frequencies of its parts). WordPiece marks word-continuation pieces with ## and is used by BERT; BPE is used by GPT, Claude, Llama 3+ and most modern LLMs.
What is the difference between BPE and Unigram tokenization?
BPE builds its vocabulary bottom-up by merging pairs. Unigram starts with a large candidate vocabulary and prunes the tokens whose removal hurts likelihood least, then picks the most probable segmentation of each input. Unigram can sample alternative segmentations, which helps with robustness during training.
What is byte-level BPE?
Byte-level BPE runs BPE over UTF-8 bytes instead of Unicode characters, so the base vocabulary is just 256 bytes and any input can be encoded without an unknown token. GPT-2 introduced it; OpenAI's cl100k_base and o200k_base are byte-level BPE tokenizers.
Which tokenizer does ChatGPT use?
Current OpenAI models (GPT-6, GPT-5.6, o3, o4-mini) use o200k_base, a byte-level BPE tokenizer with about 200,000 tokens. Older GPT-4 and GPT-3.5 models used cl100k_base (~100,000 tokens).