AI Compass
Compass

Tokenisation

How text becomes numbers: byte-pair encoding, why German compounds cost more than English words, and what that means for cost and context.

·2 min read·By Fachredaktion Technik
DETAIL
3 sections

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

EffectPractical consequence
German needs more tokensThe same content costs 20 to 40 percent more
Context windows count tokensA German document fits less far into one
Numbers get splitWhich is why arithmetic is not a language model strength
Rare names get fragmentedTechnical terms occupy disproportionately many tokens

Byte-pair encoding

  1. 01

    Start with individual bytes

    The vocabulary initially contains all 256 possible bytes. Every text is representable, including emoji and other scripts.

  2. 02

    Find the most frequent pair

    Across the whole training text, find the most frequently adjacent pair.

  3. 03

    Merge

    That pair becomes a new token with its own number.

  4. 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  13

What this means for cost

At 3 EUR per million input tokens and a document of 10,000 characters:

LanguageCharacters per tokenTokensCost
English4.02,5000.0075 EUR
German3.23,1250.0094 EUR
German, technical2.83,5710.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.

Bits per character as a comparable quantity

BPC = (T / C) · log₂(PPL)

Only converting to characters makes two models with different vocabularies comparable.

PPL
perplexity per token
T
number of tokens
C
number of characters

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.938 bits per character
  • B: (1/4.0) × log₂ 12 = 0.25 × 3.585 = 0.896 bits 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. 1234 can 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

VocabularyTokens per textOutput layerNote
32,000more32,000 · h parametersOlder models
128,000fewer128,000 · hCommon today
256,000markedly fewer256,000 · hMultilingual 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

CourseFree1500 minEN

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.

Hugging FaceGo to offer
BookFreeEN

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.

Was this page helpful?
Tokenisation