Tokenization is the step that turns your text into the units a language model actually reads. Before any "thinking" happens, the text is chopped into tokens — chunks that sit between whole words and single characters — and each token becomes an integer ID.
Where you meet it
- Every prompt to an LLM (GPT, Claude, Llama) is tokenized before the model sees it.
- Context limits and API bills are counted in tokens, not words or characters.
- Tools like the OpenAI tokenizer or
tiktokenthat show how a string splits.
What it does
A tokenizer maps text to a sequence of integers and back. It owns a fixed vocabulary — typically tens of thousands of entries — where every token has one ID. encode turns your string into IDs the model can embed; decode turns the model's output IDs back into text.
Subwords exist to dodge two traps: a word vocabulary that runs out, and a character one that never says anything.
How it works
The text is split into subword pieces, and each piece is looked up in the vocabulary to get its ID. Frequent words stay whole — the is one token. Rarer words break into known fragments: tokenization → token + ization. The split is a deliberate compromise between two bad extremes:
- Pure words would need a vocabulary of every word and inflection ever seen — and any new word becomes an unknown token (
<unk>), which the model can't represent. This is the out-of-vocabulary (OOV) problem. - Pure characters never run out of vocabulary, but a sentence becomes a very long sequence of near-meaningless symbols — slower and harder to learn from.
Subword tokenizers learn the middle ground from data. The common algorithms are BPE (Byte-Pair Encoding, used by GPT and Llama) and WordPiece (used by BERT): both start from characters and repeatedly merge the most useful adjacent pair until the vocabulary hits a target size. BPE is covered in depth in its own lab.
Watch out
- Token ≠ word ≠ character. A rough rule for English is ~4 characters per token, but it varies — so estimate cost and context usage in tokens, not word count.
- Leading spaces belong to the token.
love(with its space) is usually one token, distinct fromloveat the start of a line. - Non-English text and code fragment harder. German compounds, accented languages, and source code often need noticeably more tokens for the same meaning — and cost more.
- The tokenizer is model-specific. The same string splits differently across GPT-4, Llama, and BERT. A token count from one model doesn't transfer to another.
Go deeper
Tokenization ist der Schritt, der deinen Text in die Einheiten zerlegt, die ein Sprachmodell wirklich liest. Bevor irgendein "Denken" passiert, wird der Text in Tokens zerlegt — Stücke zwischen ganzem Wort und einzelnem Zeichen — und jedes Token bekommt eine ganzzahlige ID.
Wo es vorkommt
- Jeder Prompt an ein LLM (GPT, Claude, Llama) wird tokenisiert, bevor das Modell ihn sieht.
- Kontextlimits und API-Rechnungen werden in Tokens gezählt, nicht in Wörtern oder Zeichen.
- Tools wie der OpenAI-Tokenizer oder
tiktoken, die zeigen, wie ein String zerfällt.
Was es tut
Ein Tokenizer bildet Text auf eine Folge von Integern ab — und zurück. Er besitzt ein festes Vokabular — meist Zehntausende Einträge — in dem jedes Token genau eine ID hat. encode macht aus deinem String die IDs, die das Modell einbetten kann; decode macht aus den Ausgabe-IDs wieder Text.
Subwörter gibt es, um zwei Fallen zu umgehen: ein Wort-Vokabular, das ausgeht, und ein Zeichen-Vokabular, das nichts aussagt.
Wie es funktioniert
Der Text wird in Subword-Stücke zerlegt, und jedes Stück wird im Vokabular nachgeschlagen, um seine ID zu erhalten. Häufige Wörter bleiben ganz — the ist ein Token. Seltenere Wörter zerfallen in bekannte Fragmente: tokenization → token + ization. Diese Zerlegung ist ein bewusster Kompromiss zwischen zwei schlechten Extremen:
- Reine Wörter bräuchten ein Vokabular aus jedem je gesehenen Wort und jeder Flexion — und jedes neue Wort würde zum Unbekannt-Token (
<unk>), das das Modell nicht darstellen kann. Das ist das Out-of-Vocabulary-Problem (OOV). - Reine Zeichen gehen nie aus, aber ein Satz wird zu einer sehr langen Folge fast bedeutungsloser Symbole — langsamer und schwerer zu lernen.
Subword-Tokenizer lernen den Mittelweg aus Daten. Die gängigen Verfahren sind BPE (Byte-Pair Encoding, bei GPT und Llama) und WordPiece (bei BERT): beide starten bei Zeichen und verschmelzen wiederholt das nützlichste benachbarte Paar, bis das Vokabular eine Zielgröße erreicht. BPE wird in einem eigenen Lab ausführlich behandelt.
Worauf achten
- Token ≠ Wort ≠ Zeichen. Eine grobe Faustregel für Englisch sind ~4 Zeichen pro Token, aber das schwankt — schätz Kosten und Kontextverbrauch also in Tokens, nicht in Wortzahlen.
- Führende Leerzeichen gehören zum Token.
love(mit Leerzeichen) ist meist ein Token, anders alsloveam Zeilenanfang. - Nicht-englischer Text und Code zerfallen stärker. Deutsche Komposita, akzentreiche Sprachen und Quellcode brauchen für dieselbe Bedeutung oft deutlich mehr Tokens — und kosten mehr.
- Der Tokenizer ist modellspezifisch. Derselbe String zerfällt bei GPT-4, Llama und BERT unterschiedlich. Eine Token-Zahl von einem Modell überträgt sich nicht auf ein anderes.