Tokenization
Splitting text into the meaningful units a model can actually consume.
By the end of this chapter, you'll be able to:
- Split a sentence into tokens using a simple, rule-based tokenizer
- Explain why word-level tokenization breaks down in practice
- Understand what subword tokenization (BPE) fixes
Unicode gets you from a character to a number. But a model doesn't reason about individual characters — it reasons about tokens: the meaningful chunks a sentence gets split into before anything else happens.
A minimal tokenizer
The simplest possible approach: lowercase everything, strip punctuation, and split on whitespace.
import re
class Tokenizer:
def tokenize(self, text: str):
text = text.lower()
text = re.sub(r"[^\w\s]", "", text)
tokens = text.split()
return tokenstokenizer = Tokenizer()
tokens = tokenizer.tokenize("I love AI")
print(tokens)['i', 'love', 'ai']Raw input
"I love AI"
Lowercase
"i love ai"
Strip punctuation
"i love ai" (no punctuation here, so nothing changes this time)
Split on whitespace
['i', 'love', 'ai']
Three words in, three tokens out. That mapping — 'I love AI' → ['i', 'love', 'ai'] — is the entire job of a tokenizer: turn a string into a sequence of
discrete units that everything downstream (vocabulary, embeddings) can operate on.
This tokenizer is intentionally naive
Splitting on whitespace and stripping punctuation is enough to demonstrate the idea, but
it breaks down fast: "don't" becomes "dont", compound words and misspellings each
get their own unique token, and any word the tokenizer hasn't seen before has nowhere to
go. Production tokenizers solve this differently — see below.
Where word-level tokenization breaks
- Vocabulary explosion. Every distinct word form —
run,runs,running,runner— becomes a completely separate token with no learned relationship between them. - Out-of-vocabulary words. A model can only ever produce tokens it was trained on. Encounter a brand-new word, a typo, or a made-up name at inference time, and a pure word-level tokenizer has no way to represent it.
- No cross-lingual sharing. Every language needs its own enormous word list, with zero token overlap between them.
What real tokenizers do instead: subword units
Modern LLM tokenizers (GPT's included) use an algorithm called Byte-Pair Encoding (BPE). Instead of splitting on whitespace, BPE starts from individual characters (or bytes) and iteratively merges the most frequent adjacent pair into a new token — repeating this thousands of times until it has built a vocabulary of subword chunks.
The payoff: common words stay as single tokens, rare or unseen words get split into smaller known pieces instead of failing outright, and the same subword vocabulary works reasonably well across many languages, since it's built from raw bytes rather than a hand-curated word list per language.
Check your understanding
1. What does the tokenizer above do to the string 'AI, ML, NLP'?
2. What problem does Byte-Pair Encoding primarily solve?
Try it yourself
Type any word and replay SIGNAL's byte-pair encoding one learned merge at a time — the same algorithm as the subword tokenizer above.
Try it — Learning Lab
Tokenizer Lab
Type a word and step through every merge SIGNAL's tokenizer learned — from single characters to the tokens a model actually sees.
The question:How does a model split text — even words it has never seen — into pieces it knows?
Loading SIGNAL's tokenizer…
Interview questions
Interview questions
This tokenizer treats "don't" and "dont" as different concerns entirely (it strips
the apostrophe, producing "dont"). Try modifying
python-lab/src/tokenizer.py to special-case common contractions before
stripping punctuation, and see how it changes the resulting vocabulary size on a longer
piece of text.