AI API cost monitoring
How to monitor spend across OpenAI, Anthropic, and Gemini
To monitor your AI API spend across multiple providers in one place, you have to query each vendor's usage or cost endpoints on a schedule, normalize the data, and set up your own threshold alerts. There isn't a shared cross-provider billing standard; OpenAI separates token metrics from daily dollar amounts, Anthropic enforces monthly tier caps like the $500 USD limit on its Start tier, and Google AI Studio subject your Gemini API calls to a billing processing delay of up to around 10 minutes.
If you don't track these numbers centrally, you'll only find out about a runaway loop or a leaked key when an invoice hits your credit card. Setting up unified monitoring means understanding what each platform exposes, how long the reporting delays are, and where hard limits can stop traffic before your budget breaks.
What each provider exposes
Every AI platform handles billing and usage tracking differently. Some give you granular token counts by model, others expose dollar figures grouped by project, and several only return your remaining prepaid credit balance.
| Provider | Programmatic tracking | Supported billing / limits | Granularity / delay | Limit behavior |
|---|---|---|---|---|
| OpenAI | Completions Usage API & Costs API | Spend Alerts & Spend Limits | 1d bucket for Costs API; 1m, 1h, or 1d for Usage | Fails with HTTP 429 when hard spend limit is enforced |
| Anthropic | Usage and Cost Analytics API | Credit purchases / AWS Marketplace | 1d time buckets via Analytics API | HTTP 429 at tier cap; HTTP 400 at custom org or workspace limit |
| Gemini | Cloud Billing reports & AI Studio usage | Prepay & Postpay (starts March 23, 2026) | Up to ~10 min pipeline latency; 24h for charts | HTTP 402 when balance hits $0; HTTP 429 on rate caps |
| OpenRouter | Management API (/api/v1/keys) & /api/v1/key | Prepaid credits & budget caps | Upfront in-flight budget hold with short settlement window | HTTP 403 on budget cap; HTTP 402 on credit or in-flight limit |
| DeepSeek | Get User Balance API (/user/balance) | Granted & topped-up balance | Current available balance (total_balance) | HTTP 402 (Insufficient Balance) when balance runs out |
| xAI | Management API | Prepaid credits & Monthly invoiced | Usage explorer monitoring | HTTP rejection at $0 invoiced limit or depleted credits |
OpenAI and Anthropic provide dedicated reporting APIs that return structured usage data. Platforms like DeepSeek only expose a balance endpoint, so you have to track spend by calculating the delta between balance checks.
OpenAI: Splitting token usage from dollar billing
For basic manual checks, OpenAI provides a default web dashboard at https://platform.openai.com/usage. But if you're pulling data into an internal monitoring tool, you need to use two separate endpoints: the Completions Usage API and the Costs API. Both endpoints require an Admin API key passed in the Authorization header as a Bearer token.
The Completions Usage API lives at https://api.openai.com/v1/organization/usage/completions. When calling it, start_time is required and must be provided in Unix seconds. You can pass an optional bucket_width parameter set to 1m, 1h, or 1d (it defaults to 1d), and you can filter requests by project_ids, user_ids, api_key_ids, and models. Each result object returns input_tokens, output_tokens, and num_model_requests, alongside specialized counters for input_cached_tokens, input_audio_tokens, and output_audio_tokens. If you don't supply a group_by parameter, fields like project_id and model will return as null. You can group by multiple fields at once, such as group_by=["model", "project_id"].
Dollars are handled separately. The Costs API endpoint is https://api.openai.com/v1/organization/costs. Unlike the usage endpoint, the only supported bucket_width for the Costs API is 1d. Each result item contains an amount object with a numeric value and a currency string (such as usd). You can group cost queries by project_id or line_item. Grouping by line_item exposes exact service categories, including ft-gpt-4o-2024-08-06, input, assistants api | file search, and evals | gpt-4o-mini-2024-07-18, input.
Pagination across both APIs uses the next_page cursor from the response, which you pass as the page query parameter in the next GET call. OpenAI also provides endpoints in its API reference to create, retrieve, update, delete, and list Spend Alerts, and endpoints to manage Spend Limits. If you want to dive deeper into parsing these payloads, see our guide on How do I track OpenAI API costs programmatically with the Usage and Costs APIs?.
Anthropic: Tier caps, workspaces, and analytics
The Claude API enforces two limit types: rate limits for throughput, and spend limits that cap your maximum monthly cost. Anthropic sets service-configured caps at the organization level across three tiers:
- Start: $500 USD monthly spend cap
- Build: $1,000 USD monthly spend cap
- Scale: $200,000 USD monthly spend cap
When you hit your tier's monthly spend cap, API calls pause until 00:00 UTC on the first of the next month. The API returns an HTTP 429 error with the type rate_limit_error, but it doesn't include a retry-after header, and automatic SDK retries will fail. On the Messages API, you can identify this condition because error.details.error_code is set to enforced_spend_limit_reached.
You can also set custom limits. At platform.claude.com/settings/workspaces, select a workspace and click Spend limits to configure limits or add email alerts. Custom organization limits can't exceed your current tier cap, and you can't set spend limits on the Default Workspace. When a user-configured limit is hit, requests return an HTTP 400 invalid_request_error stating "You have reached your specified API usage limits" (or workspace limits). To check which workspace handled a call, inspect the anthropic-workspace-id response header. Note that the Claude Code workspace is the only one supporting per-user monthly limits, and Claude Platform on AWS bills through AWS Marketplace instead of Anthropic credits.
To poll spend programmatically, Anthropic provides the Usage and Cost Analytics API:
curl --globoff "https://api.anthropic.com/v1/organizations/analytics/usage?\
starting_at=2026-09-01T00:00:00Z&\
ending_at=2026-09-08T00:00:00Z&\
workspace_ids[]=wrkspc_01JwQvzr7rXLA5AGx3HKfFUJ&\
group_by[]=workspace_id&\
time_bucket_width=1d" \
-H "anthropic-version: 2026-10-21" \
-H "anthropic-beta: analytics-usage-cost-2026-08-24"If you need to configure hard guardrails inside your console, read How do I cap my Claude API spend?.
Gemini: Billing latency, spend rate limits, and prepay traps
Starting March 23, 2026, Google AI Studio and the Gemini API implement Prepay and Postpay billing plans. Billing account tiers dictate your limits: Tier 1 carries a $250 monthly cap once an active billing account is linked, Tier 2 raises it to $2,000 after $100 in cumulative paid spend and 3 days, and Tier 3 provides $20,000 to $100,000+ after $1,000 paid spend and 30 days.
Can you rely on instant shutoffs? Not quite.
Prepay billing has an approximately 10-minute billing pipeline latency. Long-running batch jobs and agents can consume credits past your balance before usage halts. When credits reach $0, all API keys across all linked projects stop working immediately with an HTTP 402 Payment Required error; they don't downgrade to the Free Tier. Any excess usage caused by pipeline delays shows up as a negative credit balance, which gets deducted from your next purchase. Prepay credits require a $5 minimum purchase, cap out at $5,000, expire after 12 months, and are non-refundable.
Gemini also enforces rolling 10-minute spend rate limits: $10 per 10 minutes for Tier 1, and $200 per 10 minutes for Tier 2 and Tier 3. Exceeding them triggers a 429 RESOURCE_EXHAUSTED error.
AI Studio Project Cap (up to ~10 min delay)
│
├──> Overages accrue to negative balance
│
Cloud Billing Spend Cap (Service ID: AEFD-7695-64FA)
│
├──> Alerts at 50%, 80%, 100% (non-customizable recipients)
└──> Gross estimated cost hits 100% -> pauses new project usageProject spend caps in AI Studio carry that same 10-minute processing delay, and Total Cost breakdown graphs on the Billing and Spend pages can take up to 24 hours to update. In Google Cloud Billing, spend cap budgets for the Gemini API (generativelanguage.googleapis.com, Service ID AEFD-7695-64FA) pause new usage once gross estimated costs reach 100%. If you lift an enforced Cloud Billing spend cap, restoring service can take up to one hour. Invoiced accounts can't use Prepay or spend caps. For setup instructions, check out How do I limit what I spend on the Gemini API?.
OpenRouter, DeepSeek, and xAI: Balance endpoints and credit limits
Aggregators and alternative providers often rely on credit-checking endpoints rather than complex reporting suites.
On OpenRouter, you can query your key status directly by sending a GET request to https://openrouter.ai/api/v1/key using your standard API key. The response returns your limit, limit_remaining, limit_reset, and usage counters: usage (all-time), usage_daily (current UTC day), usage_weekly (UTC week starting Monday), and usage_monthly (UTC month). It also includes an effective_budget and an array of budgets. OpenRouter checks budgets in order: Guardrail budgets first, Workspace budgets second, and the key's own limit third. If a budget blocks a request, it fails with HTTP 403, returning limit_source (openrouter_guardrail_budget, openrouter_workspace_budget, or openrouter_key_limit) and an ISO 8601 UTC timestamp in resets_at with no Retry-After header.
To prevent concurrent requests from draining balances before settlement, OpenRouter calculates an in-flight hold using input tokens plus max_tokens completion tokens. If this fills up, you get an HTTP 402 with reason: "in_flight_budget_exhausted" and a Retry-After header. Account-level credits are checked via GET /api/v1/credits, which requires an administrative Management API key.
DeepSeek keeps it minimal. Send an HTTP GET request to https://api.deepseek.com/user/balance with Authorization: Bearer ${DEEPSEEK_API_KEY}. The response gives you:
is_available: boolean showing if your balance supports callsbalance_infos: array listing currency (CNYorUSD)total_balance,granted_balance, andtopped_up_balance: formatted as decimal strings (such as "110.00")
When your balance hits zero, DeepSeek calls fail with 402 - Insufficient Balance.
For xAI, you choose between prepaid credits and monthly invoiced billing. The default invoiced billing limit is $0, meaning requests are rejected as soon as prepaid credits run out. If you set an invoiced limit, xAI uses prepaid credits first, charging your default payment method at the end of the month. Auto top-up can be configured with a minimum $5 purchase and a monthly maximum value; the console shows a warning card when you hit 80% of that monthly limit. Kimi (Moonshot AI) operates on a pay-as-you-go model with a Token Calculation API to retrieve exact token counts per call.
Bringing it into one view with Costbrake
Building your own polling scripts, database tables, and notification pipelines across half a dozen APIs takes time, and maintaining custom scrapers every time an auth header or schema changes gets old quickly.
Costbrake tracks your spend across 23 providers, including OpenAI, Anthropic, Google Cloud (Gemini API and Vertex AI), OpenRouter, xAI, DeepSeek, and Moonshot AI. It evaluates spending hourly on the Free plan, every 5 minutes on Pro and Team, and up to every minute during an active spike, depending on the provider.
Alerts trigger across multiple scenarios:
- Spike alert: fires when an hour's cost is more than 3x your 7-day hourly baseline and at least $5 higher.
- Budget alerts: notify you at 50%, 80%, and 100% of defined budget targets.
- Forecast alert: triggers when your month-to-date spend plus the pace of the last 72 hours projects that you'll exceed your budget by month-end.
- New-charge alert: flags any new service showing up above $1 in a month.
Notifications route via email on every plan, Slack, Discord, and signed webhooks on Pro, and PagerDuty on Team.
When an anomaly occurs, Pro and Team include an opt-in killswitch called brakes across 18 providers. A brake never runs automatically; an admin must click and confirm it. Brakes pause, stop, cap, or disable a resource, record the prior state, and have an undo function. For OpenAI, a brake freezes the hard spend limit at $0.01. For Anthropic, it disables specific API keys or all keys in a workspace. For OpenRouter, it drops a key's limit to $0 or disables it, and on Google Cloud, it applies a zero spend cap to a specific service in a project.
Credentials are encrypted with AES-256-GCM, and Costbrake uses read-only keys whenever providers offer them. Plans start at Free ($0 for 2 connections, 1 seat, and 30 days of history), Pro ($12 a month or $120 a year for 10 connections, 3 seats, and brakes), and Team ($39 a month or $390 a year for 50 connections and PagerDuty). View complete plan details on Costbrake pricing. To get started, you can connect keys manually at https://app.costbrake.com/signin or run our agent setup with your AI coding assistant. If an API key ever slips into a public repo, check What should I do in the first hour after an API key leaks?.
Questions and answers
How do I check my OpenAI costs without using the web dashboard?
You query the OpenAI Costs API at https://api.openai.com/v1/organization/costs using an Admin API key passed as a Bearer token. The API supports a bucket_width of 1d and lets you group costs by project_id or line_item.
What happens when I hit my Anthropic monthly spend cap?
When an organization hits its usage tier cap (such as $500 USD on the Start tier), calls pause until 00:00 UTC on the first of the next month. The Messages API returns an HTTP 429 error with error.details.error_code set to enforced_spend_limit_reached.
Can Gemini API calls exceed my prepaid credit balance?
Yes, because Google AI Studio has an approximately 10-minute billing pipeline latency. Tasks like batch requests or agents can continue to run and consume credits before the system halts traffic, resulting in a negative credit balance that gets deducted from your next credit purchase.
How does OpenRouter prevent concurrent calls from overrunning my credit balance?
OpenRouter estimates token costs up front based on input tokens and the max_tokens parameter, holding that estimated amount against an in-flight spending budget. If that budget is exhausted, it returns an HTTP 402 error with reason: "in_flight_budget_exhausted" and includes a Retry-After header.
What is the quickest way to pull DeepSeek balance data programmatically?
Make an HTTP GET request to https://api.deepseek.com/user/balance with your API key in the Authorization: Bearer header. The returned JSON payload contains an is_available boolean and string values for total_balance, granted_balance, and topped_up_balance.
Sources
- Usage - OpenAI API Reference
- Completions usage API - OpenAI Developers
- Rate limits - Claude Docs
- Create and manage Workspaces in the Claude Console - Claude Help Center
- Workspaces - Claude Docs
- Billing - Google AI Studio
- Rate limits - Google AI Studio
- Manage spend cap budgets - Cloud Billing - Google Cloud Documentation
Updated