Tokenization
Tokenization is the step that turns text into the pieces an LLM can process. Learn how tokenizers split text, assign token IDs, and handle words, punctuation, numbers, and unfamiliar words.
By the end of this lesson, you will be able to:
- Explain what tokenization does and why LLMs need it.
- Distinguish characters, words, tokens, and token IDs.
- Understand why subword tokenization is useful.
- Inspect real tokenization with Python and a tokenizer library.
- Explain why different models can tokenize the same text differently.
What Is Tokenization?
Tokenization is the process of converting text into a sequence of tokens. A tokenizer applies a vocabulary and rules to decide which pieces of text become tokens.
Words Are Not the Unit the Model Must Use
A tokenizer does not have to split text only at spaces. It may keep common words together while splitting less common words into reusable subword pieces.
helpful
A common word might be represented as one token or a small number of pieces.
unhelpful
A tokenizer can reuse pieces related to un, help, and ful rather than needing every possible word in its vocabulary.
Hello!
The exclamation mark is text too. It can receive its own token or be grouped according to the tokenizer.
Why Use Subword Tokenization?
A vocabulary containing every possible word would be enormous. Subword tokenization lets a model reuse pieces across many words.
Word-only approach
βunhappinessβ is treated as a single vocabulary item.
Subword approach
The tokenizer can compose unfamiliar words from reusable pieces.
This helps balance vocabulary size, sequence length, and the ability to represent uncommon words. It is one reason modern LLM tokenizers are not simply dictionaries of whole words.
Tokenization Happens Before the Model Sees the Numbers
One Sentence Can Contain Different Kinds of Tokens
Consider:
This row is illustrative. A real tokenizer may split 4821, attach spaces to neighboring pieces, or use other boundaries. The lesson is that tokenization is algorithm- and vocabulary-dependent.
Words
Human-friendly units.
Punctuation
Can be tokenized too.
Numbers
May become one or several tokens.
Spaces
May affect token boundaries depending on tokenizer.
Different Tokenizers Can Produce Different Results
There is no universal tokenization of English text. Two models can use different vocabularies and algorithms, so the same sentence can produce different token IDs and token counts.
Tokenizer A
Vocabulary and rules A
tokens β IDsTokenizer B
Vocabulary and rules B
possibly different tokens β different IDsSee Tokenization With Python
For a real tokenizer experiment, you can use a Hugging Face tokenizer. The code below loads a tokenizer and prints the tokens and IDs. The exact output depends on the selected model tokenizer.
from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained(
"distilbert/distilbert-base-uncased"
)
text = "Tokenization helps an LLM process text."
tokens = tokenizer.tokenize(text)
token_ids = tokenizer.encode(text, add_special_tokens=False)
print("Tokens:", tokens)
print("Token IDs:", token_ids)
print("Token count:", len(token_ids))
Change the Text and Experiment
Now change only the input string and compare the token count.
playCompareshort, common wordplayingComparerelated formunpredictableComparelonger wordMachine learning is useful.Comparesentence + punctuationAsk: Which inputs are split into more pieces? Which words are reused as recognizable pieces? This is a practical way to build intuition instead of relying on the rule βone word equals one token.β
Tokenization and Multilingual Text
Token counts can vary significantly across languages and writing systems. A tokenizer's vocabulary is designed around the data used to build it, so different languages may require different numbers of tokens for comparable text.
Do not assume an English word-to-token estimate works equally well for every language. Measure with the tokenizer you actually use.
Common Tokenization Mistakes
False. Words can be split into multiple pieces.
False. Token boundaries depend on the tokenizer.
False. Tokenizers and vocabularies differ.
False. An ID is meaningful within its tokenizer's vocabulary.
Build a Tokenization Explorer
Use the Python example above. Test at least five inputs: a short word, a long word, punctuation, a number, and text in another language. Record the token pieces and token count.
Why do modern tokenizers often use subword pieces?
30-second recap
- Tokenization converts text into model-readable token pieces.
- Tokens are mapped to vocabulary IDs before entering the model.
- Subword pieces help represent common and uncommon words with a reusable vocabulary.
- Punctuation, numbers, spaces, and multilingual text can affect tokenization.
- Different models can tokenize the same text differently.
- When exact counts matter, use the tokenizer associated with the model.