Skip to content

Glossary term

What is a tokenizer?

AI, in DepthBy Published Last checked

A tokenizer is the part of a language model's pipeline that splits text into tokens, small pieces such as words, parts of words or punctuation, and turns each one into a number the model can process.

What does a tokenizer do?

A model never sees your words directly. The tokenizer runs first: it cuts the text into tokens from a fixed vocabulary and maps each token to an id. The model reads those ids, predicts new ones, and the tokenizer turns the answer back into text.

Most modern tokenizers work on subwords, a middle ground between whole words and single characters. Hugging Face’s guide to tokenization algorithms explains why: a vocabulary of whole words grows huge and still misses new words, while single characters make every text very long. With subwords, “common words stay intact as single tokens, and rare or unknown words decompose into subwords.”

What kinds of tokenizers are there?

The same guide names three main subword algorithms:

AlgorithmUsed by (per Hugging Face)
Byte pair encoding (BPE)Llama, Gemma, Qwen2, GPT
UnigramT5, BigBird, Pegasus
WordPieceBERT, DistilBERT, Electra

OpenAI publishes its tokenizer as tiktoken, “a fast BPE tokeniser for use with OpenAI’s models.” You can count tokens with it in a few lines:

import tiktoken

enc = tiktoken.get_encoding("o200k_base")
print(len(enc.encode("How many tokens is this sentence?")))

Why does the tokenizer matter for cost?

Every model has its own tokenizer, so the same text can be a different number of tokens in Claude, GPT or Gemini. Providers charge per token, which makes the tokenizer part of the price. When Anthropic released Claude Haiku 5.5, a footnote said its new tokenizer uses slightly more tokens per task, as I covered in my Claude Haiku 5.5 take.

For a full walk through tokens, context windows and how to estimate an AI bill, read what tokenization is and why it decides what AI costs.

Sources

  1. Tokenization algorithms (Hugging Face Transformers docs)
  2. tiktoken on GitHub

More in AI, in Depth · The full glossary

Share

It’s Showtime, folks! · All That Jazz (1979)