Tokenizers (BPE)
For engineers
How byte-pair encoding builds a vocabulary, how real tokenizers are trained and configured, and how tokenizer choices surface as model bugs
110 questions across four stages: Understand, Apply, Solve and Judge.
Try 5 questions
Practice items, not counted towards your mastery. No account needed.
Check yourself · 1 of 5
In one BPE training step, what exactly gets merged?
Check yourself · 2 of 5
Why does byte-level BPE have no unknown-token problem?
Check yourself · 3 of 5
What is the main benefit of a larger BPE vocabulary?
Check yourself · 4 of 5
What does add_generation_prompt=True do in a Hugging Face chat template?
Check yourself · 5 of 5
Why can a completion prompt that ends with a trailing space produce odd output?
Topics
- Byte-pair merge algorithm step by step5 min read · Start from bytes, count adjacent pairs, merge the most frequent pair, record the merge and repeat until the target vocabulary size
- Byte-level BPE and pre-tokenization rules5 min read · UTF-8 bytes remove unknown tokens, and regex pre-splitting on spaces, digits and punctuation decides which merges can ever happen
- Choosing vocabulary size trade-offs5 min read · Larger vocabularies shorten sequences but cost embedding parameters, and multilingual and code coverage push sizes from about 32k to over 200k
- Special tokens for roles and boundaries5 min read · Begin, end, padding and role tokens, how chat templates rely on them, and the injection risk when user text can produce them
- Tokenizer quirks behind model bugs5 min read · Leading spaces, digit splitting, glitch tokens, trailing whitespace and the extra cost of non-English text that show up as odd model behaviour
- Training a tokenizer with real libraries6 min read · Using Hugging Face tokenizers, SentencePiece or tiktoken to train or load a tokenizer, and why swapping a pretrained model's tokenizer needs retraining
Start here
- Byte-Pair Encoding tokenization (Hugging Face LLM course) (opens in a new tab)CourseFree
Walks through BPE training and encoding step by step on a tiny corpus, including the byte-level trick that removes unknown tokens.
- Andrej Karpathy: minbpe and the GPT tokenizer lecture (opens in a new tab)ToolFree
Short, readable Python for training, encoding and decoding BPE; the best code to step through next to the merge algorithm.
- Hugging Face LLM Course: The Tokenizers library (opens in a new tab)CourseFree
Hands-on chapters on training a tokenizer from an old one and on the BPE, WordPiece and Unigram algorithms.
Helps to know first
110 questions · last verified 2026-12