Tokenization
Abstract
Tokenization is the process of converting text into a sequence of number tokens that a language model can work with.
What is it
A neural network does not understand letters and words directly. It needs numbers. Therefore, before text enters the model, it is cut into pieces — tokens — and each token is assigned a numeric ID from the model's dictionary.
This process is called tokenization.
Why not just take letters or words
| Method | Problem |
|---|---|
| By letters | Too long sequences, lose meaning |
| By whole words | Too large dictionary, unknown words break the model |
| By tokens | Balance: compact dictionary, but meaning is preserved |
A token is a compromise: it can be a whole word, part of a word, or even a punctuation mark.
How it works
- Text is broken into small fragments.
- Each fragment is searched for in the tokenizer's dictionary.
- Each token is assigned a numeric ID.
- The model receives a list of numbers as input.
For example, the word 'tokenization' may become two tokens: token + ization. A short word like 'cat' may become one token.
Why Russian is 'more expensive' than English
Most popular tokenizers are primarily trained on English texts. Therefore, English words are sliced more efficiently, while Russian words are broken into more tokens.
| Text | Approximate number of tokens |
|---|---|
cat |
1 token |
| a Russian word of similar length | 1–2 tokens |
hello world |
2 tokens |
| the same phrase in Russian | 3–4 tokens |
This means that a text with the same meaning in Russian may cost more when paying for API: you pay for each token.
Where else tokenization is important
- Context window is measured in tokens. The longer the tokenization, the faster the context fills up.
- API cost depends on the number of tokens at the input and output.
- Model quality depends on the tokenizer: rare words may be broken and understood worse.