Why AI API Bills Differ from Token Count Estimates
- Your token count
- 1000 tokens
- You entered
- Price per 1K tokens
- 0.002 USD
- You entered
- Overhead factor
- 20 %
- You entered
AI API bills often differ from token count estimates because billing is based on the actual tokens processed by the provider, which include hidden formatting tokens, system prompts, and per-request overhead that your local tokenizer may not count. This article explains the main reasons for the discrepancy and shows you how to estimate your costs more accurately.
Formula
Estimated Bill = (Your Token Count × (1 + Overhead Factor / 100) × Price per 1K Tokens) / 1000
Where:
- Your Token Count (t) – the number of tokens you count locally, in tokens.
- Overhead Factor (o) – the percentage of extra tokens added by the provider (default 20%).
- Price per 1K Tokens (p) – the provider’s rate per 1,000 tokens, in USD.
- Estimated Bill (c) – the resulting cost in USD.
Worked Example
Using the default values: token count = 1000, overhead = 20%, price = $0.002 per 1K tokens.
- Add overhead: 1000 × (1 + 20/100) = 1000 × 1.2 = 1200 tokens.
- Divide by 1000 to get thousands: 1200 / 1000 = 1.2 thousand tokens.
- Multiply by price: 1.2 × 0.002 = $0.0024.
The estimated bill is $0.0024.
Why Token Counts Differ
When you count tokens locally (e.g., using a tokenizer library), you typically count only the text you plan to send. But the API provider counts every token that goes through its system, including:
- System prompts – instructions you set for the model, often hidden from the user but sent with every request.
- Chat formatting – special tokens that mark the beginning of messages, roles, and turns (e.g.,
<|im_start|>). - Function calling schemas – if you use tools, the JSON schema is tokenized and billed.
- Conversation history – previous messages are re-sent with each new request in a chat session.
- Response tokens – the output is billed separately and often not included in your pre-request estimate.
These extra tokens can easily add 10–30% to your bill, depending on the API and how you structure your requests.
How APIs Count Tokens
Tokenization is not a simple character count. It’s a method of splitting text into subword units. Different models use different tokenizers, so the same text can produce different token counts in different APIs. For example, the word “tokenization� might be one token in one model and three tokens in another. Most providers offer a tokenizer tool or library that matches their model, but if you use a generic tokenizer, you’ll get a rough estimate, not the exact count.
Additionally, APIs often count tokens in the prompt and the completion separately. Some providers charge different rates for input and output tokens, which can further skew your estimate if you only consider the prompt.
Common Causes of Bill Shock
- Long conversations – each turn resends the entire history. A 10-turn chat with 500 tokens per turn will bill 5,000 tokens for the last request, not 500.
- Retries and multiple calls – if your code retries failed requests, you pay for each attempt.
- Streaming – streaming responses don’t reduce token count; you still pay for all tokens generated.
- Hidden system prompts – some providers insert a long system prompt that you never see but still pay for.
- Tool definitions – complex function schemas can be hundreds of tokens, sent with every request.
How to Estimate More Accurately
- Use the provider’s own tokenizer, not a generic one.
- Add a buffer of 10–30% for overhead, depending on your use case.
- Check the pricing page for input vs. output rates.
- If you use chat completions, multiply your per-request token count by the number of turns in the conversation.
- Monitor your actual usage via the provider’s dashboard and compare it with your estimates.
How to Reduce Token Usage
To lower your bill, you can:
- Trim conversation history – send only the last few messages or a summary.
- Shorten system prompts – remove unnecessary instructions.
- Use smaller models – they often have lower token prices.
- Cache frequent prefixes – some providers offer prompt caching at a reduced cost.
- Batch requests – if the API supports it, batching can reduce per-request overhead.
Frequently Asked Questions
Why is my bill higher than my token count times the price?
Because the provider counts more tokens than you do. Hidden system prompts, formatting tokens, and conversation history are all billed. Also, output tokens are often charged at a different rate.
Can I see the exact token count for each request?
Most providers include usage statistics in the API response, such as prompt_tokens and completion_tokens. You can sum these to get the exact count for that request.
Does streaming cost more?
No, streaming doesn’t change the token count. You pay for the same number of tokens whether you receive them all at once or in chunks.
How much overhead should I expect?
It varies. For a simple single-turn prompt, overhead might be 5–10%. For a chat with a long system prompt and tools, it could be 30% or more. Check the usage data from your API responses to see the actual overhead.
Is there a way to avoid paying for system prompts?
Not really. System prompts are part of the request and are billed. However, you can make them shorter or use prompt caching if your provider offers it.
Why does the same text have different token counts in different APIs?
Different models use different tokenizers. For example, a word like “hello� might be one token in one model but two in another. Always use the tokenizer provided by the API you’re using.
Related
Formula used
in t | Your token count | tokens | 1000 | 0 in p | Price per 1K tokens | USD | 0.002 | 0 in o | Overhead factor | % | 20 | 0 out c | Estimated bill | USD | (t * (1 + o/100) * p) / 1000 | 4
Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.