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:

input_tokens_per_call output_tokens_per_call cache_write_tokens_per_miss cache_read_tokens_per_hit calls cache_hit_rate prices_per_1m_tokens

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:

I — regular input tokens O — output tokens W — cache write / creation tokens R — cache read / cached tokens Pi — input price per 1,000,000 tokens Po — output price per 1,000,000 tokens Pw — cache write price per 1,000,000 tokens Pr — cache read price per 1,000,000 tokens

Cost of one call:

C = I / 1_000_000 × Pi + O / 1_000_000 × Po + W / 1_000_000 × Pw + R / 1_000_000 × Pr + Cextra

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.

from decimal import Decimal, InvalidOperation MILLION = Decimal("1000000") def read_decimal(label: str, *, allow_empty: bool = False) -> Decimal: raw = input(label).strip().replace(",", ".") if allow_empty and raw == "": return Decimal("0") try: value = Decimal(raw) except InvalidOperation as exc: raise SystemExit(f"Invalid number for {label!r}") from exc if value < 0: raise SystemExit(f"Negative value is not allowed for {label!r}") return value input_tokens = read_decimal("Input tokens per call: ") output_tokens = read_decimal("Output tokens per call: ") cache_write_tokens = read_decimal("Cache write tokens per call: ") cache_read_tokens = read_decimal("Cache read tokens per call: ") calls = read_decimal("Number of calls: ") price_input = read_decimal("Input price per 1M tokens: ") price_output = read_decimal("Output price per 1M tokens: ") price_cache_write = read_decimal("Cache write price per 1M tokens: ") price_cache_read = read_decimal("Cache read price per 1M tokens: ") extra_per_call = read_decimal("Extra cost per call (empty = 0): ", allow_empty=True) per_call = ( input_tokens / MILLION * price_input + output_tokens / MILLION * price_output + cache_write_tokens / MILLION * price_cache_write + cache_read_tokens / MILLION * price_cache_read + extra_per_call ) total = per_call * calls print(f"Cost per call: {per_call:.8f}") print(f"Total cost: {total:.8f}")

Save the code as api_cost_calculator.py and run:

python3 api_cost_calculator.py

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.

N — total number of calls h — cache-hit rate from 0 to 1 Nhits — N × h Nmiss — N - Nhits Chit — cost of a call with cache read Cmiss — cost of a call without a hit or with cache write

Result:

Ctotal = Nhits × Chit + Nmiss × Cmiss + Cextra_total

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:

scenario, calls, hit_rate, input, output, write, read, extra, total base, ..., ..., ..., ..., ..., ..., ..., ... low, ..., ..., ..., ..., ..., ..., ..., ... high, ..., ..., ..., ..., ..., ..., ..., ...

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:

  1. perform one safe test task;
  2. count the actual API calls;
  3. group them by model and usage category;
  4. apply the formula to each group;
  5. separately add tool or search units;
  6. 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.

Ready to optimize your LLM workflow?

Join thousands of developers building faster, smarter, and more cost-effective AI applications with BetterToken.