Byte-pair encoding, step by step

Train a real byte-level BPE tokenizer on your own text, then watch it encode and decode, the same algorithm behind GPT-style language models. Every count, merge, and token id is computed live.

Train the tokenizer

BPE learns a vocabulary from data. It starts with the 256 possible bytes, then repeatedly finds the most frequent adjacent pair of tokens in the corpus and fuses it into a new token.

How training works (30-second read)

1 · The corpus is first pre-tokenized with the GPT-2 rule: split into word-like pieces so merges never cross word boundaries. Notice pieces keep their leading space — ␣low and low are different tokens, exactly as in real tokenizers.

2 · Each piece is turned into raw UTF-8 bytes. The starting vocabulary is every possible byte: ids 0–255. This is why byte-level BPE can encode any text, in any language, with no unknown-token fallback.

3 · Each training step counts every adjacent token pair across the corpus (weighted by how often each piece occurs), picks the most frequent pair, and merges it into one new token with the next free id (256, 257, …). Ties go to the pair seen first.

4 · The ordered list of merges is the tokenizer. Encoding later just replays these rules in learned priority order.

Training corpus

space / → steps

Corpus as tokens

Unique pieces with their counts. Watch pairs fuse into single chips as merges are learned. Colored chips are learned tokens; gray chips are raw bytes.

Pair frequencies

The highlighted pair is the current winner — it merges next.

Learned merge rules

This ordered list is the entire tokenizer. Rule order = priority during encoding.

    the merge list is the whole artifact — this file is a working tokenizer

    Encode & decode

    Encoding replays the learned merges by priority (rank 0 first — the exact GPT-2 loop), never by re-counting. Decoding walks the ids one by one: look up bytes, append, interpret as UTF-8. Step through both below.

    applies to both Play buttons · space / → steps whichever side is mid-run

    Encoder — text → token ids

    Text to encode

    Working tokens

    Each shaded group is one pre-token — merges never cross group boundaries. The highlighted pair merges next because it matches the learned rule shown below (also lit up in the vocabulary panel).

    Decoder — token ids → text

    Token ids

    Paste ids (any separators work) and press Start, or send them over from the encoder.

    Token queue

    The highlighted token is decoded next; faded tokens are already consumed.

    Vocabulary reference

    Ids 0–255 are the raw bytes (always present). Below are the learned tokens in the order they were merged — hover any chip for its id and bytes. This is the lookup table both sides use.

    Vocabulary size vs. token count

    Encodes the text from the encoder box with only the first k merges enabled — the same bytes get cheaper as the vocabulary grows. This is the trade-off real tokenizers tune: bigger vocabulary → fewer tokens per text.