I ran a small experiment with OpenAI's tokenizer (tiktoken), and it explains a lot about LLM cost, speed, and context.
"Hello World" → 𝟐 𝐭𝐨𝐤𝐞𝐧𝐬
"Hello World" (2 spaces) → 𝟑 𝐭𝐨𝐤𝐞𝐧𝐬
That second space becomes its own token (id 220). Same words. 𝟓𝟎% 𝐦𝐨𝐫𝐞 𝐭𝐨𝐤𝐞𝐧𝐬. Small detail, big bill at scale.

🔍 𝐇𝐨𝐰 𝐭𝐨𝐤𝐞𝐧𝐬 𝐚𝐫𝐞 𝐜𝐫𝐞𝐚𝐭𝐞𝐝 (𝐁𝐏𝐄):

  1. Split text by regex — spaces stay attached to the next word
  2. Merge frequent character pairs into single tokens
  3. Token → ID → embedding → model A token is not a word or a character — it's whatever chunk the merges learned.

📊 𝐓𝐢𝐧𝐲 𝐜𝐡𝐚𝐧𝐠𝐞𝐬, 𝐝𝐢𝐟𝐟𝐞𝐫𝐞𝐧𝐭 𝐜𝐨𝐮𝐧𝐭𝐬 — 𝐬𝐞𝐞 𝐓𝐚𝐛𝐥𝐞 𝟏 𝐢𝐧 𝐭𝐡𝐞 𝐢𝐦𝐚𝐠𝐞𝐬.

🌍 "𝐇𝐞𝐥𝐥𝐨 𝐖𝐨𝐫𝐥𝐝" 𝐚𝐜𝐫𝐨𝐬𝐬 𝐥𝐚𝐧𝐠𝐮𝐚𝐠𝐞𝐬, 𝐚𝐧𝐝 𝐭𝐡𝐞 𝐬𝐚𝐦𝐞 𝐟𝐮𝐥𝐥 𝐬𝐞𝐧𝐭𝐞𝐧𝐜𝐞 — 𝐬𝐞𝐞 𝐓𝐚𝐛𝐥𝐞 𝟐.
GPT-4o's bigger vocab (200k vs 100k) cut the non-English penalty — but didn't remove it. Telugu was 𝟐𝟎 𝐭𝐨𝐤𝐞𝐧𝐬 𝐨𝐧 𝐆𝐏𝐓-𝟒 𝐯𝐬 𝟐 𝐟𝐨𝐫 𝐄𝐧𝐠𝐥𝐢𝐬𝐡.

💻 𝐂𝐨𝐝𝐞 𝐜𝐨𝐮𝐧𝐭𝐬 𝐭𝐨𝐨 — 𝐬𝐞𝐞 𝐓𝐚𝐛𝐥𝐞 𝟑.
𝐏𝐲𝐭𝐡𝐨𝐧 𝟑𝟔 𝐭𝐨𝐤𝐞𝐧𝐬, 𝐉𝐚𝐯𝐚𝐒𝐜𝐫𝐢𝐩𝐭 𝟒𝟒, 𝐉𝐚𝐯𝐚 𝟑𝟑 for the same job. Indentation spends tokens on spaces too.

🔢 𝐍𝐮𝐦𝐛𝐞𝐫𝐬 𝐜𝐡𝐮𝐧𝐤 𝟑 𝐝𝐢𝐠𝐢𝐭𝐬 𝐚𝐭 𝐚 𝐭𝐢𝐦𝐞 — 𝐬𝐞𝐞 𝐓𝐚𝐛𝐥𝐞 𝟑.
"1,000,000" costs 𝟓 𝐭𝐨𝐤𝐞𝐧𝐬 𝐯𝐬 𝟑 for "1000000" — every comma is a token. That's why LLMs are shaky at arithmetic: they see chunks, not quantities.

💡 𝐖𝐡𝐲 𝐭𝐡𝐢𝐬 𝐦𝐚𝐭𝐭𝐞𝐫𝐬:
• 𝐂𝐨𝐬𝐭 — extra tokens × 1M requests = a bigger bill
• 𝐂𝐨𝐧𝐭𝐞𝐱𝐭 — non-English fills the window 2–9x faster; multilingual RAG needs language-aware chunking
• 𝐋𝐚𝐭𝐞𝐧𝐜𝐲 — more tokens in = slower first token
• 𝐇𝐲𝐠𝐢𝐞𝐧𝐞 — pretty JSON, trailing spaces, repeated whitespace = a silent tax
• 𝐌𝐨𝐝𝐞𝐥 𝐜𝐡𝐨𝐢𝐜𝐞 — counts differ by model, measure with your target tokenizer
Tokens are the unit of money, memory, and speed in every AI project. 𝐂𝐨𝐮𝐧𝐭 𝐭𝐡𝐞𝐦 𝐛𝐞𝐟𝐨𝐫𝐞 𝐭𝐡𝐞𝐲 𝐜𝐨𝐮𝐧𝐭 𝐲𝐨𝐮. 💸

Have you profiled token usage in your project? What surprised you?

AI #LLM #GenAI #MachineLearning #Tokenization #SoftwareEngineering #AIEngineering