Module 7 · AI infrastructureLesson 79 of 886 minIntermediate
Tokenization
Models don't read characters or words. They read tokens, chunks of text mapped to numbers, and everything from cost to context limits is counted in them.
Try it
Tokenize each sample and compare characters per token. Notice how rare words, code and Japanese need more tokens.
Before a model sees any text, a tokenizer turns it into a list of numbers.
How it splits text
Most modern tokenizers use subword methods such as byte-pair encoding (BPE):
- Start with single bytes or characters.
- Repeatedly merge the most common neighbouring pairs from a huge training corpus into new tokens.
- Stop at a vocabulary size, often 32,000 to 200,000 tokens.
The result: common words (" the", " design") are single tokens, often including the leading space; rare words split into pieces ("un", "believ", "ably"); anything at all can still be encoded, byte by byte if needed.
Rules of thumb
- English: about 4 characters or 0.75 words per token.
- Code: whitespace, symbols and names often take more tokens.
- Many non-English languages, especially non-Latin scripts, use noticeably more tokens for the same meaning.
- Numbers can split oddly ("2026" might be one token or two), which is one reason models are clumsy with arithmetic.
Why it matters for system design
- Cost: APIs charge per input and output token.
- Latency: output is generated one token at a time; more tokens, more time.
- Context limits: a "128k context" model holds 128,000 tokens, not words.
- Chunking for RAG: documents are split by token counts so chunks fit the embedding model and the prompt budget.
- Matching tokenizers: count tokens with the same tokenizer as the model you call, or your estimates will be off.