Tokenization is the computational process of dissecting a raw character sequence into a sequence of tokens—words, subwords, characters, or sentences. It is the critical first step in any NLP pipeline, transforming unstructured text into a structured symbolic representation that a machine learning model can ingest. The granularity of the token directly impacts a model's vocabulary size and its ability to handle out-of-vocabulary (OOV) terms.
Glossary
Tokenization

What is Tokenization?
Tokenization is the foundational NLP task of segmenting a continuous string of text into discrete units called tokens, which serve as the atomic inputs for all subsequent language processing and modeling.
Modern tokenization strategies, such as Byte-Pair Encoding (BPE) and WordPiece, operate on the subword level to balance vocabulary size against semantic representation. These algorithms statistically merge frequent character pairs, allowing the tokenizer to decompose rare or unseen words into known, meaningful fragments. This subword approach is a core architectural component of Transformer models, enabling them to process open-vocabulary language without resorting to a generic unknown token.
Key Characteristics of Tokenization
Tokenization is the foundational NLP preprocessing step that segments raw text into discrete units called tokens. The strategy chosen—whether word, subword, or character-level—directly impacts a model's vocabulary size, handling of out-of-vocabulary terms, and downstream task performance.
Granularity Spectrum
Tokenization operates on a spectrum from coarse to fine granularity, each with distinct trade-offs:
- Word Tokenization: Splits on whitespace and punctuation. Simple but creates large vocabularies and fails on unseen words.
- Subword Tokenization: Breaks words into meaningful fragments (e.g., 'un' + 'break' + 'able'). Balances vocabulary size with OOV handling.
- Character Tokenization: Treats each character as a token. Minimal vocabulary but produces very long sequences, challenging for transformers.
Modern LLMs overwhelmingly use subword algorithms like Byte-Pair Encoding (BPE) or SentencePiece to navigate this trade-off.
Out-of-Vocabulary Mitigation
A primary function of tokenization is eliminating the Out-of-Vocabulary (OOV) problem. Word-level tokenizers fail catastrophically on novel terms like 'GPT-4o' or misspelled words, mapping them to a generic <UNK> token.
Subword tokenizers solve this by recursively decomposing unknown words into known constituent parts:
- 'unhappiness' → 'un' + 'happiness'
- 'happiness' → 'happi' + 'ness'
This guarantees that any input text can be represented, even if the full word was never seen during training. The Byte-fallback strategy in models like GPT extends this further by encoding raw bytes, ensuring universal coverage.
Vocabulary Construction via BPE
Byte-Pair Encoding (BPE) is the dominant vocabulary construction algorithm. It starts with a base vocabulary of individual characters and iteratively merges the most frequent adjacent pair until a target vocabulary size is reached.
Key properties:
- Data-driven: The vocabulary is learned from the training corpus, not predefined by linguistic rules.
- Frequency-based: Common words remain intact as single tokens; rare words get split.
- Reversible: The merge table allows perfect reconstruction of the original text from token IDs.
This is why 'the' is a single token in GPT models, while 'tokenization' might be split into 'token' + 'ization'.
Tokenization-Aware Preprocessing
Effective tokenization requires upstream text normalization to produce consistent tokens:
- Case Folding: Lowercasing ensures 'Apple' and 'apple' map to the same token, unless case carries semantic meaning (e.g., 'Apple' the company vs. 'apple' the fruit).
- Unicode Normalization: NFC or NFD forms ensure visually identical characters have identical byte representations.
- Contraction Expansion: Splitting 'don't' into 'do' + "n't" prevents a proliferation of contracted tokens.
Inconsistent preprocessing creates token mismatch, where semantically identical text produces different token sequences, degrading retrieval and generation quality.
Context Window Economics
Tokenization directly governs the economics of transformer inference. The context window—the maximum sequence length a model can process—is measured in tokens, not words or characters.
Implications:
- A 128k token context window may hold only ~96k English words due to subword splitting.
- Non-English languages often tokenize less efficiently; the same sentence in Burmese can consume 4-5x more tokens than in English.
- Token billing: API costs are per-token, making tokenization efficiency a direct cost driver.
Choosing a tokenizer optimized for your target language and domain can significantly reduce inference latency and expense.
Deterministic Reversibility
A well-designed tokenizer is deterministic and losslessly reversible. Given the same input string and tokenizer, the output token sequence must be identical every time. The detokenization process must reconstruct the original text exactly.
Challenges to reversibility:
- Whitespace ambiguity: Tokenizers like BPE use special prefix symbols (e.g., 'Ġ' in GPT-2) to encode space location, enabling perfect reconstruction.
- Normalization drift: If the detokenizer applies different Unicode normalization than the tokenizer, the output text will differ from the input.
This property is critical for code generation, where a single character error breaks syntax, and for retrieval systems where exact string matching is required post-generation.
Tokenization Strategies Compared
Comparison of the three dominant subword tokenization algorithms used in modern transformer-based language models, evaluating their approach to vocabulary construction and handling of out-of-vocabulary terms.
| Feature | Byte-Pair Encoding (BPE) | WordPiece | Unigram Language Model |
|---|---|---|---|
Core Algorithm | Iterative byte/character pair merging by frequency | Greedy merging by likelihood gain | Probabilistic pruning of subword units |
Vocabulary Construction | Bottom-up: starts with characters, adds merges | Bottom-up: starts with characters, adds merges | Top-down: starts with large seed, prunes low-probability units |
Training Objective | Maximize frequency of merged symbol pairs | Maximize language model likelihood of training data | Maximize likelihood under unigram language model with EM |
Tokenization at Inference | Deterministic: applies learned merge rules in order | Greedy longest-match-first from vocabulary | Probabilistic: selects most likely segmentation path |
Handles OOV Words | |||
Subword Granularity | Variable: common words stay intact, rare words split heavily | Variable: favors more granular splits for rare words | Variable: multiple segmentation candidates with probabilities |
Vocabulary Size Control | Fixed by number of merge operations | Fixed by target vocabulary size | Fixed by pruning threshold and target size |
Original Application | GPT series (GPT-2, GPT-3, GPT-4) | BERT, DistilBERT | XLNet, ALBERT, T5 (SentencePiece) |
Frequently Asked Questions
Tokenization is the foundational first step in any NLP pipeline, transforming raw text into structured, machine-readable units. These answers address the most common technical questions about how tokenizers work, why they fail, and how to choose the right strategy.
Tokenization is the fundamental NLP task of segmenting a continuous string of text into discrete units called tokens, which can be words, subwords, characters, or sentences. The process works by applying a set of rules or a trained statistical model to identify boundaries within the text. A simple whitespace tokenizer splits text on spaces and punctuation, turning "The cat sat." into ["The", "cat", "sat", "."]. More sophisticated subword tokenizers like Byte-Pair Encoding (BPE) or WordPiece use frequency-based merging algorithms to break rare words into smaller, meaningful fragments, such as splitting "unhappiness" into ["un", "happiness"] or ["un", "happi", "ness"]. This segmentation is the critical bridge between raw human language and the numerical vectors that machine learning models require for computation.
Enabling Efficiency, Speed & Accuracy
Intelligent Analysis, Decision & Execution
We build AI systems for teams that need search across company data, workflow automation across tools, or AI features inside products and internal software.
Talk to Us
Search across company data
Give teams answers from docs, tickets, runbooks, and product data with sources and permissions.
Useful when people spend too long searching or get different answers from different systems.

Automate internal workflows
Use AI to route work, draft outputs, trigger actions, and keep approvals and logs in place.
Useful when repetitive work moves across multiple tools and teams.

Add AI to products and internal tools
Build assistants, guided actions, or decision support into the software your team or customers already use.
Useful when AI needs to be part of the product, not a separate tool.
Related Terms
Tokenization is the foundational first step in any NLP pipeline. Understanding these adjacent concepts is critical for designing robust text normalization and semantic search systems.
Part-of-Speech Tagging
The process of assigning a grammatical category—such as noun, verb, or adjective—to each token. POS tagging is a critical prerequisite for lemmatization and syntactic parsing, as the correct lemma depends on the word's part of speech.
- Enables disambiguation of homographs like 'lead' (noun vs. verb)
- Typically uses a Hidden Markov Model or transformer-based architecture
- Essential for accurate dependency parsing
Stop Word Filtering
The removal of high-frequency, low-information words—such as 'the', 'is', and 'at'—from a token stream. This reduces dimensionality and noise in the feature space, improving computational efficiency for sparse retrieval models like BM25.
- Stop word lists are language-specific and domain-dependent
- Modern dense retrieval models often retain stop words for context
- Over-aggressive filtering can destroy phrase semantics
N-gram Extraction
A contiguous sequence of N tokens from a text sample. Moving beyond unigrams to bigrams and trigrams captures local word order and multi-word expressions like 'New York' or 'machine learning'.
- Unigram: single token
- Bigram: two-token sequence
- Trigram: three-token sequence
- Essential feature for traditional text classification and language modeling
Case Folding
The normalization step of converting all characters to a single case, typically lowercase, to ensure that tokens like 'Apple' and 'apple' are treated identically. This reduces vocabulary size and improves recall.
- Critical for case-insensitive search engines
- Can destroy information for Named Entity Recognition (e.g., 'Apple' the company vs. 'apple' the fruit)
- Often combined with truecasing for downstream tasks
Out-of-Vocabulary (OOV) Handling
The challenge of managing tokens encountered during inference that were absent from the model's fixed vocabulary. Subword tokenization strategies like BPE and WordPiece mitigate OOV issues by decomposing unknown words into known subword fragments.
- Character-level tokenization eliminates OOV entirely but loses semantic granularity
- The
[UNK]token is a fallback in word-level vocabularies - OOV rate is a key metric for tokenizer quality

About the author
Prasad Kumkar
CEO & MD, Inference Systems
Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.
His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.
Partnered with leading AI, data, and software stack.
How We Work
Custom AI workflows for your Business
One-fit-all AI don't work for modern businesses. At Inferensys, we aim to understand your business & custom requirements; which we use to define most efficient agentic workflows, the data, and the tools for your business.
01
Review the use case
We understand the task, the users, and where AI can actually help.
Read more02
Pick the right approach
We define what needs search, automation, or product integration.
Read more03
Build the first useful version
We implement the part that proves the value first.
Read more04
Improve from there
We add the checks and visibility needed to keep it useful.
Read moreThe first call is a practical review of your use case and the right next step.
Talk to Us