Predict the approximate token count difference between 'Hello world' and its Arabic equivalent 'مرحبا بالعالم' in a GPT-4 tokenizer.
Same topic, related formats. Practice these next.
Same topic, related formats. Practice these next.
Using tiktoken with cl100k_base (GPT-4's tokenizer), compare the token count for: - English: 'Hello world' (2 words, 11 characters) - Arabic: 'مرحبا بالعالم' (same meaning: 'Hello world', 2 words, 13 Arabic characters) Arabic characters use 2 bytes each in UTF-8. The cl100k_base tokenizer was trained predominantly on English data. Predict the token count for each string and the approximate ratio.
English 'Hello world' is about 2 tokens; the Arabic equivalent is roughly 8-10, a 4-5x token tax driven by English-biased merge rules.
Imagine a vending machine stocked to dispense whole sandwiches for the most popular orders. Ask for a regular sandwich and one button gives you the whole thing. Order something the owner never expected and the machine has no sandwich button, so it hands you the ingredients one slice at a time, and you pay per slice. That is what happens here. The tokenizer learned shortcuts for common English, so 'Hello' pops out as one piece. Arabic was rarely stocked, so its words come out almost letter by letter, and you pay for every little piece. Same meaning, many more pieces, a much bigger bill.
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.
3 min: 2-byte UTF-8 Arabic + missing Arabic merges + fertility 4-5x + cost/context/rate-limit budgets + why tiktoken beats a quoted integer.
import tiktoken
enc = tiktoken.get_encoding("cl100k_base") # GPT-4's tokenizer
english = "Hello world"
arabic = "مرحبا بالعالم" # same meaning
en_toks = enc.encode(english)
ar_toks = enc.encode(arabic)
print(len(en_toks)) # ~2
print(len(ar_toks)) # ~8-10
print(len(ar_toks) / len(en_toks)) # ~4-5x token tax| Property | English 'Hello world' | Arabic 'مرحبا بالعالم' |
|---|---|---|
| UTF-8 bytes per character | 1 byte (ASCII) | 2 bytes |
| Learned merge coverage | Extensive (word-level) | Sparse (few pairs merge) |
| Approx tokens | ~2 | ~8-10 |
| Fertility (tokens/word) | ~1 | ~4-5 |
| Relative cost / context use | Baseline | 4-5x higher |
Real products, models, and research that use this idea.
What an interviewer would ask next. Try answering before peeking at the approach.
Red flags and common mistakes that signal junior thinking. Click to expand.
Estimating Arabic tokens from character count and assuming roughly 1 token per word, the way English behaves, then under-budgeting cost by 4-5x.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.