Tokenization is the process that splits raw text into the units, called tokens, that a language model consumes and produces, assigning each an integer id. Word-level vocabularies cannot cover open-ended text, and character-level schemes make sequences impractically long, so modern tokenizers use learned subword units: frequent words become single tokens while rare words decompose into recognizable pieces, leaving no input out of vocabulary.1

The tokenizer is trained once, before the model, and then frozen, and everything downstream is denominated in its output: training corpora are measured in tokens, including the trillions cited for modern pretraining runs, and context windows, API prices, and throughput limits are all quoted per token. The segmentation also fixes what the model can perceive, from the letters inside a word to the text that fits in a window.

Role in the network

Tokenization is the bridge between raw text and the network's arithmetic, in both directions. On the way in, the tokenizer maps text to a sequence of integer ids, and each id indexes a row of the embedding table. On the way out, the model produces a probability distribution over the same vocabulary, an id is sampled or selected, and detokenization concatenates the chosen pieces back into text. Because the model is trained by teacher forcing on token sequences, the tokenizer also defines the prediction targets themselves: different tokenizers define different learning problems on the same corpus. Its decisions sit upstream of, and invisible to, the gradient descent that trains the model.

The mathematics

Byte-pair encoding (BPE) learns its vocabulary by iterative compression. Split the corpus into words, each a sequence of symbols with a count; starting from the alphabet as the vocabulary, count every adjacent pair, merge the most frequent pair into a new symbol everywhere it occurs, and append it to the vocabulary:

$$(a, b)^{*} = \arg\max_{(a,b)} \mathrm{freq}(a, b), \qquad V_{k+1} = V_k \cup \{ab\}$$
Symbol Shape Meaning
\(\mathrm{freq}(a, b)\) scalar adjacent occurrences of the pair within words, weighted by word counts
\(V_k\) set vocabulary after \(k\) merges
\(ab\) symbol new token formed by concatenating the winning pair
\(M\) scalar target number of merges, a hyperparameter

Training stops after \(M\) merges, so \(|V| = |V_0| + M\), plus any special tokens, and the ordered merge list, each entry stamped with its rank, is the tokenizer. Segmenting new text starts from the base symbols and applies the merges in rank order, equivalent to repeatedly merging whichever present pair has the lowest rank.1 Byte-level variants take \(V_0\) as the 256 byte values, so any string is representable: GPT-2's vocabulary is \(256 + 50{,}000\) merges plus one special token, 50,257 in all.2

Worked example: three merges over a toy corpus

Take a corpus of three word types with counts: "low" appearing 5 times, "lower" 2 times, and "owl" 3 times. The base vocabulary is the alphabet \(V_0 = \{l, o, w, e, r\}\), five symbols, and words begin as character sequences. The initial adjacent-pair counts are

$$\mathrm{freq}(o, w) = 5 + 2 + 3 = 10, \quad \mathrm{freq}(l, o) = 5 + 2 = 7, \quad \mathrm{freq}(w, l) = 3, \quad \mathrm{freq}(w, e) = 2, \quad \mathrm{freq}(e, r) = 2$$

so the first merge is \((o, w)\): the token "ow" enters the vocabulary, now 6 symbols, and the corpus rewrites to "l ow" (5), "l ow e r" (2), "ow l" (3). The fresh counts are \(\mathrm{freq}(l, ow) = 5 + 2 = 7\), \(\mathrm{freq}(ow, l) = 3\), \(\mathrm{freq}(ow, e) = 2\), \(\mathrm{freq}(e, r) = 2\), so the second merge is \((l, ow)\): "low" enters, 7 symbols, and the corpus becomes "low" (5), "low e r" (2), "ow l" (3). The remaining counts are \(\mathrm{freq}(ow, l) = 3\), \(\mathrm{freq}(low, e) = 2\), \(\mathrm{freq}(e, r) = 2\), so the third merge is \((ow, l)\): "owl" enters, 8 symbols, and the corpus becomes "low" (5), "low e r" (2), "owl" (3).

After three merges the frequent words "low" and "owl" are single tokens while the rarer "lower" still needs three pieces. The corpus's token count fell from \(5 \times 3 + 2 \times 5 + 3 \times 3 = 34\) characters to \(5 \times 1 + 2 \times 3 + 3 \times 1 = 14\) tokens, from 3.4 tokens per word to 1.4. An unseen word goes through the same merge list in rank order: "below" starts as "b e l o w", rank 1 gives "b e l ow", rank 2 gives "b e low", rank 3 finds no "ow l" pair, and the segmentation is "b e low". Nothing is out of vocabulary; the new word simply decomposes into known pieces.

A tokenizer's tokens-per-word figure on a text is its fertility; its inverse, characters per token, measures compression. OpenAI's documentation gives an English rule of thumb of about 4 characters per token, roughly 0.75 words,3 and fertility is language-dependent: tokenizers trained mostly on English fragment many other languages into several times more tokens per word.4

Intuition

BPE is dictionary compression repurposed as a neural network's alphabet: frequent strings earn short codes, single tokens that cost one embedding lookup and one generation step, while rare strings pay by the piece. The consequence is that the model's perception is quantized at token boundaries. It sees "low" as one indivisible symbol and "lower" as three, and never sees the characters of either, which is why character-level questions are hard: in OpenAI's byte-level vocabularies "strawberry" is three tokens ("str", "aw", "berry" in the cl100k vocabulary of the GPT-3.5 and GPT-4 generation), so counting its letters means reasoning across pieces whose boundaries hide the spelling. Digit handling has the same shape: cl100k groups digits at most three per token ("123456" becomes "123" "456"), creating place-value puzzles inside single symbols, while Llama 3 splits numbers into individual digits.5

Variants and extensions

Sennrich, Haddow, and Birch adapted byte-pair encoding, a 1994 compression algorithm, to neural translation in 2015: starting from characters, the most frequent adjacent pair of symbols is merged iteratively until the vocabulary reaches a target size, and new text is segmented by replaying the merge list.1 WordPiece, used by BERT, merges pairs by likelihood gain rather than raw frequency, scoring a pair by its probability against the product of the parts' probabilities, favoring merges of individually rare pieces;6 the algorithm descends from Schuster and Nakajima's 2012 segmentation method for Japanese and Korean voice search.7 Kudo's unigram language model works in the opposite direction, starting from a large vocabulary and pruning the tokens whose removal least reduces a subword likelihood, which also supports sampling alternative segmentations as regularization;8 BPE-dropout gets a similar effect by randomly skipping merges in training.9 SentencePiece trains BPE or unigram models directly on raw text without pre-splitting on whitespace, which made it the standard for multilingual models such as T5 and the Llama line.10 OpenAI's models use byte-level BPE, applied to UTF-8 bytes so that any string is representable, implemented in the fast tiktoken library.2

A more radical variant removes the tokenizer entirely: byte-level models such as ByT5 read raw bytes and accept the longer sequences,11 and patch architectures such as MEGABYTE and the Byte Latent Transformer group bytes into learned segments, trading sequence length for freedom from a fixed vocabulary.1213

Adoption

Every modern large language model ships with a subword tokenizer from one of these families. BERT's WordPiece vocabulary holds about 30,000 tokens;6 T5's SentencePiece unigram vocabulary 32,000;10 GPT-2 introduced the 50,257-token byte-level BPE vocabulary that GPT-3 inherited.214 The Llama line uses SentencePiece BPE, with Llama 3 enlarging the vocabulary to 128,000 tokens and splitting digits individually.5 Gemma trains a 256,000-token SentencePiece vocabulary,15 the Qwen line uses byte-level BPE with about 151,000 tokens,16 and OpenAI's GPT-4o generation moved to the roughly 200,000-token o200k vocabulary, reported to compress many languages more efficiently than its predecessor.17

Limitations and critiques

Tokenization artifacts surface in model behavior: numbers split into digit groups complicate arithmetic, whitespace variants create near-duplicate tokens, and languages underrepresented in tokenizer training data fragment into many more tokens per word, a documented source of inequity for multilingual models.418 The fragmentation is also economic: because APIs bill per token, the same text costs more in a low-resource language than in English.4 The tokenizer is a frozen commitment, welded to the embedding matrix at pretraining time, so a better segmentation cannot be adopted later without retraining. Vocabulary size is itself a design axis: GPT-3 used a vocabulary of about 50,000, while Llama 3 uses 128,000, trading a larger embedding matrix for shorter sequences.514 Finally, the token interface hides characters from the model, so tasks defined on characters, counting letters, checking spelling, matching rhymes, must be solved across piece boundaries or fail there.

History

Byte-pair encoding began as a general-purpose compression method published by Philip Gage in 1994.19 WordPiece-style segmentation for voice search dates to Schuster and Nakajima in 2012,7 and Sennrich, Haddow, and Birch adapted BPE to neural translation in 2015.1 Kudo's unigram model and the SentencePiece toolkit followed in 2018,810 and BERT carried WordPiece into the pretraining era later that year.6 GPT-2 moved BPE to raw bytes in 2019,2 with BPE-dropout exploring stochastic segmentation the same year.9 Tokenizer-free byte models arrived with ByT5 in 2021,11 vocabulary sizes grew through the Llama 3 and GPT-4o generations,517 and learned byte patching with the Byte Latent Transformer in 2024.13

See also

References


  1. Sennrich, R., Haddow, B., and Birch, A., "Neural Machine Translation of Rare Words with Subword Units," arXiv:1508.07909, August 2015. ↩↩↩↩

  2. Radford, A., et al., "Language Models are Unsupervised Multitask Learners," OpenAI, February 2019. ↩↩↩↩

  3. OpenAI, "Tokenizer," OpenAI platform documentation, accessed July 2026. ↩

  4. Petrov, A., La Malfa, E., Torr, P., and Bibi, A., "Language Model Tokenizers Introduce Unfairness Between Languages," arXiv:2305.15425, May 2023. ↩↩↩

  5. Grattafiori, A., et al., "The Llama 3 Herd of Models," arXiv:2407.21783, July 2024. ↩↩↩↩

  6. Devlin, J., et al., "BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding," arXiv:1810.04805, October 2018. ↩↩↩

  7. Schuster, M., and Nakajima, K., "Japanese and Korean voice search," IEEE ICASSP, March 2012. ↩↩

  8. Kudo, T., "Subword Regularization: Improving Neural Network Translation Models with Multiple Subword Candidates," arXiv:1804.10959, April 2018. ↩↩

  9. Provilkov, I., Emelianenko, D., and Voita, E., "BPE-Dropout: Simple and Effective Subword Regularization," arXiv:1910.13267, October 2019. ↩↩

  10. Kudo, T., and Richardson, J., "SentencePiece: A simple and language independent subword tokenizer and detokenizer for Neural Text Processing," arXiv:1808.06226, August 2018. ↩↩↩

  11. Xue, L., et al., "ByT5: Towards a Token-Free Future with Pre-trained Byte-to-Byte Models," arXiv:2105.13626, May 2021. ↩↩

  12. Yu, L., et al., "MEGABYTE: Predicting Million-byte Sequences with Multiscale Transformers," arXiv:2305.07185, May 2023. ↩

  13. Pagnoni, A., et al., "Byte Latent Transformer: Patches Scale Better Than Tokens," arXiv:2412.09871, December 2024. ↩↩

  14. Brown, T. B., et al., "Language Models are Few-Shot Learners," arXiv:2005.14165, May 2020. ↩↩

  15. Team, G., et al., "Gemma: Open Models Based on Gemini Research and Technology," arXiv:2403.08295, March 2024. ↩

  16. Yang, A., et al., "Qwen2 Technical Report," arXiv:2407.10671, July 2024. ↩

  17. OpenAI, "Hello GPT-4o," OpenAI, May 2024. ↩↩

  18. Rust, P., Pfeiffer, J., Vulic, I., Ruder, S., and Gurevych, I., "How Good is Your Tokenizer? On the Monolingual Performance of Multilingual Language Models," arXiv:2012.15613, December 2020. ↩

  19. Gage, P., "A New Algorithm for Data Compression," C Users Journal, February 1994. ↩