Before you move a control in the demo, say what you expect to change.
Foundation reference
Tokenization & Vocabulary Design
Tokenization splits text into tokens, pieces drawn from a fixed vocabulary, and maps each token to an integer ID. Prompt prices and context limits are counted in tokens, not characters, and the computation and latency of generation grow with the number of tokens.
Study guide
Where to start
Why are tokens not words, and what does the model treat as its basic units when a tokenizer mixes whitespace, punctuation and pieces of words?
How to study this pagePredict · Check · Generalize · Continue
Compare what the demo shows with the key equation and with the papers cited below.
Identify the part of the idea that survives a change of notation or model, so you can recognize it in another paper or system.
The newer notebook covers the same concept and has an interactive demo.
Why It Matters for Modern Models
- Tokenization splits text into tokens, pieces drawn from a fixed vocabulary, and maps each token to an integer ID. Prompt prices and context limits are counted in tokens, not characters, and the computation and latency of generation grow with the number of tokens.
- Tokenizer design changes what a model finds easy: code, numbers and non-English text can be compact or split into many pieces depending on where subword boundaries fall.
- Long-context engineering depends on token counts: a "128k context" holds 128k tokens, and the tokenizer decides how much text that is.
- Vocabulary size is an architectural choice: the embedding and output layers grow with |V|, trading parameters for shorter sequences.
- Unicode edge cases (normalization, invisible characters) affect reliability and safety: two strings that look identical can tokenize differently.
Questions to Think About
- BPE merges whatever is frequent in its training text; why do rare strings such as identifiers and long numbers break into many tokens?
- Why does unigram tokenization choose the most probable segmentation rather than the one with the longest tokens?
- Byte-level tokenization never fails on unseen characters, so why does it make non-Latin scripts and emoji more expensive?
- How can Unicode normalization (NFC versus NFKC, zero-width characters, non-breaking spaces) change token boundaries and cost without changing what the text looks like?
Interactive Demo
Core Math
If you prefer intuition first, try the demo above, then come back for the math.
Tokenization is the discrete interface between raw text (or bytes) and a transformer. The model never sees characters or words directly; it sees token IDs from a vocabulary .
1) Tokenization is a segmentation into vocabulary items
A tokenizer maps a string or byte sequence into tokens :
Each token then becomes an integer ID that indexes the embedding table.
2) Unigram tokenization as maximum-likelihood segmentation
The unigram language model used in SentencePiece treats segmentation as probabilistic inference: it chooses the segmentation with the highest total log-probability under per-token probabilities (Kudo, 2018):
In practice this is solved with the Viterbi algorithm, a dynamic program over all possible segmentations.
3) BPE (byte-pair encoding) builds the vocabulary by merges
BPE starts from base symbols (characters or bytes), then repeatedly merges the most frequent adjacent pair (Sennrich et al., 2016):
Each merge adds one token to the vocabulary and usually shortens texts in which that pair is common.
4) Vocabulary size is a real model parameter (embedding and output layer)
Token IDs index the embedding matrix, and the output layer produces logits over the same vocabulary:
So the token-dependent parameters number about
or when the input and output matrices share their weights.
Trade-off: a larger vocabulary usually means fewer tokens per text (cheaper context and a smaller KV cache) but larger embedding and output layers.
What tokenization affects
Tokenization quietly shapes:
- which patterns become single units (code keywords, whitespace runs, common substrings),
- how expensive a prompt is (tokens per character),
- how multilingual and Unicode text is handled (bytes versus scripts versus normalization),
- and some security issues (invisible characters, look-alike characters, normalization).