How to audit an LLM API bill, request by request

Trace each LLM API charge to one request id with its tokens, model and price, export the month as CSV, and check the signed statement against a public key.

Checked 2026-11-22

·All posts·Models

Every charge on this API traces back to a single request id carrying its token counts, the model that answered and the exact price, exportable as CSV and totalled in a monthly statement that is signed so you can prove it has not been altered; this is how to reconcile an LLM bill line by line, from one response body up to the month's statement. The method has four levels, each checking the one below it.

Level 1: the request itself

Start where the money leaves: the response. A non-streamed answer in the OpenAI or Anthropic format carries usage.cost, the charge for that call in USD, next to the usual token counts, and the body's model field names the model that answered after any alias, preset or fallback. A streamed answer carries usage.cost in each usage event as a running figure; the last one (OpenAI's usage chunk with stream_options.include_usage set, Anthropic's message_delta) is the whole charge.

python
from openai import OpenAI

client = OpenAI(base_url="https://api.uplinkswitch.com/v1", api_key=KEY)
r = client.chat.completions.with_raw_response.create(
    model="gpt-6.1-sol",
    messages=[{"role": "user", "content": "Classify this ticket: billing, bug or other."}],
)
body = r.parse().to_dict()
print(body["model"], body["usage"]["cost"], body["usage"])
request_id = r.headers.get(...)   # the request id in the response headers

Log three things per call in your own system: the request id from the response headers, the body's model, and usage.cost. That is your side of the ledger, and everything below is checked against it.

Two billing rules matter at this level. Billed input can never exceed what the request could hold, so the tokens on a receipt are the tokens billed, and any fixed prompt a route adds is paid by UplinkSwitch, not you. And an answer that fails or is refused, or that misses a JSON schema the request asked for, is not billed; the refund shows up as a reason on the request's record rather than as a silent zero.

Level 2: look a request up

Within about a minute of finishing, every request lands in the usage log, and any key of the organization can read it:

bash
curl https://api.uplinkswitch.com/v1/requests/REQUEST_ID \
  -H "Authorization: Bearer $UPLINKSWITCH_API_KEY"

The record has the model (after any alias), status and outcome, usage with input_tokens, cache_read_tokens, cache_write_tokens and output_tokens, charge_usd, the same tokens at the developer's list price as list_cost_usd, saved_usd, latency_ms and a refund field that is null or names the reason. This is also where a stream's figures live, since a stream's headers leave before its tokens exist.

Dispute test: multiply the record's tokens by the model's price on the price index (input, cached input and output are separate rates) and you should land on charge_usd to six decimal places. If the figures disagree, you have a request id to quote.

For latency rather than money, the console shows a timing trace per request: millisecond marks for auth, route, admit, each upstream attempt, first byte, end and settle. A request that cost more time than tokens explains itself there.

Level 3: the month as a file

The Usage page in the console shows each request beside its tokens and charge, filters by key, model and outcome, and exports the rows as CSV. The same file comes from the API:

bash
curl "https://api.uplinkswitch.com/v1/usage/export?from=2026-10-01&to=2026-10-31&key_id=KEY_ID" \
  -H "Authorization: Bearer $UPLINKSWITCH_API_KEY" -o october.csv

Columns, oldest row first: request_id, created_at, key_id, session_id, model, outcome, status, input_tokens, cache_read_tokens, cache_write_tokens, output_tokens, charge_usd, latency_ms, refund_reason, user. Filters: from and to (dates or Unix milliseconds; the default window is the 30 days before to), key_id, model, status (including failed for upstream errors and rejections together) and user. One file covers at most 92 days and 200,000 rows, so a large month is pulled per key.

With the file, the reconciliation is a group-by:

python
import csv, collections
by_model = collections.defaultdict(float)
by_key = collections.defaultdict(float)
for row in csv.DictReader(open("october.csv")):
    by_model[row["model"]] += float(row["charge_usd"])
    by_key[row["key_id"]] += float(row["charge_usd"])
print(dict(by_model)); print(dict(by_key))

Compare by_key with GET /v1/keys/{key_id}/usage, which gives a key's request count and spend over three windows: today (UTC), this month (UTC) and all time (add ?group_by=user for a per-end-user split from the user field). Compare the sum of charge_usd with the drop in your balance over the same window, and the refund_reason column tells you which rows were not billed and why. Rows whose status is an upstream error should carry no charge at all.

What to look for in the file, beyond the totals:

  • A model you did not expect. The model column names what answered. With model: "auto" or an alias, that is the resolved id; with budget autopilot, a cheaper same-developer model near a key's limit.
  • Cache behaviour. cache_read_tokens should be high on a key with long repeated prefixes. If it is zero, the prompt is not caching and you are paying the full input rate.
  • Waste. A response also carries a signal when a noticeable share of its request was text already sent earlier in the conversation, or a base64 blob pasted inline; media sent as proper file or image parts does not count. A key that trips the signal on most requests has a prompt to fix.

Level 4: the signed statement

Each month has a statement on the console's billing page: a JSON file signed with Ed25519 plus a CSV with one row per request. The signed payload carries the CSV's SHA-256, so one signature covers both files. Fields: org, period, totals, by_model, by_key, payments, usage_csv, with amounts as exact six-place decimals. It becomes final one day after the month ends and is served exactly as stored from then on.

Check it in the browser at /statements/verify: drop in the statement JSON, its CSV, or a single response receipt, and the page verifies the signature with Web Crypto against the public key. Nothing is uploaded, no account is needed, and fetching the public key is the only request the page makes. The key itself is public at GET /v1/statements/public-key, a JWK with no key required, so an accountant or a customer's own script can verify without any access to your account.

Payments are the other half of the ledger. A receipt email goes out when a payment is first credited, each payment has a printable receipt page in the console's billing section, refund and dispute notices are emailed too, and GET /v1/invoices lists invoices with a PDF per invoice number. The statement's payments field should match those receipts, and totals should match the CSV from level 3.

Per-request receipts for the strict case

When a single answer has to be defensible (a customer disputes a charge, a compliance review asks which model produced a text), every non-streamed response also carries a signed receipt, and any request's receipt is a document at GET /v1/requests/{request_id}/receipt. It states the request_id, model, billed_tokens (input, cache_read, cache_write, output), charge_usd, refund and hashes (SHA-256) of both the request and response bodies, signed with the same Ed25519 key as the statements. Hash your stored copy of the request and compare: a matching hash ties the charge to the exact bytes you sent. Whether the id in that field is the one that actually answered is checked separately, every day, on /proof.

A monthly routine

  1. Pull the month's CSV per key with /v1/usage/export, sum charge_usd by key and model.
  2. Compare the sums with each key's /usage figures and with the statement's by_key and by_model.
  3. Verify the statement and its CSV at /statements/verify; file both with the payment receipts.
  4. Spot-check a handful of request ids from your own logs against GET /v1/requests/{id}: tokens times price equals charge_usd.
  5. Read the refund_reason column and the cache columns for anything that points at a prompt or a key to fix.

The whole chain is request id to record to CSV to signed statement; the observability page covers sessions and log export to your own tools, and the SDK guide has the request and receipt routes with examples.

Questions and answers

Read usage.cost in the response body for the exact charge in USD, and the body's model field for the model that answered. Within about a minute the same request is at GET /v1/requests/{request_id} with its input, cached and output tokens, charge_usd, the list-price cost, the saving and any refund reason.

Yes. The console's Usage page exports filtered rows as CSV, and GET /v1/usage/export returns the same file by from and to date, optionally for one key, model, outcome or end user, with one row per request including tokens, charge_usd and refund_reason. One file covers up to 92 days and 200,000 rows.

Open /statements/verify and load the statement JSON and its CSV. The page checks the Ed25519 signature in your browser against the public key at GET /v1/statements/public-key; the signed payload includes the CSV's SHA-256, so a changed row fails. Nothing is uploaded and no account is needed, so a third party can run the same check.
Join on Telegram
Help