How a model reads

Before a language model sees a sentence, a tokenizer cuts it into pieces. Byte-pair encoding learns those pieces by counting: it finds the most frequent pair of neighbouring symbols, fuses it into one new symbol, and repeats. This one is character-level and learned 2,000 merges from 1,115,394 characters of Shakespeare. Drag the slider to watch the pieces grow.

Byte-pair encoding, trained from scratch

Base characters
65
Merges learned
2,000
Vocabulary
2,065
First pair count → last
23,837 → 34
Training text, in tokens
1,115,394 → 384,882
Chars per token, at the end
2.90
How it works

Tokens

Use the left and right arrow keys to step through the tokens one at a time.

0of 2,000 merges applied

The training text, compressed

How it works

  1. Pre-split. The text is cut into chunks, a simplified version of GPT-2’s rule (Radford et al., 2019): a word with its leading space, a run of digits, a run of punctuation, or whitespace. Merges never cross a chunk edge, so can exist but a token spanning two words cannot. The rule is one regular expression:
  2. Start from characters. Every distinct character in tiny-shakespeare is a token. There are 65. The text has 15,056 distinct chunks, each counted by how often it appears.
  3. Count and fuse. Count every pair of neighbouring tokens inside the chunks. Fuse the most frequent pair into a new token everywhere it occurs. Ties go to the pair with the smallest ids. Repeat 2,000 times, logging each merge. The whole run takes about 3 seconds in plain Python.
  4. Read new text. Split it the same way, start from characters, then keep applying the earliest-learned merge that fits until none do. The slider simply limits which merges are allowed. Characters never seen in training have no merges, so they stay alone.
  5. Check. The tokenizer on this page is a few dozen lines of JavaScript reading the exported merge list. On a test set of training lines, new sentences, code and unusual characters, its output equals the Python trainer's token for token.

How it was made: idea, direction and every taste call by Joe; code written by Claude Code under his review.

What BPE does not do

  • It has no idea what anything means. became a token at merge only because and sat next to each other times. Frequency is the only signal.
  • Its pieces depend on the training text. The script format repeats speaker names, so is a single token (merge 1,482), while a modern word falls apart: . Trained on other text, the same word would likely split differently.
  • It does not know two spellings are one word. , , and are unrelated ids to the model that reads them.
  • It cannot handle what it never saw. This version starts from the characters of Shakespeare, so leaves the new characters outlined and unmerged. GPT-2-style (byte-level) tokenizers start from raw bytes instead, so nothing is unknown, though rare text still costs many tokens.
  • It is small. 2,000 merges on 1,115,394 characters of one author. Production tokenizers learn far more merges from far more varied text.