What is tokenization and byte-pair encoding (BPE)?
Tokenization is the step that splits raw text into units, called tokens, and maps each one to an integer ID a model can use. Byte-pair encoding (BPE) is the most common way to build the token list: it starts from single characters and repeatedly merges the most frequent adjacent pair until the vocabulary reaches a chosen size. The result is a vocabulary of subwords, pieces between a letter and a whole word.
Every text model starts with a tokenizer. GPT-2, GPT-4, Llama, and RoBERTa use BPE variants, and BERT uses the closely related WordPiece. The tokenizer decides how long each input is, which sets compute cost and how much text fits in a context window, and it decides which words a model can represent at all. API pricing for language models is also counted in tokens, not words.
Key ideas
- A tokenizer turns text into a sequence of integer IDs from a fixed vocabulary.
- Character vocabularies are tiny but make long sequences; word vocabularies are short but cannot cover rare words.
- BPE builds a subword vocabulary by repeatedly merging the most frequent adjacent pair.
- Every merge adds one vocabulary entry and shortens the encoded corpus.
- New text is encoded by replaying the learned merges in training order, so the output is deterministic.
- Each token ID indexes one row of an embedding matrix, and the model works on those vectors.
Common questions
How many tokens is a word?
It depends on the tokenizer and the language. For English text with GPT-style tokenizers, a common rule of thumb is about 4 characters or 0.75 words per token, so 100 tokens is roughly 75 words. Common words are usually one token, while rare words, names, and numbers split into several.
What is the difference between BPE, WordPiece, and SentencePiece?
BPE merges the pair with the highest raw count. WordPiece, used by BERT, picks the merge that most increases the likelihood of the training data, which favors pairs that are frequent relative to their parts. SentencePiece is a library rather than an algorithm: it treats the space as an ordinary symbol and can train either BPE or a unigram language-model tokenizer directly on raw text.
Why do language models struggle to count letters?
The model sees token IDs, not characters. A word like "strawberry" may arrive as two or three subword tokens, so the individual letters are never directly visible. The model has to have memorized the spelling of each token to answer letter-level questions.
Do tokenizers treat all languages equally?
No. A tokenizer trained mostly on English learns long merges for English words and fewer for other languages, so the same sentence in Thai, Hindi, or Amharic can take several times as many tokens. That makes those languages slower and more expensive to process and leaves less room in the context window.
Related explainers
- Embeddings — Turning words and categories into vectors
- Language Modeling — Predict the next token — the objective that powers GPT, BERT, and modern NLP
- Bag of Words — Turn text into numbers — count word occurrences to create the simplest document representation in NLP
- Word2Vec & Word Embeddings — Learn dense vector representations where similar words live near each other in space
- The Transformer Architecture — A visual guide to 'Attention Is All You Need'
References
- Sennrich, Haddow & Birch (2016), "Neural Machine Translation of Rare Words with Subword Units", ACL
- Kudo & Richardson (2018), "SentencePiece: A simple and language independent subword tokenizer and detokenizer for Neural Text Processing"
- Kudo (2018), "Subword Regularization: Improving Neural Network Translation Models with Multiple Subword Candidates"
- Jurafsky & Martin, "Speech and Language Processing" (3rd ed. draft)
Last updated