Inferensys

Glossary

Tokenization

The process of segmenting a raw text string into a sequence of discrete atomic units, or tokens, from a fixed vocabulary, which serves as the fundamental input representation for a language model.
MLOps engineer reviewing model serving infrastructure on laptop, container orchestration visible, technical workspace.
FUNDAMENTAL NLP PREPROCESSING

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.

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.

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.

FUNDAMENTAL MECHANICS

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.

01

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.

02

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.
03

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.
04

Tokenization-Induced Biases

The tokenization algorithm introduces inherent biases that propagate through the model's behavior.

  • Integer splitting causes a single number like 380 to 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.
05

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.
06

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.
SUPERVISED SUBWORD SEGMENTATION

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.

FeatureByte-Pair Encoding (BPE)WordPieceUnigram LMSentencePiece

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

TOKENIZATION

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.

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.