Text becomes numbers
A model can only do arithmetic, so text must become numbers before it goes in. A tokenizer cuts the text into pieces called tokens and replaces each piece with its number, its token ID. Here is a real sentence cut by the tokenizer of Qwen3.8:
Tokenization␣is␣surprisingly␣important.
Common words are one token each, usually with the space in front included. A less common word, “Tokenization”, is two pieces. The whole list of tokens a tokenizer knows is its vocabulary, and its size is a design choice:
A bigger vocabulary means fewer tokens per text, especially for languages other than English, but a bigger first and last matrix in the model.
| Token | ID |
|---|---|
| The | 760 |
| ␣cat | 7993 |
| ␣sat | 7338 |
| ␣on | 383 |
| ␣the | 279 |
| ␣mat | 5344 |
| . | 13 |
␣ marks a space. These seven numbers are all the model ever receives.
A useful rule of thumb for English: one token is about three quarters of a word, or about four characters. A 1,000-word essay is roughly 1,300 tokens. Prices, speeds and context limits are all counted in tokens, so this rule is worth remembering.
Why not letters, or whole words?
One token per letter
- A tiny vocabulary, and nothing is ever unknown.
- But texts become very long: every letter is one step of the model, so it is slow and the context fills up fast.
- The model has to learn from scratch that c-a-t is a word.
One token per word
- Short sequences, and each token means something.
- But there are millions of words, names and typos, in thousands of languages.
- Any word not in the list becomes “unknown”, and the model is stuck.
Modern tokenizers sit in between, with subword tokens. Frequent words get their own token; rare words are built from frequent pieces, like “Token” + “ization”. Nothing is ever unknown, because in the worst case a word is spelled out from small pieces.
Byte-pair encoding: build a tokenizer by hand
Most tokenizers are built with byte-pair encoding (BPE), an old compression trick adapted for language models in 2015. The idea is simple:
- Start with the smallest pieces: every single character (in modern tokenizers, every one of the 256 possible bytes).
- Count which two pieces appear side by side most often in a large amount of text.
- Merge that pair into one new piece and add it to the vocabulary.
- Repeat, often more than 100,000 times, until the vocabulary is the size you want.
A tiny training text: the words “low” 5 times, “lower” 2 times, “newest” 6 times and “widest” 3 times. Press the button to merge the most common pair, and watch the token count fall.
This interactive part needs JavaScript.
The first merge is “e” + “s” (it appears 9 times, in “newest” and “widest”), then “es” + “t”, then “l” + “o”. After 10 merges, “low”, “newest” and “widest” are single tokens, and the text has shrunk from 79 tokens to 20. The rare word “lower” stays in pieces: “low”, “e”, “r”. A real tokenizer does the same on trillions of characters.
Real tokenizers, real surprises
Once you know that models see tokens, many famous mistakes stop being mysterious.
strawberry
Ask a model how many letter r’s are in “strawberry” and it may get it wrong. It never saw the letters: it saw token number 70135, “ strawberry”, as one indivisible unit. To count letters, it has to have learned how that token is spelled, which is like asking you how many strokes are in a word you have only ever heard. Models that reason step by step can get it right by spelling the word out first, one letter per token.
1234567
1234567
Numbers matter for maths. Older tokenizers cut numbers into irregular chunks (“12”, “345”, “67”), so the model saw the same digit in a different place each time, and arithmetic was harder to learn. Today’s tokenizers use a fixed rule: Qwen gives every digit its own token, while DeepSeek and gpt-oss group them in threes, like the commas in 1,234,567.
def␣add(a,␣b):↵␣␣␣␣return␣a␣+␣b
Spaces count too. In Qwen3.8, “the” at the start of a line is token 1719, but “ the” with a space in front is token 279, and “ The” is 561. To the model these are three different things that it has learned are related.
پرائیویسی
Tokenizers learn their merges from their training text, which is mostly English, Chinese and code. Languages with less text online get fewer merges, so their words break into more pieces. The Chinese for “artificial intelligence”, 人工智能, is one token in Qwen3.8 and DeepSeek-V4. Urdu “پرائیویسی” is five. The lesson on AI in your language measures this on a long text, in the languages of this course.
One token or several, in Qwen3.8’s tokenizer? (All measured.)
-
“ the” (with a space in front)
Answer: One token
-
“strawberry” (no space in front)
Answer: Several tokens
-
“ Islamabad”
Answer: One token
-
“1234567”
Answer: Several tokens
-
“人工智能”
Answer: One token
-
“ChatGPT”
Answer: Several tokens
Special tokens and the chat template
Besides pieces of text, every vocabulary has a few special tokens that never appear in normal text. They mark structure: where a message starts and ends, who is speaking, where the model’s private thinking begins, where a tool call is. Qwen3.8 has 33 of them, including <|im_start|>, <|im_end|>, <think> and <tool_call>.
A chat is not really a back-and-forth. Before every reply, the app or engine turns the whole conversation into one long text, using the model’s chat template (the chat_template.jinja file from the AI stack lesson). Here is what Qwen3.8 actually reads for a short chat, with thinking switched off:
<|im_start|>system
You are a helpful assistant.<|im_end|>
<|im_start|>user
What is a token?<|im_end|>
<|im_start|>assistant
<think>
</think>
- Each message is wrapped in
<|im_start|>plus the speaker’s role, and closed with<|im_end|>. - The text ends by opening the assistant’s turn. The model continues from there, one token at a time.
- With thinking switched off, the template writes an empty
<think>block, so the model answers straight away. With thinking on, it opens<think>and lets the model write its reasoning first. - When the model produces
<|im_end|>, the engine stops: that special token is how a model says it has finished.
Quick check
Remember
- A tokenizer cuts text into tokens and gives each a token ID. In English, one token is about ¾ of a word.
- Vocabularies have 128,000 to 250,000 tokens. Subword tokens mean nothing is ever unknown.
- Byte-pair encoding builds the vocabulary by repeatedly merging the most frequent pair, starting from bytes.
- Tokens explain the strawberry problem, the care taken with digits, the importance of spaces, and why some languages need more tokens.
- Special tokens and the chat template turn a whole conversation into one text; the model stops when it writes its end token.
Words to know
- Token
- A piece of text, often a word or part of a word, that a model reads and writes.
- Token ID
- The number that stands for a token. A model only ever sees these numbers.
- Tokenizer
- The program, with its vocabulary and rules, that turns text into token IDs and back.
- Vocabulary
- The full list of tokens a tokenizer knows, such as 129,280 for DeepSeek-V4.
- Subword
- A token smaller than a word, like “ization”, used to build rare words.
- Byte-pair encoding (BPE)
- Building a vocabulary by repeatedly merging the most common pair of neighbouring pieces.
- Byte-level
- A tokenizer that starts from the 256 bytes, so it can encode any text at all.
- Special token
- A token for structure, not text, such as the start or end of a message.
- Chat template
- The rule for writing a conversation, with roles and special tokens, as the single text a model reads.
Go to the source
This lesson is built on these papers and documents. Paper links open arxiv.org, a separate site.
-
Neural Machine Translation of Rare Words with Subword Units
Where BPE for language came from. The demo’s low / lower / newest / widest example is from this paper.
-
Language Models are Unsupervised Multitask Learners (GPT-2)
Section 2.2 explains byte-level BPE, the approach almost every tokenizer now follows.
-
SentencePiece: A simple and language independent subword tokenizer
A tokenizer that treats spaces as ordinary characters, so it works for languages written without spaces.
-
Language Model Tokenizers Introduce Unfairness Between Languages
Measures how the same text can take many times more tokens in some languages.
-
minbpe (A. Karpathy)
A short, clean BPE tokenizer in Python, with an exercise to build your own.
Check yourself
5 questions. Tap the answer you think is right.
0 of 5 answered
Lesson complete
Answer every question above to complete this lesson.
Your progress is saved on this device only.
Next lessonEmbeddings: where meaning becomes geometry