← All 96 breakdowns
TOKENS

TOKENS: the token-counting script

From Adam Rosler. The thing you commented TOKENS for: count what your prompts really cost in each language.

What the reel showed

Aleksandar Petrov, Emanuele La Malfa, Philip Torr and Adel Bibi (University of Oxford; NeurIPS 2023) counted the tokens in the same ~2,000 sentences across 200 languages (FLORES-200). On GPT-4's tokenizer (cl100k_base), Spanish took 1.55x English's tokens, Hindi 4.8x, Telugu 8.3x, Burmese 11.7x. On the newer tokenizer GPT-4o and GPT-5 use (o200k_base), Hindi drops to 1.6x, but Amharic still takes 5.9x and Burmese 3.2x. Ahia et al. (University of Washington; EMNLP 2023) showed what that does to an API bill, and that for Telugu and Thai not even one worked example fit in ChatGPT's prompt. Their released code, re-run here on their data, matched their table for all 204 languages.

The script

count_tokens.py (below). Put the same text in each language in its own file (a real translation of your system prompt, your docs or a typical user message), then:

pip install tiktoken
python count_tokens.py english.txt hindi.txt amharic.txt --price 2.50

It prints each file's tokens and the ratio to the first file (and the cost, if you give your price per 1M tokens).

"""Count how many tokens the same text costs in different languages, on the tokenizer your model uses.

    pip install tiktoken
    python count_tokens.py english.txt hindi.txt amharic.txt          # the first file is the baseline
    python count_tokens.py --encoding cl100k_base english.txt hindi.txt  # GPT-4 / GPT-3.5's tokenizer

Each file should hold the same text (a real translation of your prompt, your docs or your typical user message).
o200k_base is the tokenizer of GPT-4o, GPT-4.1, the o-series and GPT-5; cl100k_base is GPT-4's and GPT-3.5's. Other
providers use their own tokenizers: count with their token-counting endpoint or tokenizer instead, because the ratios
differ a lot between tokenizers.
"""
import argparse
import tiktoken


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("files", nargs="+")
    ap.add_argument("--encoding", default="o200k_base")
    ap.add_argument("--price", type=float, default=None, help="your model's price per 1M input tokens, to show cost")
    a = ap.parse_args()
    enc = tiktoken.get_encoding(a.encoding)
    rows = []
    for path in a.files:
        text = open(path, encoding="utf-8").read()
        rows.append((path, len(text), len(enc.encode(text, disallowed_special=()))))
    base = rows[0][2]
    print(f"tokenizer: {a.encoding}")
    for path, chars, n in rows:
        cost = f"  ${n * a.price / 1e6:.6f}" if a.price else ""
        print(f"{path:30s} {chars:8d} chars {n:8d} tokens  {n / base:5.2f}x{cost}")


if __name__ == "__main__":
    main()

What to do with it

  1. Count on your model's tokenizer: o200k_base for current OpenAI models; for other providers use their token counter, because tokenizers differ a lot (in the Oxford table, Hindi ranges from 1.3x to 7.5x English by tokenizer).
  2. Price per language, not per English prompt: a 5x language is a 5x bill and a 5x smaller context window.
  3. Re-check when you switch models: the newer OpenAI tokenizer cut Hindi from 4.8x to 1.6x; others still lag.

Sources: Petrov, La Malfa, Torr & Bibi, NeurIPS 2023 (arXiv:2305.15425; code github.com/AleksandarPetrov/tokenization-fairness); Ahia et al., EMNLP 2023.

Files for this breakdown

FREE ONE-TIME UNLOCK

Unlock the full TOKENS breakdown + downloadable runnable scripts

Drop your email once to read the complete step-by-step guide, grab the files, and unlock all 96 Reel breakdowns on this device forever.

✓ Instant reveal right here✓ No spam, unsubscribe any time