Skip to main content

glossary terms

Tokenizer

Category
LLMs & Generative AI
Difficulty
Intermediate

Definition

A tokenizer is a computational component that breaks down raw text into smaller units called tokens, which are then mapped to numerical identifiers for processing by machine learning models.

How It Works and Context

In modern AI, models do not 'read' text as humans do; they process numbers. A tokenizer acts as the essential pre-processing layer that segments input text into tokens—which can be words, sub-words, or individual characters. Modern LLMs typically use sub-word tokenization (like Byte-Pair Encoding or WordPiece), which balances vocabulary size with the ability to handle rare words or misspellings by breaking them into meaningful fragments. Once segmented, each token is assigned a unique integer ID from the model's vocabulary. This numerical sequence is then converted into dense vectors (embeddings) that the model uses to calculate relationships and predict subsequent tokens. The choice of tokenizer is fixed during a model's training; using a different tokenizer than the one a model was trained on will result in nonsensical output.

Why It Matters

Because LLMs have finite context windows measured in tokens, the efficiency of the tokenizer directly impacts how much information a model can process at once.

Real-world Example

When you input the word 'unhappiness' into an LLM, the tokenizer might split it into three tokens: ['un', 'happi', 'ness']. If the model's tokenizer is inefficient, it might split the same word into many more fragments, consuming more of the model's context window and increasing the cost of the API call. Developers must be aware of these splits to ensure their prompts remain within the model's operational limits.

Common Mistakes

  • Assuming one token always equals one word; in reality, a single word can be multiple tokens, and some tokens can be parts of words.
  • Using a tokenizer that does not match the specific model being used, which leads to 'garbage' input and incoherent model responses.
  • Ignoring the impact of whitespace and punctuation, which are often treated as distinct tokens by many models.
  • Underestimating the cost of tokenization for long documents, leading to unexpected API billing or truncated inputs.

Frequently Asked Questions

Why do models use sub-word tokenization instead of just whole words?

Whole-word tokenization creates an impossibly large vocabulary and fails to handle new, rare, or misspelled words. Sub-word tokenization allows the model to construct any word from a smaller, fixed set of common fragments, making it more robust and memory-efficient.

Can I change the tokenizer of a pre-trained model?

No. The tokenizer is an integral part of the model's architecture. The numerical IDs generated by the tokenizer must correspond exactly to the embeddings the model learned during training. Changing it would require retraining the model from scratch.

How can I estimate how many tokens my text will consume?

Most model providers offer a 'tokenizer' or 'tiktoken' tool that allows you to input text and see exactly how it is segmented and how many tokens it counts as. This is the most accurate way to predict costs and context usage.