Tokenisation
How text becomes numbers: byte-pair encoding, why German compounds cost more than English words, and what that means for cost and context.
The idea
A model computes with numbers. Before text goes in, it is cut into pieces and each piece gets a number. Those pieces are called tokens and are usually shorter than a word and longer than a letter.
The German "Rechnungsprüfung" becomes roughly Rech | nungs | prüfung, three
tokens. The English "invoice check" is two.
What follows
| Effect | Practical consequence |
|---|---|
| German needs more tokens | The same content costs 20 to 40 percent more |
| Context windows count tokens | A German document fits less far into one |
| Numbers get split | Which is why arithmetic is not a language model strength |
| Rare names get fragmented | Technical terms occupy disproportionately many tokens |
Byte-pair encoding
- 01
Start with individual bytes
The vocabulary initially contains all 256 possible bytes. Every text is representable, including emoji and other scripts.
- 02
Find the most frequent pair
Across the whole training text, find the most frequently adjacent pair.
- 03
Merge
That pair becomes a new token with its own number.
- 04
Repeat
Until the target vocabulary size is reached, usually 32,000 to 200,000.
# Rules of thumb for a quick estimate, without a library:
def estimate_tokens(text, language="en"):
chars_per_token = 3.2 if language == "de" else 4.0
return round(len(text) / chars_per_token)
de = "Die Rechnungsprüfung erfolgt durch die Fachabteilung."
en = "The invoice check is carried out by the department."
print(estimate_tokens(de, "de"), estimate_tokens(en, "en")) # 16 13What this means for cost
At 3 EUR per million input tokens and a document of 10,000 characters:
| Language | Characters per token | Tokens | Cost |
|---|---|---|---|
| English | 4.0 | 2,500 | 0.0075 EUR |
| German | 3.2 | 3,125 | 0.0094 EUR |
| German, technical | 2.8 | 3,571 | 0.0107 EUR |
The 25 to 43 percent premium is not a rounding error but a structural disadvantage that belongs in every cost estimate.
Why perplexity becomes incomparable
Perplexity is measured per token. A tokeniser that splits the same text into more pieces spreads the same information over more predictions and lowers perplexity without the model being better.
Worked through: model A has PPL = 8 at 3.2 characters per token; model B has
PPL = 12 at 4.0.
- A:
(1/3.2) × log₂ 8 = 0.3125 × 3 = 0.938bits per character - B:
(1/4.0) × log₂ 12 = 0.25 × 3.585 = 0.896bits per character
Model B is the better language model despite the higher perplexity. A raw perplexity comparison would have suggested the opposite.
Practical traps
- A leading space is part of the token.
" invoice"and"invoice"are different tokens with different ids. - Numbers split differently by tokeniser.
1234can be one token or four, which makes arithmetic harder still. - Special characters and emoji often need several tokens although they are one character.
- A vocabulary without your domain vocabulary fragments every technical term and consumes more context.
Vocabulary size as a trade-off
| Vocabulary | Tokens per text | Output layer | Note |
|---|---|---|---|
| 32,000 | more | 32,000 · h parameters | Older models |
| 128,000 | fewer | 128,000 · h | Common today |
| 256,000 | markedly fewer | 256,000 · h | Multilingual models |
At h = 4096 the output layer costs 131 million parameters at 32,000 entries and
1.05 billion at 256,000. A larger vocabulary saves context and compute per text
but costs memory: the trade every model family decides afresh.
Related courses and sources
Hugging Face NLP course
Tokenisation, transformers, fine-tuning and deployment, with code throughout. Assumes Python, and in exchange you end up working with real models.
For development with language models once it has to go beyond calling an interface.
Speech and Language Processing
Jurafsky and Martin, the standard work on language processing, free chapter by chapter. Covers classical methods and language models in one arc.
For anyone learning language processing systematically, classical and modern in one arc.