Tokenization: How AI Models Break Text into Tokens
Also known as: Text tokenization · Subword tokenization
Definition
Tokenization is the process of converting text or another input into a sequence of discrete units called tokens, then mapping those tokens to numeric identifiers that a model can process.
Contents
Language models do not receive a sentence as a human-readable string. Before the model processes text, a tokenizer applies a fixed encoding procedure that turns the string into a sequence of token identifiers. A token may represent a whole word, part of a word, punctuation, whitespace, a byte, or a special control marker. The exact split depends on the tokenizer and vocabulary paired with the model.

Simple analogy
Imagine building messages with a box of reusable tiles. The box contains common tiles such as the, ing, punctuation marks, and smaller fragments that can be combined when a complete word is missing. To encode “unbelievable,” one tile set might use un + believ + able; another might use a different split. Each tile has a catalog number, so the final message can be stored as a sequence of numbers.
The analogy has an important limit: a tokenizer does not look up dictionary meanings and then choose the most meaningful split. It follows rules learned or constructed for a particular vocabulary. A split can look odd to a reader while still being valid and useful for the model.
How it works
Build or choose a vocabulary
A tokenizer begins with a finite vocabulary. Word-level vocabularies are simple but struggle with rare names, spelling variants, and languages where words are not separated by spaces. Character-level vocabularies avoid unknown words but create long sequences. Modern language systems often use subword methods that occupy the middle ground: frequent strings can remain intact, while rare strings are assembled from smaller pieces.
Byte Pair Encoding, or BPE, was adapted for neural machine translation by repeatedly merging frequent adjacent symbols into reusable subword units. Unigram tokenization starts from a larger candidate vocabulary and selects a set of pieces using a probabilistic model. SentencePiece provides implementations of BPE and unigram tokenization that can be trained directly from raw text, treating whitespace as an explicit symbol rather than requiring a language-specific word splitter first.
Normalize and segment the input
At encoding time, a tokenizer may first normalize Unicode or whitespace according to its configured rules. It then segments the normalized string into vocabulary pieces. That segmentation is deterministic for many deployed tokenizers, although methods such as subword regularization can sample alternative valid segmentations during training.
Consider the hypothetical text unbelievable!. One tokenizer might produce un, believ, able, and !. A different tokenizer may produce fewer or more pieces because its vocabulary was trained on different data or with a different objective. Token boundaries are therefore model-specific, not universal linguistic facts.
Map pieces to token IDs
Every vocabulary piece has an integer identifier. After segmentation, the tokenizer replaces the pieces with their IDs. The model receives those IDs and looks up a learned vector, called an embedding, for each position. Position information and model layers then transform those vectors; the numeric token ID itself is only an index into the model's vocabulary.
Decoding reverses the mapping: token IDs become pieces, and the pieces are joined according to the tokenizer's rules. Some tokenizers include byte-level fallbacks or an unknown-token mechanism so they can represent strings not covered cleanly by ordinary pieces. Special tokens can mark boundaries, padding, masked positions, roles, or other control structure, but their meanings are defined by the specific model interface.
Why it matters
Tokenization determines sequence length. A phrase that becomes ten tokens consumes twice as much context space as one that becomes five, and it generally requires more model computation. The vocabulary also affects how efficiently names, source code, numbers, and different writing systems are represented. A tokenizer trained mostly on one kind of text may split other text into longer or less convenient sequences.
The tokenizer and model weights form a matched pair. Changing token IDs without retraining or carefully adapting the model breaks the meaning of the embedding lookup: the model would receive familiar numbers attached to unfamiliar pieces. This is why an application must use the exact tokenizer expected by the model.
Tokenization also creates boundaries that can affect behavior. A visible word may span several model steps, while punctuation or a leading space may share a token with nearby characters. Token counts therefore cannot be inferred reliably by counting words or characters. The only dependable method is to run the model's tokenizer.
Practical use cases
- Preparing model input: Applications tokenize prompts, retrieved passages, tool results, and generated history before sending them to a model.
- Managing context limits: Token counts help systems decide when to truncate, summarize, or retrieve only the most relevant material.
- Estimating workload: Many inference systems measure throughput and resource use in input and output tokens rather than words.
- Training language models: A training pipeline learns or selects a vocabulary, encodes the corpus, and teaches the model to operate over the resulting token sequences.
- Supporting multilingual text: Subword and byte-aware methods can represent writing systems and rare strings without requiring every possible word in the vocabulary.
Tokenization is not limited to text. Vision systems may treat image patches as tokens, audio systems may use encoded frames or learned units, and multimodal models may combine several token types. Those representations share the idea of discrete model inputs, but they are not necessarily produced by a text tokenizer.
Common misconceptions
“One token equals one word.” A token can be a word, subword, character sequence, byte, punctuation mark, whitespace-bearing piece, or special marker. The relationship varies by tokenizer and input.
“All models split the same text the same way.” Token vocabularies and normalization rules differ. The same sentence can have different token counts and boundaries across models.
“The tokenizer understands meaning.” Vocabulary construction uses statistics and optimization rules, but tokenization alone does not perform the contextual reasoning of the language model. A fragment's meaning is developed through learned embeddings and model layers.
“Detokenization always reconstructs any original string perfectly.” Some schemes, including SentencePiece under its documented assumptions, are designed for reversible text conversion. Other pipelines may normalize or discard distinctions before segmentation, so exact recovery depends on the configured process.
Frequently asked questions
Why do unusual names often use more tokens?
A vocabulary contains pieces that were useful in its training data. An uncommon name may be absent as one piece, so the tokenizer represents it with several smaller subwords or bytes.
Can two tokenizers assign different IDs to the same visible piece?
Yes. Token IDs are local indexes in a particular vocabulary. Their numeric values have no universal meaning across tokenizers or models.
Does a larger vocabulary always make a model better?
No. A larger vocabulary can shorten some sequences but increases the embedding table and may allocate capacity inefficiently. Vocabulary size is a design tradeoff, not a standalone quality score.
Why must an application count tokens before sending a long prompt?
Models accept a bounded sequence, and both the prompt and generated continuation occupy that space. Counting with the correct tokenizer prevents unexpected truncation or rejected requests.
