Lesson 6.1core2 h 30 min⚡ live demo

Text to numbers: tokenization

How language becomes a sequence of integers — and what gets lost

Every language model — from a 2013 word2vec model to a 2026 frontier LLM — starts by chopping text into tokens and mapping them to integers. The algorithm that does this (byte-pair encoding and its cousins) decides the vocabulary size that sizes the embedding table (7.4 Inside the block: residuals, LayerNorm, FFN and embeddings), how expensive each language is to process (9.9 Production: latency, cost, caching, guardrails, security and privacy), how many tokens fit in a context window (9.6 Generation side: context assembly, citations, structured outputs and tool calling) and a whole catalogue of strange LLM failures that have nothing to do with intelligence and everything to do with tokenization.

You'll be able to

  • Compare character, word and subword tokenization and state the trade-offs each makes
  • Run the BPE training algorithm by hand on a tiny corpus and tokenize a new word with the learned merges
  • Distinguish byte-level BPE, WordPiece and Unigram/SentencePiece and name which models use which
  • Predict tokenization-driven failure modes (arithmetic, spelling, multilingual cost) and budget text in tokens

Key ideas

  1. Tokenization maps text to integer ids from a fixed vocabulary; subwords balance vocabulary size against sequence length and eliminate unknown words.
  2. BPE training: start from characters/bytes, repeatedly merge the most frequent adjacent pair, record merge rules; the number of merges sets the vocabulary size.
  3. Tokenizing new text applies the learned merges in order, so unseen words decompose into known pieces ("low" + "est").
Show 4 more key ideas
  1. Byte-level BPE (GPT-2/4, Llama 3) uses 256 bytes as the base and a regex pre-tokenizer; WordPiece merges by likelihood gain; Unigram/SentencePiece prunes a large vocabulary by likelihood.
  2. Vocabulary sizes grew from 50k to 100k–250k: fewer tokens per text, larger embedding and output layers.
  3. Many LLM weaknesses are tokenization artifacts: arithmetic on chunked digits, letter counting, 2–5× token cost for non-English text, whitespace handling, glitch tokens.
  4. Budget in tokens: ≈ 0.75 English words or 4 characters per token.

Suggested path through this lesson