Inferensys

Glossary

Tokenization

Tokenization is the fundamental NLP task of segmenting a continuous string of text into discrete units, or tokens, which can be words, subwords, characters, or sentences.
Product manager reviewing autonomous task execution dashboard on laptop, completed tasks visible, casual work session.
DEFINITION

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.

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.

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.

FUNDAMENTAL PROPERTIES

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.

01

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.

02

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.

03

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

04

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.

05

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.

06

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.

SUbWORD SEGMENTATION METHODS

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.

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

TOKENIZATION CLARIFIED

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.

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.