Tokenization

Tokenization is the process of splitting raw input such as text into discrete units called tokens, which a model maps to numeric IDs.

Roadmap step 1 of 4: Tokens and embeddings. Try it: Transformer Explainer: tokenization and embedding ↗

What it is

Tokenization converts text, and sometimes other data like audio or image patches, into a sequence of tokens from a fixed vocabulary. For language models, tokens are usually subword pieces, so common words may be a single token while rare words are split into several. A tokenizer is trained or built separately from the model and must be used consistently with it. Common methods include byte-pair encoding, WordPiece and unigram models.

Why it matters

The tokenizer shapes what a model can see and how much it costs to use. Token counts determine context window usage, speed and API pricing. Poor coverage of a language or script can make text longer in tokens, which hurts efficiency and quality, and is a common issue for multilingual and low-resource languages. Quirks such as splitting numbers or code oddly can also affect reasoning and arithmetic.

How it works

A tokenizer learns a vocabulary from a large text corpus, for example by repeatedly merging the most frequent adjacent symbol pairs until a target vocabulary size is reached. At run time it splits input into the longest matching pieces, falls back to smaller pieces or raw bytes for unknown strings, and returns integer IDs. The model looks up an embedding for each ID, and its output IDs are decoded back into text. Special tokens mark things like sequence boundaries or chat roles.