Inferensys

Glossary

Tokenization

Tokenization is the process of segmenting raw text into a sequence of discrete tokens from a predefined vocabulary, which serves as the fundamental input for a language model.
ML engineer running AI model benchmarks, performance charts on multiple screens, late night home office setup.
FUNDAMENTAL DATA PREPROCESSING

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.

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.

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.

FUNDAMENTAL MECHANISMS

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.

01

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
02

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
03

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
04

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
05

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
06

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|>user and <|im_start|>assistant to 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
SUbWORD SEGMENTATION STRATEGIES

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.

FeatureByte-Pair Encoding (BPE)WordPieceUnigram 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.

TOKENIZATION CLARIFIED

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.

Prasad Kumkar

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.