How to calculate and reconcile API token costs

Calculate a request from its actual usage and your API key's rates, including cache reads and writes. This guide explains the arithmetic and a reproducible reconciliation process; its numerical example is illustrative, not a FlyRouter benchmark or current price quote.

Updated:

The four parts of a token bill

CategoryWhat to count
Uncached inputInput tokens not counted as a cache read or cache write
OutputBilled output tokens reported by the API
Cache readTokens read from an existing cache
Cache writeTokens written to a cache, separated by TTL if rates differ

Each category costs tokens × USD per million tokens / 1,000,000. Add all categories. Do not add cache tokens on top of an input total that already includes them. Usage fields differ across protocols, so inspect the actual response format before normalizing counts.

Worked example with illustrative rates

Suppose a request contains 10,000 uncached input tokens, 40,000 cache-read tokens and 2,000 output tokens. Suppose the rates are $1, $0.10 and $5 per million respectively, with no cache writes.

CategoryCalculationCost
Uncached input10,000 × 1 / 1,000,000$0.010
Cache read40,000 × 0.10 / 1,000,000$0.004
Output2,000 × 5 / 1,000,000$0.010
TotalSum$0.024

At identical usage, 100 requests cost $2.40. This is not a promise that 100 agent turns will have identical usage: history grows, cache entries expire and retries add requests. Use the cost calculator with your own rates.

Reconcile a real request

  1. Record its time, model ID, request ID and the key's billing group. Never publish the key.
  2. Preserve the returned usage fields, including cache writes by TTL and any billed reasoning tokens. Do not count reasoning tokens twice if already included in output.
  3. Match the request to the console usage record and the price effective at that time. Today's catalog cannot prove a historical charge.
  4. Select the applicable input-length or time tier. Keep all rates from the same group; do not combine the lowest rates from different groups.
  5. Calculate the components and compare the total with the recorded deduction. Keep precision until the final display; contact support with the request ID if the difference cannot be explained.

Compare cold and warm requests fairly

Use the same model, route, prompt, output budget and workload. Record cache-read and cache-write tokens for every request. Report sample count, time window, successful and failed attempts, total spend and task completion. Publish only redacted records. A lower input unit price alone does not establish a lower completed-task cost.

See prompt caching, billing and current pricing.