Best on a larger screen — the animation and controls have more room to breathe.
Inputs

Tokenization

Tokenization converts raw text into the integer tokens a language model reads. Byte-Pair Encoding (BPE), the algorithm behind GPT-style models, starts from the raw bytes of the input, repeatedly fuses the adjacent pair with the highest-priority learned merge, and maps each surviving subword to its vocabulary ID.

This lesson runs the real GPT-2 (r50k) tokenizer, in your browser, on whatever you type.

Subword tokens work like building blocks. No model can store a separate entry for every word in every language, yet a vocabulary of roughly fifty thousand subwords can assemble any text (a typo, a brand name, an emoji, or a newly coined word) from pieces it already owns. Type tokenization and it splits into token + ization; type a word that never appeared in training and it still encodes. Byte-level BPE has no "unknown word" token, only smaller known pieces and, beneath them, raw bytes.

How BPE Tokenization Works

Before merging, the tokenizer splits text with a regular expression into pieces: roughly words, runs of digits, and runs of punctuation. A single space directly before a word (or a number or punctuation run) stays attached to the piece that follows it, which is why the tokenizes differently from the; on the canvas, that attached space appears as Ġ. Whitespace with nothing to attach to (a trailing space, the extra spaces in a run, or a newline) becomes a piece of its own. Merges never cross a piece boundary, so two words never fuse into one token.

Inside each piece, encoding starts from individual bytes, with each byte as its own token. The tokenizer then consults its ranked merge table of roughly fifty thousand rules, each saying to fuse two adjacent symbols. A lower rank means higher priority in the learned table, so lower-rank rules always fire first.

On every step, the algorithm finds the single adjacent pair with the lowest rank anywhere in the piece, fuses that pair, and scans again. When no adjacent pair is a learned merge, encoding stops and each surviving token is looked up to get its integer ID. In GPT-2's r50k_base table, IDs 0–255 are the 256 raw bytes, IDs 256–50,255 are the 50,000 learned merges in priority order (so a merge's logged rank doubles as its vocabulary ID), and 50,256 is <|endoftext|>. The model receives those integers rather than the letters.

One key invariant governs the merge order: BPE is greedy by rank, not left to right. It fuses the highest-priority pair available anywhere, even if that means working in the middle of a word before its start. Applied ranks also never decrease. Once the encoder passes a priority, it never returns to a lower rank, because fusing two pieces can only create a longer, later-learned (higher-rank) pair. As a result, the merge log fills top to bottom with non-decreasing ranks.

Tokenization Step by Step

The default input is the word tokenization. It is one regex piece (no spaces), so it begins as twelve raw byte tokens (for plain ASCII, each byte corresponds to its letter):

t o k e n i z a t i o n

The tokenizer then repeatedly fuses the lowest-rank adjacent pair, jumping across positions rather than moving strictly left to right:

t o k e n i z a t i o·n   →   fuse (o, n)  → on
t o k e n i z a·t i on    →   fuse (a, t)  → at
t o k e·n i z at i on     →   fuse (e, n)  → en
t o k en i z at i·on      →   fuse (i, on) → ion
t o k en i z at·ion       →   fuse (at, ion) → ation
t o·k en i z ation        →   fuse (o, k)  → ok
t ok en i·z ation         →   fuse (i, z)  → iz
t ok en iz·ation          →   fuse (iz, ation) → ization
t ok·en ization           →   fuse (ok, en) → oken
t·oken ization            →   fuse (t, oken) → token
token ization
  1. Start. tokenization is twelve byte tokens.
  2. Merge, ten times. Each step fuses the single lowest-rank pair. Notice the tokenizer builds the suffix ization (via at → ation → ization) and the stem token in interleaved order: ation is finished before token begins, because rank rather than position drives the choice. The same suffix ization is reused across organization, realization, and many other words, which is the main benefit of a learned merge table.
  3. Settle. The remaining adjacent pair (token, ization) is not a learned merge, so encoding stops.
  4. Tokens. The result is [token, ization]: two tokens for a twelve-letter word. Token count is not letter count and not word count.
  5. IDs. Each token is looked up in the 50,257-entry vocabulary: token → 30001, ization → 1634. The model receives [30001, 1634], and those integers are all it sees.

On the canvas, cell width reflects token span: a single byte is narrow, a fused chunk is wider, and each merge slides two cells together into a larger piece while the left panel logs the merge and its global rank.

Why Subwords Beat Words and Characters

A fixed word-level vocabulary cannot represent words absent at training time, turning each unseen word into <unk> at inference as new words enter the language. A character-level vocabulary avoids unknowns only for the characters it includes, at the opposite cost: every word becomes a long string of symbols and the model spends capacity relearning spelling.

Subwords sit between those extremes, and byte-level BPE uses the 256 possible bytes as base symbols so nothing is ever out of vocabulary. Type the made-up word fizzlepuff and it encodes into known subwords (f + izzle + puff); type an emoji like 🦄 and it splits into the byte tokens its UTF-8 encoding requires. There is no <unk> fallback, only known pieces, though rare inputs may spell out one byte at a time. Byte coverage is useful, while modern language models use different tokenizer families, vocabularies, and special-token policies.

Token count also determines sequence cost, which you can test in the input box. A common English word is often one token; a rare or misspelled one splits into many; an emoji like 🦄 costs several byte tokens for what looks like one character. Because context windows and API bills are measured in tokens, the distinction between tokens, characters, and words directly determines capacity and cost.

This is the applying half of BPE. Its companion BPE Training lesson is where the merge table is built from corpus frequencies. That lesson uses a small corpus so you can watch the table grow merge by merge; at GPT-2 scale the same procedure yields 50,257 entries (too many to show at once), and every merge applied here follows that learned rank order. Training builds the vocabulary; this lesson applies it.

Edge Cases

  • Nothing is out of vocabulary. A never-seen word (fizzlepuff) decomposes into known subwords; an emoji (🦄) decomposes into raw byte tokens. Byte-level BPE has an atom for every possible byte.
  • A space usually belongs to the next word. the tokenizes differently from the; the leading space is part of the token (shown as Ġ). A trailing space or the extra spaces in a run have no word to join and form their own pieces. Merges never cross a piece boundary, so fix the AI keeps fix, Ġthe, and ĠAI separate.
  • Case matters. Hello and hello are different byte sequences and can tokenize differently because the tokenizer is not case-folded.
  • Rank, not position, decides. When several merges are available the lowest-rank one fires, wherever it sits; the applied ranks never decrease.
  • A single byte is already a token; there is nothing to merge.

Common Mistakes

  • Assuming one word = one token. tokenization is two tokens; a short common word is often one and a long rare one is many. Context limits and bills are counted in tokens, not words.
  • Merging left to right. BPE is greedy by rank. Fusing the leftmost matching pair first can yield a different, incorrect tokenization.
  • Confusing a token with its ID. token is the string; 30001 is its vocabulary index. The model uses the integer ID to look up a learned embedding.
  • Expecting universal IDs. A token's ID depends on the vocabulary its tokenizer was trained with: GPT-2's 30001 means nothing to a different tokenizer, as there is no standard table.
  • Forgetting spaces and case cost tokens too. Whitespace and capitalization change the byte sequence, so they can change which tokens you get and the total token count you are billed for.

A Note on Simplification

This lesson runs a real GPT-2 (r50k) byte-pair encoder: the 50,257-token vocabulary, every merge, and the output token IDs match what the library produces. Other models use different vocabularies, special tokens, and pre-tokenization rules; their sizes and boundaries must be checked for the specific tokenizer. The core encoding procedure (split into bytes, fuse the lowest-rank pair, and map to integers) is the part shared across BPE implementations.