You need to count tokens locally. When do you reach for tiktoken versus SentencePiece?
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.
Detailed answer & concept explanation~5 min readEverything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
4 min: identify tiktoken as OpenAI's BPE library with cl100k_base and o200k_base; identify SentencePiece as Google's general toolkit supporting BPE and Unigram LM; name three production models on each side; explain the Llama 3 switch from SentencePiece-Unigram to a tiktoken-style BPE; close with the rule that mixing them silently is a common bug source.
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.
- Loading Llama 3.3 tokenizer via HuggingFace: AutoTokenizer.from_pretrained('meta-llama/Llama-3.3-70B-Instruct'); the tokenizer.json artifact uses a tiktoken-style byte-level BPE with a 128K vocabulary.
- Counting Claude tokens: the proprietary tokenizer is not shipped as a library; the canonical way is anthropic.Anthropic().messages.count_tokens with the model name.
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?
QWhy does SentencePiece bother to operate on raw bytes instead of pre-tokenizing on whitespace like classical tokenizers?
Don't say thisRed flags and common mistakes that signal junior thinking. Click to expand.
Red flags and common mistakes 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.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.
Same topic, related formats. Practice these next.