Tokenization is the algorithmic process of segmenting a raw text string into a sequence of discrete atomic units, called tokens, drawn from a fixed, pre-defined vocabulary. These tokens—which can be whole words, subwords, or individual characters—serve as the fundamental, numerically-indexed input representation that a large language model (LLM) can ingest and process. This conversion from human-readable text to machine-readable integers is the mandatory first step in every NLP pipeline.
Glossary
Tokenization

What is Tokenization?
Tokenization is the foundational preprocessing step that converts raw text into a structured sequence of discrete units, or tokens, that a language model can mathematically process.
Modern models predominantly use subword tokenization algorithms like Byte-Pair Encoding (BPE) or WordPiece, which break text into statistically frequent character sequences. This approach elegantly solves the out-of-vocabulary problem by decomposing rare or unseen words into known constituent parts (e.g., "tokenization" becomes ["token", "ization"]), while keeping common words intact. The resulting token count directly impacts the context window limit and inference cost, making token efficiency a critical engineering concern.
Key Characteristics of Tokenization
Tokenization is the critical preprocessing step that converts raw text into a structured sequence of tokens—the atomic units a language model can process. The chosen tokenization algorithm directly impacts vocabulary size, model efficiency, and the ability to handle novel or misspelled words.
Subword Segmentation
Modern tokenization relies on subword segmentation rather than whole-word or character-level splitting. This balances vocabulary size with the ability to represent open-vocabulary text.
- Byte-Pair Encoding (BPE) iteratively merges the most frequent adjacent byte or character pairs to build a vocabulary of subword units.
- WordPiece uses a likelihood-based merging criterion, selecting pairs that maximize the training data's probability.
- Unigram LM starts with a large vocabulary and prunes it by removing tokens that least impact the overall likelihood.
Subword tokenization ensures that rare, misspelled, or novel words can still be represented as sequences of known fragments, preventing the dreaded <UNK> token.
The Token ID Mapping
The tokenizer's output is not text but a sequence of integer IDs. The tokenizer maintains a fixed vocabulary—a bidirectional lookup table mapping each unique subword string to a unique integer index.
- The vocabulary is frozen after training; the model can only understand tokens within this set.
- The total vocabulary size (e.g., 50,257 for GPT-2, 32,000 for Llama) is a critical architectural hyperparameter.
- This mapping is the sole interface between raw human language and the model's embedding layer, which converts each integer ID into a high-dimensional vector.
Special Tokens and Control Codes
Tokenizers inject special tokens that do not correspond to text but serve as structural control signals for the model.
- BOS (Beginning of Sequence) and EOS (End of Sequence) tokens demarcate utterance boundaries and act as generation halting signals.
- PAD (Padding) tokens ensure all sequences in a batch have uniform length for parallel processing.
- UNK (Unknown) tokens represent out-of-vocabulary characters, though modern subword tokenizers largely eliminate their need.
- Control tokens like
<|user|>and<|assistant|>in chat models delineate conversational turns and roles.
Tokenization-Induced Biases
The tokenization algorithm introduces inherent biases that propagate through the model's behavior.
- Integer splitting causes a single number like
380to be tokenized as['38', '0'], destroying its holistic numerical value and forcing the model to learn arithmetic via fragmented representations. - Whitespace handling varies: GPT-style tokenizers treat leading spaces as part of the token, while others do not, causing inconsistent representations of the same word in different contexts.
- Language disparity arises because the training corpus for the tokenizer determines token fertility—the number of tokens needed to represent a concept. A word in a low-resource language may require 4-5x more tokens than its English equivalent, consuming more context window and increasing inference cost.
Token Count vs. Character Count
A common operational pitfall is conflating character count with token count. The relationship is non-linear and language-dependent.
- In English, a token averages roughly 4 characters or 0.75 words. The sentence "I love AI" is 3 tokens, while "I love artificial intelligence" is 5.
- Code and whitespace-heavy formats like JSON are token-inefficient; a single curly brace or indentation space is its own token.
- Non-Latin scripts (e.g., Hindi, Arabic) often require multiple tokens per character due to Unicode byte representation in the tokenizer's training data.
- This discrepancy is critical for context window budgeting: a 4,096-token limit may correspond to anywhere from 2,500 to 3,500 English words, but significantly fewer in other languages.
The Tokenizer Training Pipeline
A tokenizer is a standalone, pre-trained module trained on a raw text corpus before the language model itself is trained.
- The training corpus determines the vocabulary; a tokenizer trained on English Wikipedia will perform poorly on Korean or Python code.
- The vocabulary size is a fixed hyperparameter: too small, and common words fragment into many tokens (increasing sequence length); too large, and the embedding matrix becomes memory-prohibitive.
- Once trained, the tokenizer is deterministic—the same input always produces the same token sequence. This determinism is essential for reproducible inference.
- Tokenizer choice is a binding architectural decision; switching from a GPT-2 tokenizer to a Llama tokenizer requires retraining the entire model from scratch.
Tokenization Algorithms Compared
A technical comparison of the dominant subword tokenization algorithms used to construct vocabularies for large language models, evaluating their segmentation logic, handling of rare tokens, and impact on context window efficiency.
| Feature | Byte-Pair Encoding (BPE) | WordPiece | Unigram LM | SentencePiece |
|---|---|---|---|---|
Core Algorithm | Iterative merge of most frequent byte/character pairs | Greedy merge based on likelihood gain | Probabilistic pruning of a large seed vocabulary | End-to-end tokenizer framework (supports BPE or Unigram) |
Training Objective | Maximize frequency of merged symbol pairs | Maximize language model likelihood of training data | Maximize likelihood of data given a unigram language model | Configurable; defaults to BPE or Unigram LM |
Segmentation Direction | Bottom-up (starts with characters, merges upward) | Bottom-up (starts with characters, merges upward) | Top-down (starts with full words, prunes subword units) | Configurable; depends on underlying algorithm |
Out-of-Vocabulary Handling | Decomposes unknown words into known subword units | Decomposes unknown words into known subword units | Probabilistically segments into known subword units | Decomposes into subword units; whitespace-agnostic |
Whitespace Preservation | Whitespace treated as a token; requires pre-tokenization | Whitespace treated as a token; requires pre-tokenization | Whitespace treated as a token; requires pre-tokenization | Whitespace escaped with meta-character '_' (U+2581); lossless |
Deterministic Encoding | ||||
Vocabulary Size Control | Fixed by number of merge operations | Fixed by target vocabulary size | Fixed by target vocabulary size; prunes low-probability units | Fixed by target vocabulary size |
Primary Adoption | GPT-series (GPT-2, GPT-3, GPT-4), RoBERTa, LLaMA | BERT, DistilBERT, Electra | T5, XLNet, ALBERT | LLaMA, Mistral, Gemma, PaLM, T5 |
Frequently Asked Questions
Clear, technically precise answers to the most common questions about how language models segment text into atomic units for processing.
Tokenization is the foundational preprocessing step that segments a raw text string into a sequence of discrete atomic units called tokens from a fixed vocabulary, which serve as the fundamental input representation for a language model. The process typically employs a subword algorithm like Byte-Pair Encoding (BPE) or WordPiece, which iteratively merges the most frequent character pairs in a training corpus to build a vocabulary that balances granularity and coverage. During inference, an input string is decomposed into these learned subword units, each mapped to a unique integer ID, which is then embedded into a high-dimensional vector for the model's self-attention mechanism. This subword approach elegantly handles out-of-vocabulary words by decomposing them into known constituent parts, ensuring the model can process any arbitrary text input without encountering unknown tokens.
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
Mastering tokenization requires understanding its direct impact on context window limits, attention mechanisms, and the vocabulary construction algorithms that define a model's linguistic capabilities.
Byte-Pair Encoding (BPE)
The dominant subword tokenization algorithm that constructs a vocabulary by iteratively merging the most frequent adjacent pairs of bytes or characters in a corpus. BPE solves the out-of-vocabulary problem by decomposing rare words into known subword units.
- Training Phase: Starts with a base vocabulary of all unique characters; repeatedly merges the highest-frequency bigram until reaching a target vocabulary size (e.g., 50k tokens).
- Inference: Splits an unseen word like "tokenization" into known subwords such as
["token", "ization"]. - GPT Models: OpenAI's GPT series uses a variant of BPE with a 100k+ token vocabulary.
Attention Mechanism
The core architectural component of the Transformer that computes a weighted relevance score for every token relative to every other token in the sequence. Tokenization directly determines the granularity of these attention calculations.
- Self-Attention: Each token attends to all other tokens, including itself, to build a context-aware representation.
- Token Granularity Impact: A word tokenized as
["un", "believ", "able"]forces the model to learn attention patterns across three subword units rather than one holistic representation. - Quadratic Cost: The computational complexity is O(n²) where n is the number of tokens, making tokenization efficiency a direct cost multiplier.
Token Limit
The hard numerical ceiling on the number of tokens an LLM can accept as input or generate as output, governed by the model's architecture and serving infrastructure. Distinct from the context window, the token limit is a strict API or system constraint.
- Input vs. Output: Many models enforce separate limits for input tokens (prompt) and output tokens (completion).
- Tokenizer Variance: The same text produces different token counts across models. "Hello, world!" is 4 tokens in GPT-4 but may be 5–6 in other tokenizers.
- Truncation Strategy: When content exceeds the limit, systems must truncate—often losing critical context from the middle of the document.
Lost-in-the-Middle
A documented performance failure mode where information placed in the center of a long context window is significantly less likely to be retrieved or utilized compared to content at the beginning or end. Tokenization strategy influences how content is positioned within this attention gradient.
- Primacy/Recency Bias: Models exhibit strong recall for tokens at the start (primacy) and end (recency) of the context.
- Chunking Mitigation: Strategic tokenization and semantic chunking can ensure critical facts are positioned at context boundaries.
- Empirical Finding: In multi-document QA tasks, accuracy can drop by 20–30% when the relevant document is placed in the middle of a 10k+ token context.
Semantic Chunking
A content segmentation strategy that divides documents into chunks based on semantic boundaries—paragraph breaks, topic shifts, or embedding similarity thresholds—rather than fixed token counts. The chunking strategy must align with the target model's tokenization scheme.
- Token-Aware Splitting: Chunkers often use the target model's tokenizer to ensure chunks don't exceed the embedding model's token limit (e.g., 512 tokens for
text-embedding-ada-002). - Overlap Strategy: Maintaining a 10–20% token overlap between chunks prevents meaning fragmentation at boundaries.
- RAG Integration: Properly chunked and tokenized content ensures retrieved context fits within the generator model's context window without truncation.

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