To track AI API spend per key, route every model call through a gateway that logs tokens and cost on each request, issue a separate API key for each app, team, or environment, and attribute spend back to the key that made the call — then set a budget and alert per key so you're warned long before the invoice arrives. The reason bills shock teams is that AI spend is metered in tokens, varies per request, and — without per-key attribution — shows up as one opaque number at month's end.
This guide shows how to get ahead of it: why AI bills surprise you, why "per key" is the right unit of attribution, a step-by-step tracking setup, the metrics to watch, and how to set budgets that stop runaway spend. NevTan Cloud is used as the reference platform, since its AI Gateway logs token usage and cost per app out of the box.
AI API bills surprise you because cost scales with tokens and varies per request. Fix it with attribution: one key per app/team/environment, every call routed through a gateway that logs tokens and cost, spend rolled up per key, and a budget plus alert on each key. Then cut spend with shorter prompts, smaller models, and caching — once you can finally see where it goes.
Why AI API Bills Surprise You
A traditional API bills per request; an AI API bills per token — and token counts swing wildly between calls. A few predictable reasons it gets away from teams:
Variable cost per call. A one-line answer and a 2,000-token essay cost very different amounts on the same endpoint.
Output tokens are the silent driver. Output is usually priced higher than input, and a chatty model inflates it fast.
Prompt bloat. Long system prompts and retrieved context ride along on every request.
Retries and loops. Agent retries, failed tool calls, and runaway loops multiply calls invisibly.
No attribution. With one shared key, you can't tell which app, feature, or customer caused the spike — so you can't fix it.
Key takeaway: The problem isn't just how much you spend — it's that without per-key attribution you can't see where it goes until the invoice lands.
Why "Per Key" Is the Right Unit
An API key is the natural seam to attribute cost along, because you control how many you issue and what each one represents. Give every meaningful unit of your system its own key:
Per app or service — so you know which product line drives spend.
Per environment — separate dev, staging, and production keys so test loops don't hide in your production number.
Per team — so each team owns its budget.
Per customer (for multi-tenant apps) — so you can see unit economics and spot a customer who's unusually expensive to serve.
Issuing and rotating these keys safely is a key-management and access-control task — store them in a secrets manager, scope them, and never share one key across unrelated workloads.
Key takeaway: One key per workload turns a single opaque bill into a breakdown you can act on.
How to Track AI API Spend per Key (Step by Step)
Issue a key per workload. Create a distinct API key for each app, environment, team, or tenant you want to see separately.
Route every call through a gateway. Point your apps at an AI gateway instead of calling providers directly. The gateway is the one place every request passes through, which makes it the one place you can measure them.
Capture tokens and cost per request. The gateway logs input tokens, output tokens, model, latency, and computed cost for each call — tagged with the key that made it.
Aggregate per key. Roll those request logs up by key to see spend per app/team/environment over time, not just a global total.
Set a budget and alert on each key. Cap how much a key can spend over a daily, weekly, or monthly window, and fire an alert at a threshold (say 80%) so a human hears about it before the cap — or the invoice — hits.
Review and optimize. Use the per-key breakdown to find the expensive workloads and act (see below).
Because NevTan Cloud's AI Gateway already logs every prompt, response, token count, and cost per app, the attribution step is mostly a matter of mapping keys to workloads rather than building a metering pipeline yourself.
Metrics to Track per Key
Spend is the headline, but these are the numbers that explain it:
Metric | Why it matters |
|---|---|
Input tokens | Driven by prompt length and retrieved context |
Output tokens | Usually the priciest component; watch it closely |
Total tokens | The unit your bill is actually computed from |
Requests | Spikes here often mean retries or loops |
Cost per request | Rising average signals prompt or model bloat |
Latency (p95) | Correlates with large responses and cost |
Error rate | Failed calls you still pay for, and a retry source |
Key takeaway: Track output tokens and cost per request per key, not just total spend — they tell you why a key's bill moved, not just that it did.
Set Budgets and Alerts Before You Need Them
Visibility catches problems after they happen; budgets stop them. For each key, set a spending limit scoped to that key with a refresh period (daily, weekly, or monthly), and an alert threshold below the cap. A good pattern:
Alert at 80% of budget → someone investigates.
Hard cap at 100% → the key stops spending until it resets or you raise the limit.
Separate, smaller budgets for non-production keys so a stuck test loop can't quietly run up the bill.
Wire these in from day one — the documentation covers how to configure keys and limits. A budget you set before launch is worth far more than a dashboard you check after the bill.
Reduce Spend Once You Can See It
Per-key attribution turns cost-cutting from guesswork into targeting:
Trim the prompt. Long system prompts and oversized retrieved context cost tokens on every call. Test prompt changes against cost, not just quality.
Right-size the model. A smaller open model often matches a larger one on a narrow task at a fraction of the cost — the single biggest lever for most teams.
Cache repeated calls. Identical or near-identical requests don't need to hit the model twice.
Cap output length. Set max tokens so a verbose model can't run long on your dime.
Watch agents. AI agents and their retries are a common source of surprise spend — give them their own keys and tight budgets.
How NevTan Cloud Helps
NevTan Cloud's AI Gateway is built around this exact problem. It sits in front of your model calls and logs every request — prompt, response, latency, token counts, and cost per app — so attribution is native rather than something you bolt on. Combined with usage-based pricing, you pay for what you actually run and can see it broken down rather than as one month-end lump. Treating cost monitoring as part of production governance — not an afterthought — is what keeps AI features profitable as they scale.
Common Mistakes
One shared key for everything. You lose all attribution and can't tell which workload spiked. Issue a key per app/team/environment.
No budgets or alerts. A dashboard you have to remember to check won't stop a runaway loop. Set caps and alerts per key.
Averaging, not percentiles. A low average cost hides expensive outliers. Watch p95 and the tail.
Ignoring output tokens. They're usually the priciest part and the easiest to let balloon.
Mixing test and production. Without separate keys, test loops inflate your production number invisibly.
Frequently Asked Questions
How do I track AI API spend per key? Route every model call through a gateway that logs tokens and cost per request, issue a distinct API key for each app/team/environment, and roll spend up by key. Then set a budget and alert on each key so you're warned before the cap or the invoice.
Why are AI API bills so unpredictable? Because they're billed per token, and token counts vary enormously between a short answer and a long one. Output tokens, long prompts, retrieved context, and agent retries all push cost up in ways a per-request mental model misses.
What is a token, in billing terms? A token is the unit AI providers meter — roughly a word-piece. You're billed for input tokens (your prompt and context) and output tokens (the model's response), with output usually priced higher.
Can I set a spending budget per API key? On a gateway that supports it, yes — cap how much a key can spend over a daily, weekly, or monthly window, with an alert below the cap. Issuing a key per workload is what makes those budgets meaningful.
How do I reduce AI API costs? Trim prompts and context, right-size to a smaller model where it performs, cache repeated calls, cap output length, and keep agent retries on tight budgets. Per-key attribution tells you which workload to target first.
Should each app have its own API key? Yes. Separate keys per app, environment, team, and (for multi-tenant products) customer are what let you attribute, budget, and optimize spend instead of guessing.
Stop Guessing, Start Attributing
AI API bill shock is an attribution problem, not a mystery. Route calls through a gateway, give every app, team, and environment its own key, log tokens and cost per request, and put a budget and alert on each key. Once you can see spend per key, cutting it — shorter prompts, smaller models, caching — becomes targeted instead of hopeful.
NevTan Cloud's AI Gateway logs tokens and cost per app on usage-based pricing, so the attribution is there from the first request. See plans and get started, or dig into the model catalog to pick a right-sized model for the job. Repo to production.