Tokenization is the process of converting a raw string of text into a sequence of atomic units called tokens, which are the only data format a language model can ingest. Unlike simple word splitting, modern tokenization uses subword algorithms like Byte-Pair Encoding (BPE) to break text into variable-length fragments, allowing the model to handle rare or unseen words by decomposing them into known, smaller morphological components.
Glossary
Tokenization

What is Tokenization?
Tokenization is the foundational preprocessing step that segments raw text into a sequence of discrete tokens from a predefined vocabulary, serving as the fundamental input for a language model.
The output of a tokenizer is a list of integer IDs, each mapping to a specific entry in the model's fixed vocabulary. This vocabulary is learned during training and is immutable during inference. The same tokenizer must be used for both training and inference to ensure semantic consistency; a mismatch between tokenizers results in garbled, nonsensical model outputs.
Key Characteristics of Tokenization
Tokenization is the foundational preprocessing step that converts raw text into a sequence of discrete tokens, bridging the gap between human language and the numerical representations required by large language models.
Subword Segmentation
Modern tokenizers use subword algorithms like Byte-Pair Encoding (BPE) to split text into statistically frequent units. This balances vocabulary size against out-of-vocabulary issues.
- Rare words are decomposed into meaningful sub-units (e.g., 'tokenization' → 'token' + 'ization')
- Common words remain as single tokens to preserve semantic integrity
- Enables models to handle morphologically rich languages and novel compounds without an infinite vocabulary
Vocabulary Construction
A tokenizer's vocabulary is a fixed lookup table mapping integer IDs to specific byte sequences. This vocabulary is learned from the pre-training corpus and frozen before model training.
- Typical vocabularies range from 32,000 to 256,000 tokens
- Includes special tokens:
<|endoftext|>,<|user|>,<|assistant|>for structural control - The vocabulary size directly impacts embedding matrix memory and model throughput
Encoding and Decoding Pipeline
Tokenization is a bidirectional process. Encoding maps raw text to integer IDs; decoding maps IDs back to text. Lossless reconstruction is critical for production systems.
- Encoding:
"Hello world!"→[15496, 995, 0] - Decoding:
[15496, 995, 0]→"Hello world!" - Byte-level tokenizers guarantee round-trip fidelity by operating on raw UTF-8 bytes, preventing character corruption
Context Window Constraints
The context window is the maximum number of tokens a model can process in a single forward pass. Tokenization directly determines how much text fits within this budget.
- A 4,096-token window may hold only ~3,000 English words depending on token density
- Code and non-English languages often consume 2-3x more tokens per semantic unit
- Token counting is essential for prompt engineering and cost estimation in pay-per-token APIs
Tokenization-Induced Biases
The tokenization strategy introduces subtle but measurable biases in model behavior. Splitting patterns affect arithmetic, spelling, and reasoning capabilities.
- Number tokenization: '123' may be split as '12' + '3', disrupting arithmetic understanding
- Whitespace sensitivity: Leading spaces can produce entirely different token sequences for the same word
- Language disparity: Languages with non-Latin scripts often require 4-5x more tokens than English for equivalent semantic content
Special Token Architecture
Special tokens act as control codes that structure conversations, separate documents, and signal generation boundaries. They are integral to instruction-tuned model behavior.
- Chat templates use tokens like
<|im_start|>userand<|im_start|>assistantto delineate turns - Beginning-of-sequence (BOS) and end-of-sequence (EOS) tokens frame generation
- Misconfigured special tokens during inference cause format collapse and degraded output quality
Tokenization Algorithms Compared
A technical comparison of the dominant subword tokenization algorithms used in modern large language models, evaluating their core mechanisms, vocabulary construction, and practical trade-offs.
| Feature | Byte-Pair Encoding (BPE) | WordPiece | Unigram Language Model |
|---|---|---|---|
Core Mechanism | Iteratively merges the most frequent pair of bytes or characters. | Merges pairs based on highest likelihood improvement on training data. | Starts with a large vocabulary and prunes tokens with the lowest probability contribution. |
Training Direction | Bottom-up (starts with characters, builds up). | Bottom-up (starts with characters, builds up). | Top-down (starts with all possible substrings, prunes down). |
Selection Criterion | Frequency of adjacent token pairs. | Score = frequency of pair divided by frequency of individual parts. | Maximizes the likelihood of the training corpus given the current vocabulary. |
Handles Unknown Characters | |||
Reversible Without Ambiguity | |||
Encoding Algorithm | Greedy application of learned merge rules. | Greedy longest-match-first using learned vocabulary. | Viterbi algorithm to find the most probable token sequence. |
Primary Adoption | GPT models (OpenAI), Llama (Meta), Mistral. | BERT, DistilBERT (Google). | SentencePiece (often paired with BPE), XLNet, ALBERT. |
Vocabulary Characteristic | Tends to capture common whole words and frequent subwords. | Similar to BPE but often yields more linguistically plausible splits. | Probabilistic; can generate multiple valid segmentations for a word. |
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.
Frequently Asked Questions
Tokenization is the critical first step in any language model pipeline, transforming raw text into the discrete numerical units that neural networks can process. The following answers address the most common technical questions about how this segmentation works and why specific design choices matter for enterprise deployment.
Tokenization is the process of segmenting a raw text string into a sequence of discrete tokens from a predefined vocabulary, which are then mapped to integer IDs for model ingestion. The tokenizer applies a deterministic algorithm—typically a subword method like Byte-Pair Encoding (BPE) or WordPiece—to split text. For example, the word "tokenization" might be split into ["token", "ization"]. The algorithm operates by iteratively merging the most frequent co-occurring character pairs in a training corpus to build a vocabulary of subword units. This vocabulary is frozen after training. During inference, the tokenizer uses merge rules to decompose input text into the longest possible subwords present in the vocabulary, handling out-of-vocabulary words by breaking them into known fragments. The final output is a list of integer IDs, such as [3001, 4567, 289], which are then passed to the model's embedding layer.
Related Terms
Tokenization is the critical first step in the language model pipeline. The following concepts define how text is segmented, encoded, and optimized for inference.
Byte-Pair Encoding (BPE)
The dominant subword tokenization algorithm used by GPT models. BPE starts with a vocabulary of individual characters and iteratively merges the most frequent adjacent pairs to form new tokens. This creates a deterministic vocabulary that efficiently handles open-vocabulary problems—rare words are decomposed into known subword units rather than replaced with an [UNK] token. The final vocabulary size is a hyperparameter, typically ranging from 50k to 100k tokens.
SentencePiece
A language-independent tokenization framework that treats the input text as a raw byte stream, eliminating the need for language-specific pre-tokenization. It implements both BPE and the Unigram language model algorithm. Unlike standard BPE, SentencePiece normalizes text and manages whitespace by escaping spaces as a special meta-character '▁' (U+2581), ensuring lossless and reversible tokenization.
WordPiece
The subword tokenization algorithm originally developed for the BERT model. Unlike BPE, which merges based on frequency, WordPiece selects merges that maximize the likelihood of the training data. It uses a greedy longest-match-first strategy during encoding and prepends '##' to subword tokens that do not begin a word. This approach is highly effective for handling morphologically rich languages and technical terminology.
Context Window
The maximum number of tokens a model can process in a single forward pass, defining its immediate working memory. The tokenizer directly determines the effective context length—a 4k token window may represent anywhere from 2,500 to 3,500 English words depending on text complexity. Exceeding this limit requires truncation or chunking strategies. Modern models extend this via RoPE Scaling to 128k tokens or more.
Special Tokens
Reserved vocabulary entries that control model behavior rather than representing text. Critical examples include:
<|endoftext|>: Marks document boundaries during pre-training.<|im_start|>/<|im_end|>: Delimit conversational turns in chat-tuned models.<|system|>/<|user|>/<|assistant|>: Define role-specific message segments. Improper handling of these tokens during fine-tuning or inference leads to format corruption and degraded output 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