Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Reading the raw bytes
Computers store text as bytes. An English letter takes one byte, an accented letter or Greek letter two, a Chinese character three, and an emoji four. A byte-level tokeniser starts from just the 256 possible bytes, so it can read absolutely anything, including languages and symbols it has never seen. It then learns to glue frequent byte sequences together. The catch: text in languages that were rare in its training data stays split into many small pieces. The demo counts tokens for a sentence.
Logs, telemetry and documents mix languages, units, symbols and identifiers. Byte-level tokenisers never fail on them, but their token cost varies a lot by script, which affects context limits and price.
- 1. A byte-level tokeniser's base vocabulary has…
- 2. A Chinese character in UTF-8 takes…
- 3. Byte-level tokenisers have unknown tokens…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.By hand
- UTF-8: code points below U+0080 use 1 byte, below U+0800 2 bytes, below U+10000 3 bytes, the rest 4 bytes. Unicode has 1,114,112 code points, far too many for a character vocabulary.
- Byte-level BPE (GPT-2): map the 256 bytes to 256 printable characters (space becomes Ġ), pre-tokenise with a regex, then learn BPE merges on these byte strings. Vocabulary = 256 + merges (GPT-2: 50,257).
- Byte fallback (SentencePiece, LLaMA): a normal subword vocabulary plus 256 byte tokens used only for characters not in the vocabulary.
- Decoding: concatenate bytes and decode UTF-8. A sequence that ends in the middle of a multi-byte character is invalid on its own (shown as U+FFFD), which matters when streaming tokens.
- Byte-only models (ByT5, MegaByte) skip merges entirely: maximal robustness, but sequences 3–4× longer than subwords.
- 1. UTF-8 bytes for an emoji:
- 2. GPT-2's vocabulary size:
- 3. Byte fallback uses bytes…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.Byte-level tokenisation in code
Exercise 1.
- Convert text to UTF-8 bytes and back (lossless).
- Show that half of a multi-byte character cannot be decoded alone.
- Build GPT-2’s mapping from 256 bytes to printable characters.
Practice.
tiktokenand Hugging FaceByteLevelpre-tokenisers implement byte-level BPE; SentencePiece hasbyte_fallback=True.- When streaming generated text, buffer bytes until they form complete UTF-8 characters.
- Measure tokens per word or per character for every language and script you serve.
- Normalise Unicode consistently (NFC) before tokenising, or visually identical strings get different tokens.
import tiktoken
enc = tiktoken.get_encoding("cl100k_base")
for s in ["rocket engine", "moteur de fusée", "ракетный двигатель", "火箭发动机", "🚀🔥"]:
print(s, len(enc.encode(s)))- 1. Streaming decoders must buffer…
- 2. Before tokenising, normalise Unicode with…
- 3. A library exposing OpenAI byte-level encodings:
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Coverage without unknowns. Every string is a byte sequence, so a byte vocabulary has complete coverage by construction. Merges only make frequent sequences shorter; they never remove the ability to represent rare ones.
Where the cost lands. BPE merges are learned from training-data frequencies. Scripts that are rare in the training data get few merges, so their characters stay as 2–4 bytes. Petrov and colleagues (2023) measured up to 15× more tokens for the same content in some languages than in English. That means higher cost, shorter effective context and often lower quality in those languages.
Byte-only models. Removing tokenisation entirely (ByT5) gives robustness to noise and spelling and no language bias from merges. The price is much longer sequences, partly offset by architectures that process groups of bytes (MegaByte, byte latent transformers).
- 1. Merges can never…
- 2. Rare scripts in training data get…
- 3. Byte-only models trade…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.What does byte-level tokenisation cost across scripts?
History.
- A. Radford et al., “Language models are unsupervised multitask learners” (2019), introducing byte-level BPE.
- L. Xue et al., “ByT5: towards a token-free future with pre-trained byte-to-byte models” (TACL 2022).
- A. Petrov, E. La Malfa, P. Torr and A. Bibi, “Language model tokenizers introduce unfairness between languages” (NeurIPS 2023).
Exercise 2.
- Training text: 20,000 words in an English-heavy mixture: 85% English-like, 8% Spanish-like (with accents), 4% Greek, 2% Chinese (400 characters) and 1% emoji.
- Tokenisers:
- a character vocabulary of every character seen in training (298);
- byte-level BPE with 800 merges.
- Test: 2000 words per script, scored separately.
| script | UTF-8 bytes per character | character-vocabulary unknown rate | byte-BPE unknown rate | byte-BPE tokens per character | byte-BPE tokens per word |
|---|---|---|---|---|---|
| English | 1.00 | 0 | 0 | 0.318 | 1.46 |
| Spanish | 1.29 | 0 | 0 | 0.520 | 2.41 |
| Greek | 2.00 | 0 | 0 | 0.715 | 3.13 |
| Chinese | 3.00 | 0.129 | 0 | 2.380 | 3.51 |
| emoji | 4.00 | 0.011 | 0 | 1.770 | 1.77 |
- Bytes cover everything: the character vocabulary missed 12.9% of Chinese characters and some emoji; byte-level BPE never had an unknown symbol.
- But cost follows training frequency: English words take 1.46 tokens; Greek and Chinese words more than twice as many (3.13 and 3.51).
- Rare multi-byte characters stay as bytes: a Chinese character costs 2.38 tokens out of its 3 bytes, so most were never merged.
- Seeds: seeds 1 and 2 gave English 1.48/1.44 and Chinese 3.45/3.32 tokens per word, and character-vocabulary unknown rates of 0.14/0.16 for Chinese.
- Two mistakes I fixed:
- I first seeded the vocabularies with Python’s
hash(), which is randomised per process, so results changed between runs; - I first subtracted one token for the end-of-word marker even when it had merged into the last piece, which gave English 0.46 tokens per word.
- I first seeded the vocabularies with Python’s
Byte-level tokenisation in AI, robotics and aerospace
- International operations: checking token costs and quality for every language used in manuals and crew communication.
- Robust ingestion: byte-level tokenisers never fail on odd symbols in logs, telemetry or code.
- Design lesson: full coverage is not equal cost; measure tokens per word for each language you serve.
References
- A. Radford et al. (2019). “Language models are unsupervised multitask learners.” OpenAI technical report.
- L. Xue et al. (2022). “ByT5: towards a token-free future with pre-trained byte-to-byte models.” TACL 10.
- A. Petrov, E. La Malfa, P. Torr and A. Bibi (2023). “Language model tokenizers introduce unfairness between languages.” NeurIPS.