Programming
We provide developers and engineers the guides and tutorials they...
Search
We provide developers and engineers the guides and tutorials they...
Content is king. It's not just about how you're going to write, b...
Looking for an engaging connection with thousands of other people...
How LLM tokenization works: tokens vs words, byte pair encoding, vocabularies, token counts and cost, and why models struggle with spelling and maths.
Quick answer: Language models do not read letters or words. Text is first cut into tokens, pieces that may be a whole word, part of a word, a single character or a punctuation mark, and each token is replaced by an ID number. The model only ever sees and produces those numbers. Most models use a subword method such as byte pair encoding (BPE), which keeps common words whole and splits rare words into smaller known pieces. Tokenization matters in practice because context limits and prices are counted in tokens, and because it explains quirks such as models miscounting letters or fumbling long numbers.
A neural network works on numbers, so text must be mapped to a fixed list of units, the vocabulary. There are three obvious choices.
| Unit | Vocabulary size | Problem |
|---|---|---|
| Words | Millions | Cannot handle new words, typos, names or code; a huge table |
| Characters | A few hundred | Sequences become very long, and the model must learn spelling from scratch |
| Subwords | Tens of thousands to a couple of hundred thousand | A workable compromise |
Subword tokenization keeps frequent words as single tokens and builds rare words from pieces:
"cat" -> ["cat"]
"unbelievably" -> ["un", "believ", "ably"]
"Zxqvy" -> ["Z", "x", "q", "v", "y"]
Nothing is ever "unknown": any string can be built from smaller parts, down to single bytes.
Byte pair encoding began as a data compression technique. As a tokenizer, it is trained once on a large body of text.
Training the vocabulary
A toy example with the words low, lower, lowest, newer, newest:
l + o appears often, so merge to lo.lo + w is now frequent, so merge to low.e + r becomes er; e + s + t becomes est.The result is a vocabulary that includes low, er and est, so lowest is two tokens and a new word like slowest can still be built from known parts.
Using it
To tokenize new text, apply the learned merges in the same order. To turn tokens back into text, just join the pieces. The process is exactly reversible.
OpenAI publishes its tokenizer as the open-source library tiktoken. Other families use related methods, such as WordPiece (BERT) and SentencePiece (many multilingual models). Each model has its own vocabulary; token IDs from one model mean nothing to another.
" hello" and "hello" are different tokens."Hello", "hello" and "HELLO" may tokenize differently."1234567" might become ["123", "456", "7"], with no respect for place value.For English text with common tokenizers:
These are approximations. Always count with the actual tokenizer for the model you are using, especially for code or other languages.
So verbose formats cost more. The same data as compact text may use far fewer tokens than deeply nested JSON with long key names.
Ask a model how many times a letter appears in a word, and it may get it wrong. The model never saw the letters. It saw one or two token IDs, and has to rely on what it learned indirectly about how those tokens are spelled. Reversing a string or writing text with exact letter constraints is hard for the same reason.
Digits are grouped into tokens inconsistently, so the model cannot line up columns the way a person or a calculator does. It also predicts the answer left to right, most significant digit first, while carrying works right to left. Models are better at this than they used to be, particularly when allowed to work step by step, but a calculator tool is the dependable solution.
An extra space, a different capital letter or a typo changes the token sequence, which changes every probability the model computes afterwards. See how LLMs predict the next word.
Because unknown words are assembled from familiar pieces, models can often handle new terminology sensibly, and can also be misled when the pieces suggest the wrong meaning.
Each token ID is looked up in a table to fetch its embedding, a vector of numbers that the model actually processes. See how embeddings work. The tokenizer is fixed before training; the embeddings are learned during it.
The same idea extends to other kinds of data. Images are cut into small patches, each treated as a token. Audio is converted into sequences of discrete codes. That is how a single transformer can handle text, images and sound together.
A unit of text that a language model processes: a word, part of a word, a character or punctuation, mapped to a number.
In English, a word averages about 1.3 tokens. Common short words are one token; long or rare words are several.
It processes tokens, not characters. A word may be a single token, so its individual letters are never directly visible to the model.
No. Each model family has its own vocabulary and method, so the same text gives different token counts on different models.
Tokenization is the first thing that happens to your prompt and the last thing that happens to the answer. It is a compromise between words and characters that lets a model handle any text with a manageable vocabulary. Knowing how it works explains token limits and bills, and why a system that writes fluent essays can still miscount the letters in a word.