Tracking costs across OpenAI, Anthropic, Grok and Gemini

Last verified

The problem in one line: four providers, four billing APIs, four credential types, four timezone conventions — and no provider has any incentive to show you the others.

Here is what each one actually requires.

What each provider needs

ProviderCredentialEndpoint styleCadence
OpenAIAdmin key sk-admin-…/v1/organization/costs, Bearer authDaily
AnthropicAdmin key sk-ant-admin…/v1/organizations/…, x-api-key + version headerDaily
xAIManagement key (not xai-…)Team-scoped billingDaily
Google GeminiGoogle Cloud identityCloud Billing / BigQuery exportDaily

Three of the four are "paste a key". Gemini is not — see tracking Gemini spend via Google Cloud Billing. If you are building this yourself, Gemini will take longer than the other three combined.

The four gotchas

1. Every credential is a different type. None of them is the key you use for inference. The most common first failure is pasting a sk-proj- or xai- key and getting an authorisation error that looks like a permissions problem rather than a wrong-key-type problem.

2. xAI is team-scoped. You resolve the team first, then query billing against it. An empty result usually means "wrong team", not "no spend".

3. Anthropic needs anthropic-version on every request. Omitting it produces a confusing 400 that reads like a malformed request.

4. Days don't line up. Providers bucket days differently and settle at different times. A "today" total assembled from four sources is an approximation at the edges. Compare like for like over a week rather than agonising over a single day.

Why the total won't match your invoices

Even done correctly, the sum of four API figures will differ slightly from the sum of four invoices:

  • Credits and free tiers are applied at billing time, not usage time
  • Volume discounts and committed-use pricing land later
  • Tax is added at invoice
  • Timezone boundaries shift usage between days
  • Lag — recent usage may not have settled

Treat the API figure as the operational number — what you use to spot a problem — and the invoice as the accounting number. They serve different purposes and it is fine for them to differ by a percent or two.

Building it yourself

Feasible, and roughly a week of work for a decent version. What you'll need:

  1. Four credentials, held somewhere safe and separate from your inference keys
  2. Four fetchers with different auth, pagination and response shapes
  3. Normalisation to a common {provider, date, model, usd} shape
  4. Storage, because none of these APIs give you long history cheaply
  5. Something that runs it daily and shows you the result
  6. A threshold check, or it's a dashboard nobody opens

Steps 1–4 are the easy part. Step 6 is the one that determines whether any of it changes an outcome.

Or not

If you'd rather not maintain four API integrations, that's the gap TKN fills: connect each provider once, get today's combined spend with a per-provider breakdown, set a threshold, and revoke a key at the provider if the number is wrong. Keys stay in the device Keychain behind Face ID.

Other options exist and are worth knowing — LLM cost tracking tools: which one for which job is the honest comparison, including the cases where a proxy-based tool is the better answer.

Sources