How to Reconcile an LLM API Bill With Your Usage Logs
Reconcile actual billing lines to usage by attempt, normalize token buckets and service units, and flag incomplete evidence.

Your application estimates $0.050, but the charge records you can match add up to $0.035. Did the API cost less than expected, or is a charge missing? In the hypothetical example below, one attempt timed out and its charge is unknown. Subtracting those totals will not tell you what happened.
Start by matching submission attempts to the provider's records for the same account, currency and period. Keep retries and separately billed services visible: a single conversation can include cached input, tool calls, generated images and requests whose outcome your application never recorded.
1. Collect both sides before calculating
From your application, export one row per actual submission attempt: operation ID, attempt ID, timestamp in UTC, exact provider/model/route, key or project identifier (never the secret), request ID if available, raw usage reference, outcome and estimate. Preserve incomplete or timed-out attempts.
From the provider, export the usage and charge records available for the same account. For example, the direct OpenAI organization Usage and Costs API offers bucketed records, grouping dimensions and pagination. For a gateway, check its own export fields and join keys before attempting a per-request match.
Agree on a closed-open period such as midnight UTC on the first day up to midnight UTC on the next month. Compare the billing currency and whether the total includes taxes, credits or adjustments. Complete pagination; partial data is not zero usage.
2. Normalize without counting the same tokens twice
A Chat response may show prompt, completion and total tokens plus details. A Responses payload may instead report input and output categories. A reasoning-token detail can be a subset of output rather than extra billed output. Cache details can similarly describe subsets of total input. Inspect the exact API version and pricing table.
Suppose input total is 10,000, cached read is 8,000, and write is 1,000, with those two non-overlapping counts included in input total. Ordinary input is 1,000. If output is 700 and reasoning details are 200 of those 700, do not add the 200 to the total. These hypothetical buckets illustrate the arithmetic; use the bucket definitions for your actual route.
Record pricing snapshot, rate tier, cached-read/write lifetime, media units, storage and tool charges. The caching guide explains how applicable OpenAI cache token fields are reported. Use the gateway's own documented units and actual charges when reconciling gateway billing.
3. Make a difference table
The following invented amounts demonstrate how to keep an unresolved attempt visible.
| Attempt | App estimate (USD) | Confirmed charge (USD) | Difference | Observation |
|---|---|---|---|---|
| a1 | 0.020 | 0.020 | 0.000 | matched |
| a2 | 0.010 | 0.015 | 0.005 | verify applicable price and tools |
| a3 | 0.020 | unknown | unknown | timeout after possible submission |
All three app estimates total $0.050, while the two confirmed charges total $0.035. The $0.015 gap compares different sets of attempts. Resolve a3 before calculating a full-set difference.
For a1 and a2 alone, estimates total $0.030 and confirmed charges total $0.035: charges are $0.005 above estimates for the matched attempts. Investigate a2's extra $0.005 and reconcile a3 separately. Do not enter zero for its missing charge.
For an implementation, pass normalized nonnegative amounts in one currency to a local summarizer. Set charge to null until a charge record is matched to that attempt; use numeric zero only for a confirmed zero charge. The small example uses JavaScript numbers for illustration; financial production code should use decimal arithmetic or integer minor units with the provider's precision rules.
function totalRows(rows) {
let estimate = 0, knownCharge = 0, matchedEstimate = 0;
let matchedRows = 0, unknownRows = 0;
for (const row of rows) {
if (!Number.isFinite(row.estimate) || row.estimate < 0) {
throw Error("Invalid estimate");
}
estimate += row.estimate;
if (row.charge === null) {
unknownRows++;
continue;
}
if (!Number.isFinite(row.charge) || row.charge < 0) {
throw Error("Invalid charge");
}
knownCharge += row.charge;
matchedEstimate += row.estimate;
matchedRows++;
}
return {
estimate,
knownCharge: matchedRows ? knownCharge : null,
matchedRows,
matchedEstimate: matchedRows ? matchedEstimate : null,
matchedDifference: matchedRows ? knownCharge - matchedEstimate : null,
unknownRows,
completeDifference: rows.length > 0 && unknownRows === 0
? knownCharge - estimate
: null
};
}
For the table above, matchedEstimate is 0.030 and matchedDifference is +0.005. The latter is confirmed charge minus estimate for the same matched attempts. completeDifference stays null while a3 is unresolved.
If every charge is unknown, matchedRows is 0 and knownCharge, matchedEstimate and matchedDifference are all null. An empty input also has no matched comparison and returns null for completeDifference. A confirmed charge of zero, however, counts as a match: an estimate of 0.020 with a confirmed 0.000 charge produces a difference of −0.020. Keep null for missing evidence and 0 for a confirmed zero amount.

4. Investigate the difference by layer
| Pattern | Investigate |
|---|---|
| App sees fewer attempts | SDK retries, worker restarts, hidden proxy retries, logging failures |
| Usage matches, cost differs | Price effective date, account tier, cache/write mix, rounding and units |
| Provider reports an unmatched item | Shared account activity, external client, tool or storage charge |
| Response missing after timeout | Request correlation, existing job status and billing record |
| Dashboard total differs from invoice | Period boundaries, currency, credits, discounts, adjustments and tax |
If the provider reports aggregates only, reconcile at the narrowest common grouping rather than inventing request-level amounts. Keep unresolved items in a separate table with owner and next action.
Finally, allocate attributable charges by app or workload; shared or unresolved charges stay explicitly unallocated until you choose a transparent allocation rule. Verify the total again after credits and adjustments. The pricing comparison guide is useful for forecasting, while this ledger answers what happened after the calls.


