← CS Lab AI & Machine Learning / Tokenization Open standalone ↗

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

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: tokenizationtoken + ization. The split is a deliberate compromise between two bad extremes:

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 from love at 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

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: tokenizationtoken + ization. Diese Zerlegung ist ein bewusster Kompromiss zwischen zwei schlechten Extremen:

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 als love am 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.

Mehr dazu

Next up
AI & Machine LearningTemperature Sampling AI & Machine LearningContext Window All concepts →