Tokenization in NLP: Definition, BPE and Examples
Quick answer
What tokenization in NLP means, how word, subword and character splits differ, a hand-run BPE trace, and why tokens set LLM limits.
Tokenization in NLP is the step that splits raw text into smaller units called tokens, which are then mapped to numbers a model can process. A token is usually a word or a part of a word. Language models read, count and limit text in tokens, not in characters or sentences.
Key takeaways
- A token is an individual unit of language, usually a word or part of a word.
- Word-level splits give huge vocabularies; character-level splits give very long sequences; subwords sit between.
- Transformers support three subword algorithms: BPE, Unigram and WordPiece.
- A model's context limit and API usage are counted in tokens.
On this page:
- What tokenization means
- How tokenization works, with a BPE trace
- Where tokenization is used
- Common mistakes and confusions
- Related terms
- Frequently asked questions
| Split type | Example for "unhappily" | Trade-off |
|---|---|---|
| Word level | unhappily | Vocabulary becomes extremely large |
| Subword level | un, happi, ly (illustrative) | Handles rare words with a smaller vocabulary |
| Character level | u, n, h, a, p, p, i, l, y | Sequences become much longer |
What does tokenization mean in NLP?
According to IBM, a token in NLP is an individual unit of language, usually a word or a part of a word, that a machine can understand. Tokenization is the process of producing those units.
The same word has another meaning in data security, which this entry does not cover.
How does tokenization work?
The simplest method splits on spaces and punctuation. Modern models use subword methods. The Hugging Face Transformers documentation says Transformers support three subword algorithms: byte pair encoding (BPE), Unigram and WordPiece. For instance, "annoyingly" might be split into "annoying" and "ly", or "annoy", "ing" and "ly", depending on the vocabulary.
The same page notes the trade-offs: word-level vocabularies become extremely large because every unique word needs its own token, while character-level sequences become much longer. As an example of scale, it says GPT-2 uses byte-level BPE with a vocabulary size of 50,257.
A hand-run BPE trace using the toy word counts from the Hugging Face page: hug (10), pug (5), pun (12), bun (4), hugs (5). Start with characters.
| Step | Most frequent pair | Count | New token |
|---|---|---|---|
| 1 | u + g (in hug, pug, hugs) | 20 | ug |
| 2 | u + n (in pun, bun) | 16 | un |
Here is a word-level tokenizer in Python. It is word-level only, so it will not match what an LLM does.
import re
text = "Tokenization is an important NLP task."
tokens = re.findall(r"\w+|[^\w\s]", text)
print(tokens)
print(len(tokens)) # 7The output is the list ['Tokenization', 'is', 'an', 'important', 'NLP', 'task', '.'] and a count of 7. A subword tokenizer would split rarer words into more pieces.
Where is tokenization used?
It is the first step in nearly every language pipeline, from search to chatbots. An illustrative Indian example: a Hinglish message like "kal ka assignment submit karna hai" is broken into pieces before the model sees it. The number of pieces decides what fits in a context window, and usage-based APIs typically count tokens, so it affects cost and limits. No token counts are claimed here.
Read how limits work in context windows explained, and see what an LLM is for the bigger picture.
What are the common mistakes?
- Equating tokens with words. A word can be one token or several.
- Assuming every tokenizer matches. Vocabularies differ, so counts differ by model.
- Splitting on spaces only. Punctuation and rare words need care.
Which terms are related?
Context windows, embeddings and prompts all depend on tokens. See the large language models guide, LLM benchmarks explained and prompt engineering.
Next step
Run the snippet on a sentence in your own language mix, then compare the count with the word count you expected.
Frequently asked questions
What is tokenization in NLP?
Tokenization splits raw text into smaller units called tokens, usually words or parts of words, so a model can process them as numbers.
What is a token in an LLM?
A token is an individual unit of language, usually a word or part of a word. LLMs read, count and limit text in tokens.
Why do LLMs split words into pieces?
Word-level vocabularies become extremely large, while character-level sequences become much longer. Subword splits balance the two and handle rare words.
What is the difference between BPE and WordPiece?
Both are subword algorithms supported by Transformers, along with Unigram. They build their vocabularies by different rules, so the same word may split differently.