Count tokens before drawing a conclusion
A tokenizer uses a vocabulary and splitting rules to turn text into integer IDs. One word can be one token or several. Spaces and punctuation can change the split, and another tokenizer can assign a different count or ID to the same text.
Compare the two versions of one parcel question, in English and Hindi. Use the GPT-2 tokenizer and the XLM-R tokenizer, which comes from a multilingual model trained on text in 100 languages.
In a Python environment with tokenizers==0.22.2 and huggingface-hub==1.33.0, run the example below. It downloads only the two revision-pinned tokenizer files, about 10.5 MB in total. No account, model weights or inference are needed.
from huggingface_hub import hf_hub_download
from tokenizers import Tokenizer
models = (
("openai-community/gpt2",
"607a30d783dfa663caf39e06633721c8d4cfcd7e"),
("FacebookAI/xlm-roberta-base",
"e73636d4f797dec63c3081bb6ed5c7b0bb3f2089"),
)
texts = ("Where is my parcel?", "मेरा पार्सल कहाँ है?")
for repo, revision in models:
path = hf_hub_download(
repo, "tokenizer.json", revision=revision, token=False
)
tok = Tokenizer.from_file(path)
print(repo, [
len(tok.encode(text, add_special_tokens=False).ids)
for text in texts
])GPT-2 returns 5 for English and 30 for Hindi. XLM-R returns 5 and 6. These counts are for the exact strings above, including the question marks and excluding special tokens.
What the counts tell you
- Context. More tokens for the same message leave less room in a fixed token budget. For a chat system, count the whole formatted input, including instructions, roles and history.
- Cost. If a provider bills input tokens at a fixed rate, a larger count increases that part of the bill. Neither these local files nor one sentence tell you what a hosted request will cost.
- Quality. Token counts do not measure translation accuracy, useful answers or language coverage. Test those separately with representative examples and speakers of the language.
This is one measured comparison, without a universal ratio. Repeat it across several realistic messages before making a decision about a product or course.
Look at the pieces and their source spans
The variable tok holds XLM-R after the loop. Inspect the pieces and offsets for a short sentence, then try the same code with the GPT-2 file. Each offset tells you which slice of the original text contributed to that token.
text = "Ravi lives in Pune."
encoded = tok.encode(text, add_special_tokens=False)
for token, token_id, (start, end) in zip(
encoded.tokens, encoded.ids, encoded.offsets
):
print(token, token_id, text[start:end])Offsets make it possible to highlight the original span of a label in a reading tool. More than one token can refer to the same character, and a special token may have no source span.
Special tokens and truncation
Special tokens tell a particular model about boundaries or roles. The raw count above leaves them out so it compares text segmentation. In a real pipeline keep the model’s required tokens, but avoid adding them twice when the chat template has already inserted them.
Truncation discards some of the input from the primary encoding when the input exceeds the configured limit. Choose which text to keep deliberately. For example, a classifier that only receives the beginning of a complaint may miss the event described at the end. Keep the important part, split long inputs into chunks, or choose a model with more room.
Do this now
Write three short requests in a language you use. Record the exact text, tokenizer revision and token counts. If you compare translations, make sure they mean the same thing. Decide what additional checks you need before making a claim about cost or teaching quality.
Check your understanding
GPT-2 counts 5 tokens for the English parcel question and 30 for the Hindi translation. What does this experiment show?
Pick the one you would defend. Your answer stays private.
Your place and first choices stay on this browser. Classroom credit is recorded separately.
Every lesson here is open. Your progress is recorded once you pass the Image Generation, in Practice exam.
Stuck on something?Ask Arjun, your AI tutor