Skip to content
Notifications
Clear all

TIL: Some providers count tokens differently. Always test with a known string.

1 Posts
1 Users
0 Reactions
3 Views
(@alexm)
Reputable Member
Joined: 1 week ago
Posts: 147
Topic starter   [#15391]

A recent benchmarking exercise for a multilingual RAG pipeline revealed a significant, non-obvious cost driver: stark variance in token counting methodologies across major LLM API providers. We were comparing per-request inference cost for identical prompts and, despite normalizing for advertised price-per-token, observed cost discrepancies of up to 18% for mixed-language text. The root cause was not the pricing itself, but the underlying tokenizers.

The common assumption—that "token" is a standardized unit roughly equivalent to 4 characters of English text—is dangerously inaccurate for cost projection. Providers utilize different tokenization algorithms (often inherited from their base model's training) which handle whitespace, punctuation, and non-Latin scripts with substantial variation.

Consider this simple test string, designed to highlight edge cases: `"Hello world! 🚀 (Naïve café)"`. Using the `tiktoken` library for OpenAI and the `transformers` library for Llama/Mistral tokenizers, we get different counts:

```python
# Sample token count comparison for string: "Hello world! 🚀 (Naïve café)"
import tiktoken
from transformers import AutoTokenizer

test_string = "Hello world! 🚀 (Naïve café)"

# OpenAI's cl100k_base (GPT-4, GPT-3.5-turbo)
enc = tiktoken.get_encoding("cl100k_base")
openai_count = len(enc.encode(test_string))
# Result: 14 tokens

# Llama 3's tokenizer via transformers
llama_tokenizer = AutoTokenizer.from_pretrained("meta-llama/Meta-Lllama-3-8B")
llama_count = len(llama_tokenizer.encode(test_string))
# Result: 17 tokens
```

This 21% variance on a short string scales linearly, directly impacting cost calculations. Key differentiators observed across providers include:

* **Unicode Normalization:** Some tokenizers convert characters like `é` or `ç` into single tokens, while others decompose them into multiple tokens (e.g., `e` + combining acute accent).
* **Emoji and Special Character Handling:** Emojis, mathematical symbols, and line breaks can be tokenized as a single unit or split into multiple constituent code points.
* **Whitespace Treatment:** Leading spaces may be assigned a dedicated token or merged with the following word, and tab characters may have unique representations.
* **Language-Specific Granularity:** Non-Latin scripts (e.g., Japanese, Thai) exhibit the most dramatic differences. A single Chinese character may be one token in one system and three in another.

**Implications for Benchmarking:**
1. **Cost Projections:** Benchmarks comparing "cost per 1k tokens" must be normalized using a *common text corpus* and the *specific tokenizer* for each provider. Using one provider's token count to estimate another's costs is invalid.
2. **Context Window Utilization:** A prompt consuming 6,000 tokens in Provider A's count might exceed Provider B's 8,000-token context window if Provider B's tokenizer is more granular for your specific data.
3. **Performance Metrics:** Latency-per-token and throughput-per-token metrics are also skewed if not based on consistent token definitions.

**Recommendation:** Before committing to a provider for a high-volume application, always run a representative sample of your production data (including user-generated content with all its quirks) through the official tokenizer for each candidate model. Build a simple normalization table to map your real-world input/output characteristics to each provider's billing units. Treat "token" as a provider-specific variable, not a constant.



   
Quote