API Cost Calculator: Tokens, Cache, and Request Volume
Use a formula and Python calculator to estimate input, output, cache write, and cache read costs across three request-volume scenarios.
An API cost calculator multiplies each usage category by its current rate and the number of calls. Calculate input, output, cache write, and cache read separately, with every rate expressed in one currency per 1,000,000 tokens. Do not silently replace an unknown value with zero. Define the baseline request first, then add request volume and the cache-hit rate.
What data does the calculator need?
For the text API, prepare seven inputs:
Want to check the calculator's prediction on a real call? You can create a BetterToken account and API Key, take the current rates from pricing page and perform one controlled request. Then compare the model, status, input, output, applicable cache Token and consumption in the Dashboard - this will immediately show which initial assumptions need to be corrected.
Caching varies by model and protocol. Before filling out the fields, check API reference BetterToken, OpenAI Prompt Caching or Anthropic Prompt Caching.
Universal formula
Designations:
Cost of one call:
Cextra includes separately charged units: web search, images, audio, storage, tools or other operations. If there are none, the value is zero. If you don't know if there is an additional charge, leave the field unknown and check the documentation; zero would then create false precision.
The main mistake in manual calculations is forgetting to divide by a million. If the price is for 1,000,000 tokens, first divide the Token by 1_000_000, then multiply by the bid.
Copyable Python calculator
The script contains no prices or API Key. It asks for the inputs and calculates one scenario. The result uses the same currency as the rates you enter.
Save the code as api_cost_calculator.py and run:
Do not enter real Tokens into the cache write/read fields unless the current endpoint shares these categories. First convert its usage to mutually exclusive groups to avoid counting the same Token twice.
How to take into account cache hit rate
For a series of queries, it is more convenient to separate cache hits and misses.
Result:
For planning, round Nhits down and Nmiss up. This makes for a slightly more cautious assessment. In a real log, use the actual number of calls of each type.
Three scenarios instead of one number
Basic scenario
Use median input and output from recent tasks, the expected number of calls, and the observed cache-hit rate. If you have no history yet, label the values as assumptions.
Favorable scenario
Stable long prefix, high cache hit rate, limited output and no repeated errors. It shows a lower bound, but should not become a budget promise.
Worst case scenario
Add cache misses, long output, one limited retry and separately charged tools. Do not increase all parameters arbitrarily: each assumption must correspond to the real risk of the process.
Record your results on a simple sheet:
How to evaluate agent workflow
One visible agent run does not always equal one model call. Inside there may be planning, tool call, tool result, retry and final response. That's why:
- perform one safe test task;
- count the actual API calls;
- group them by model and usage category;
- apply the formula to each group;
- separately add tool or search units;
- compare the amount with Dashboard.
Don't multiply the cost per random call by the number of users if the length of requests varies greatly. It is better to consider several classes of tasks: a short question, a file review, an agent task.
How to check a forecast with a fact
After the test call, match:
- time and request status;
- Model ID;
- input and output tokens;
- cache category;
- number of retries;
- actual consumption;
- currency and price date.
The difference between a forecast and a fact usually points to one of four places: an incorrect bid, double counting of cached tokens, a hidden retry, or an additional chargeable transaction.
For BetterToken, use the current pricing page and then check the actual Dashboard entry. Do not transfer prices from an old screenshot or article.
Limitations of the calculator
The formula covers only known categories. It does not predict rate changes, future prices, dynamic routing or the number of agent steps. Image, audio, web search, storage and some tools may have their own units.
The calculator also does not evaluate the quality of the answer. A cheaper call that has to be repeated manually can increase the cost of the entire task. This is measured by a separate experiment, not by adding a made-up coefficient.
FAQ
What to enter if cache is not used?
Set cache write and cache read to zero only if the endpoint did not actually use cache. For an unknown value, check usage first.
In what currency will the result be?
The result uses the currency of the rates and extra_per_call you enter. Do not mix dollars and rubles without an explicit exchange rate and date.
Are Cached tokens included in input tokens?
It depends on the usage form of the specific API. Before calculating, check the documentation and convert fields to mutually exclusive categories to avoid double counting.
How to calculate the cost of a month?
First, calculate the cost of one task class, then multiply by the actual or forecast number of calls. For different models and tasks, make separate lines and add up the total.
Why is the actual charge higher than the estimate?
Check output, retries, agent steps, cache misses and additional tools. Map each usage line to the Dashboard, not just the total.