Skip to content
derpx06.dev
WritingExplainersNotesAbout
works on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisationworks on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisation

Learning in public.

A student's notebook on system design, distributed systems and databases. Shared as I learn.

ReadWritingExplainersNotesAbout
ElsewhereGitHubLinkedInHugging FaceLeetCode
Say hellomanaskhairnar1511@gmail.com
derpx06
NORMALmain© 2026 Derpx06Pune, Indiatop ↑
Explainers/Foundations

How do AI tokenizers work? (Why AI can't count the R's in strawberry)

How byte pair encoding learns the pieces a language model reads, and why those pieces make letter counting hard.

Derpx0614 Sept · 6 min read
Contents
The idea in one sentenceHow it worksStep 1: why not words or letters?Step 2: learning the mergesStep 3: using the merges on new textStep 4: bytes instead of charactersStep 5: why letter counting failsWhat the paper foundWhat to be careful aboutSources
0% read · 6 min leftBack to top ↑
On this page0%
Contents
The idea in one sentenceHow it worksStep 1: why not words or letters?Step 2: learning the mergesStep 3: using the merges on new textStep 4: bytes instead of charactersStep 5: why letter counting failsWhat the paper foundWhat to be careful aboutSources

A language model cannot read letters. Before it sees your text, a program called a tokenizer cuts the text into pieces called tokens and replaces each piece with a number. The model only ever sees the numbers. The way those pieces are chosen explains why models are bad at spelling, letter counting and some other tasks, and why the same text can cost different amounts in different languages. This article follows byte pair encoding (BPE), the method behind most tokenizers.

Papers

Neural Machine Translation of Rare Words with Subword Units. Rico Sennrich, Barry Haddow, Alexandra Birch (University of Edinburgh). ACL 2016. arXiv:1508.07909. This paper brought BPE to language tasks. It describes BPE as a compression method by Gage (1994) and adapts it.

Language Models are Unsupervised Multitask Learners (the GPT-2 paper), Radford et al., OpenAI, 2019. PDF. Section 2.2 describes the byte-level version of BPE.

The idea in one sentence

Start with single characters, then again and again join the pair of neighbouring pieces that occurs most often, until the list of pieces is the size you want.

Common words end up as one piece. Rare words split into smaller pieces. Because every character is on the list, any word can be written, so nothing is ever "unknown".

How it works

Step 1: why not words or letters?

Sennrich et al. wanted translation to handle any word, including rare names and long compound words such as the German Abwasserbehandlungsanlage. A list of whole words cannot cover every word. A list of single characters can, but the paper's counts show the cost: for their German training text, characters give 550 million tokens, against 100 million for whole words and 112 million for BPE.

SplittingTokens in the textDifferent pieces (types)
Whole words100 million1,750,000
Characters550 million3,000
BPE112 million63,000

BPE keeps the text nearly as short as whole words, with a small list of pieces.

Step 2: learning the merges

The training text is first split into words, and each word is written as characters. The paper adds a special end-of-word symbol · so the original spacing can be restored. Pairs are not allowed to cross a word boundary. Then repeat:

  1. Count every pair of neighbouring symbols, weighted by how often each word occurs.
  2. Join the most frequent pair into one new symbol, and record this merge.
  3. Stop after the chosen number of merges.

The final list size is the starting characters plus the number of merges. The number of merges is the only setting of the algorithm.

Toy example (our own, checked by running it). Word counts: hug 10, pug 5, pun 12, bun 4, hugs 5. I leave out the end marker to keep it short.

Start
Merge 1
Merge 2
Merge 3
Merge 4

Start with letters: h u g (10), p u g (5), p u n (12), b u n (4), h u g s (5). The pair u g appears 10+5+5=20 times, the most of any pair

Step 1 of 5
Four merges on a toy word list. The merge list is the result of training.

The result is the ordered list of merges: u+g, u+n, h+ug, p+un.

Step 3: using the merges on new text

To split a new word, write it as characters and apply the merges in the order they were learned. Do not recount anything.

  • bugs: b u g s. Merge 1 (u+g) applies: b ug s. No other merge matches. Tokens: b, ug, s.
  • pun: p u n. Merge 1 does not match. Merge 2 (u+n): p un. Merge 4 (p+un): pun. One token.
  • mug: m u g. Merge 1: m ug. Tokens: m, ug.

The word mug was never in the training text, yet it is split into known pieces. The order matters. A later merge can only use pieces that earlier merges made.

Step 4: bytes instead of characters

All text is stored as bytes, which are numbers from 0 to 255. For example, the letter é is two bytes in UTF-8 (195 and 169). The GPT-2 paper points out that a base list of all Unicode characters would be over 130,000 symbols, which is too large compared to the 32,000 to 64,000 token lists usually used with BPE. A byte-level BPE needs a base list of only 256. Any text in any language can be written with it, so there is never an unknown symbol. The GPT-2 vocabulary has 50,257 tokens.

GPT-2 adds one rule. Plain BPE on bytes would learn many separate versions of a common word, such as dog., dog! and dog?. So the paper stops BPE from merging across character categories (letters, punctuation and so on), with an exception for spaces.

Step 5: why letter counting fails

The model receives numbers for pieces. If a tokenizer cuts strawberry as straw + berry, the model gets two numbers. Those numbers do not contain the letters. To count the rs, the model would have to have learned, from its training text, how each piece is spelled. The exact split depends on the tokenizer, and some words are cut so that the model has to rely on that memory. This is a reading problem, not a counting problem.

What the paper found

Sennrich et al. compared BPE with a word-level model that, for unknown words, falls back to a dictionary (WDict). They tested English to German and English to Russian on WMT 2015 data. BLEU is an automatic translation score where higher is better. "Ens-8" means an ensemble of 8 models.

English to German, newstest2015BLEU singleBLEU ens-8Unigram F1, rare words
WDict (baseline)22.024.236.8
BPE, separate 60k lists21.524.540.9
BPE, joint 90k list22.824.741.8

For English to Russian, ens-8 BLEU was 22.8 (WDict), 23.6 (BPE-60k) and 24.1 (joint BPE). The paper says the subword ensembles beat the baseline by 0.3 to 1.3 BLEU. Joint BPE means the merges are learned on the source and target text together, which makes the splits more consistent between the two languages.

The numbers show that BPE helps with rare words in translation. They do not show it is best everywhere. On words never seen in training, the dictionary baseline scored higher in German (F1 36.8 against 33.6 for joint BPE), and a character-pair model got the best BLEU (25.3) in one German setting.

What to be careful about

  • The paper is about translation, not chatbots. Later systems use the same idea, and GPT-2 applied it to bytes, but those later designs are not tested in this paper.
  • The merge list comes from the training text. The paper notes that a piece can end up unknown if all its occurrences were merged into larger pieces, and that only unseen characters are always unknown. Byte-level BPE removes that second case.
  • BPE is greedy. It picks the most frequent pair at each step, not the best list overall. GPT-2 added its category rule because of this.
  • A token is not a word. Token counts depend on the tokenizer, so count with the tokenizer of the model you use.
Key takeaways
  • A tokenizer turns text into numbers, and the model never sees letters.
  • BPE learns merges by repeatedly joining the most frequent neighbouring pair.
  • To tokenize new text, apply the saved merges in the same order.
  • Byte-level BPE starts from 256 bytes, so any text can be written.
  • Letter counting fails because pieces are numbers with no letters inside.

Sources

  • Sennrich, Haddow, Birch. Neural Machine Translation of Rare Words with Subword Units. ACL 2016.
  • Radford, Wu, Child, Luan, Amodei, Sutskever. Language Models are Unsupervised Multitask Learners. OpenAI, 2019.
ShareXLinkedInEmail
Written by Derpx06A developer who builds AI search tools and the code around them. I explain what I got wrong first, so you can skip it. Spotted a mistake? Tell me — corrections get credited.
Research Papers · 6 minLoRA explained: fine-tune a big AI model cheaplyLLM Systems · 7 minWhy do AI chatbots make things up?Distributed Systems · 7 minHow does Raft work? Getting computers to agreeResearch Papers · 5 minMamba explained: an alternative to Transformers
Start here
01Why do AI chatbots make things up?02How does Raft work? Getting computers to agree03Mamba explained: an alternative to Transformers