primitive

tokenization

How raw text becomes the integer IDs models actually see β€” BPE, WordPiece, SentencePiece β€” and why tokenizer quirks explain so many otherwise-baffling LLM failures.

A language model never sees text. It sees a sequence of integers, each naming an entry in a fixed vocabulary, and everything downstream β€” embeddings, attention, the output head’s prediction β€” operates on those IDs. Tokenization is the deterministic preprocessing step that chops raw bytes into that sequence, and it embodies a trade-off: characters give a tiny vocabulary but absurdly long sequences; whole words give short sequences but an unbounded vocabulary that can’t handle typos or new words. Subword tokenization splits the difference β€” common words stay whole, rare words decompose into recognizable pieces (tokenization β†’ token + ization).

It is also the layer where many famous LLM failures actually live. A model that can write poetry but can’t count the letters in β€œstrawberry” or reverse a string isn’t bad at reasoning β€” it literally cannot see characters, only opaque chunks.

How it works

Byte-pair encoding (BPE), the dominant algorithm, is learned from a corpus before model training ever starts:

  1. Start with a base vocabulary of single bytes (so anything is representable).
  2. Count which adjacent pair of tokens occurs most often; merge it into a new vocabulary entry.
  3. Repeat until the vocabulary reaches a target size (typically 32k–200k entries).

Encoding new text replays those merges in order. The result is a frequency-driven vocabulary: common English words become single tokens, rare strings shatter into many. WordPiece (used by BERT) scores merges by likelihood instead of raw frequency, and SentencePiece runs directly on raw text so the same machinery works for languages without spaces.

The token IDs then index an embedding table β€” tokenization decides what the atoms are; embeddings decide what they mean.

Why tokenizer quirks matter

Sources

browse all sources β€Ί