tiktoken is OpenAI's BPE-only tokenizer with the cl100k_base and o200k_base encodings; SentencePiece is Google's general tokenizer library that supports BPE and Unigram LM and ships with Gemma, T5, and Llama 2.
Think of tiktoken as a very fast cashier who only knows the prices at one specific store. If you walk into that store (OpenAI) it is the quickest, friendliest way to ring up your bill. If you walk into a different store, the cashier has no idea what anything costs, because they only know the OpenAI catalog. SentencePiece is more like a general-purpose cash-register kit. You can set it up for many different stores. Google built it, and it is the register used by Gemma, by T5, and by older Llama 2 models. Newer Llama 3 and 4 moved to a register that mimics the OpenAI style, but the original was SentencePiece. The picking rule is simple. If you are calling an OpenAI API, you reach for tiktoken because you need the OpenAI encodings to count tokens correctly. For pretty much anything else, you reach for SentencePiece or the HuggingFace tokenizers wrapper around it.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
tiktoken and SentencePiece are the two tokenizer libraries that most often come up in 2026 production work, and confusing them is one of the easier bugs to introduce in a multi-model pipeline. They serve different model families, support different sets of algorithms, and produce token ids that mean nothing to each other.
This deep dive separates the two libraries by purpose, lists which 2026 models use which, names a third library (HuggingFace tokenizers) that increasingly mediates between them, and finishes with the practical selection rule.
tiktoken: OpenAI's narrow, fast registry
tiktoken is built for exactly one job: tokenize text for OpenAI models, quickly, with the exact same vocabulary the API server uses. The Python package wraps a Rust core that handles the regex-based pre-tokenization and byte-level BPE encoding. Encoding a million tokens runs in the hundreds of milliseconds range rather than the tens of seconds range that a pure-Python BPE would take.
The library's surface is small. There is a registry of named encodings, and you load one by name. The two that matter in 2026 are:
- cl100k_base: 100K vocabulary, used by GPT-4, GPT-4 Turbo, and GPT-3.5 Turbo. Still relevant because many production pipelines have not migrated off these models.
- o200k_base: 200K vocabulary, used by GPT-4o, o1, o3, o4-mini, GPT-5, and GPT-5.5. This is the default for new OpenAI work in 2026.
Older encodings (p50k_base, r50k_base) exist for legacy Codex and GPT-3 models and rarely show up in greenfield work.
tiktoken cannot train tokenizers, cannot tokenize text for non-OpenAI models, and does not ship the Llama, Gemma, or Claude vocabularies. It is a closed registry exposed for one purpose: counting tokens accurately before paying per-token API calls.
Situations where this technique stops working.
2–4 min · Everything important, quickly.
Real products, models, and research that use this idea.
- Counting tokens for a GPT-5.5 chat request: import tiktoken, get_encoding('o200k_base'), encode the prompt, take the length, compare to the model's context window.
- Loading the Gemma 3 tokenizer: SentencePieceProcessor().Load('gemma_tokenizer.model'); the .model file contains both vocabulary and merge or pruning state.
What an interviewer would ask next. Try answering before peeking at the approach.
QHow does tiktoken make encoding so fast compared to a pure-Python BPE implementation?
tiktoken's core is written in Rust and exposed to Python via PyO3. The hot path (regex pre-tokenization, byte-level BPE merging, hash-table lookups for token ids) runs in compiled native code, so encoding a million tokens takes hundreds of milliseconds rather than tens of seconds. The Python layer is a thin wrapper that mostly handles the registry of named encodings.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Treating tiktoken and SentencePiece as interchangeable token counters. They are different tokenizers with different vocabularies; running tiktoken on a Llama prompt and trusting the count is just wrong.
60 second bullets to scan on the way to the call.
Primary sources. Browse if you want the original framing.
Same topic, related formats. Practice these next.