← Explainer Library

Interactive Explainer

BPE Tokenizer Step by Step

Tokenization is invisible until it breaks. LLMs don't see characters — they see whatever subwords BPE learned to carve out. This page grows a tokenizer merge by merge, so you can watch it glue common pairs and end up with tokens like "ization", "tion", or " the".

~10 minDeep Learning · Tokenization · LLM

Byte-Pair Encoding was invented in 1994 for data compression. It showed up in NLP in 2015 (Sennrich et al.), and became the standard tokenizer for GPT-2 (2019) and everything after. The algorithm is tiny: repeatedly merge the most frequent adjacent pair, stop at target vocab size.

The playground

Current tokenization

Merge history

    Vocab size: 0  ·  Merges applied: 0  ·  Total tokens: 0

    Try this: Press "Initialize (chars)", then "Merge 10". Watch the vocabulary grow from letters into meaningful subwords. Notice how the tokenizer discovers suffixes ("ing", "ed") and common function words ("the") without any linguistic input — just counting.

    Why this matters. Every LLM's vocabulary is frozen at training time — GPT-2 has 50,257 tokens; Llama has 32k. When you type a rare word, it splits into whatever subwords were learned. This is why LLMs spell "strawberry" as "straw" + "berry" and sometimes miscount its letters.

    Tokenization across models

    The three tokeniser families

    Byte-level BPE — handling any Unicode

    A pure-BPE tokeniser breaks on text containing characters it never saw at training. GPT-2 solves this with byte-level BPE: operate on bytes (256 possible) instead of characters, so any Unicode string decomposes into a byte sequence the tokeniser knows. Adds a small overhead (Cyrillic / CJK get ~2× more tokens than Latin) in exchange for never crashing on rare scripts or emoji.

    The 'strawberry has 3 r's' phenomenon

    LLMs often miscount letters in their own output. The reason is tokenisation: "strawberry" is typically split into "straw" + "berry" (or just one token!). The model never sees individual r's; it sees subword tokens, none of which is "r". To answer "how many r's in strawberry?" the model has to decompose its own tokens — which it does imperfectly because the decomposition never appeared during training.

    The same explains: weird arithmetic on numbers tokenised as one piece, off-by-one errors on reversing text, and inability to spell out names character by character. The fix in 2024+ is finer-grained number tokenisation (single-digit tokens for numbers) plus character-level data mixed in during training.

    Reading list