LLM API Bill Spiked? Find the Feature or User Behind It
Your OpenAI or Claude bill jumped and the dashboard shows only totals. Log tokens per feature and user, cap retries, and set limits so the next spike is caught in hours.
To find why your LLM API bill spiked, line up the date of the jump with your deploys and scheduled jobs, check usage by project, key and model in the provider dashboard, then log tokens per request with a feature name and user ID. The usual causes are retry loops, an agent that never stops, one heavy user or script, a prompt that grew, and a leaked key. Fix it with a retry cap, per-user limits and a spend alert.
Provider dashboards show totals. As one write-up on tracking spend per feature explains, attribution to your features and customers needs tagging that you add yourself. That is why so many founders ask the same question in places like the OpenAI community forum. The steps below work for OpenAI, Anthropic and any other provider.
Step 1: Stop the Bleeding First
Before you investigate, put a ceiling on the damage.
- Set a monthly budget and an alert in your provider's billing settings. Check whether it is a hard limit or only a notification, because the two behave very differently.
- If a leaked key is possible, rotate it now. A key committed to a public repo or shipped in browser code is a common source of surprise bills.
- If one route is clearly the problem, turn it off with a feature flag until you understand it.
Rotating a key is cheap. A day of unexplained spend is not.
Step 2: Match the Spike to an Event
Open the usage graph and note the hour the cost started to climb. Then compare it with three lists:
- Your deploy history. Did a prompt, model name or loop change go out?
- Your scheduled jobs. Did a nightly batch start processing more rows?
- Your traffic. Did a launch, a post or a bot send more users?
A sharp step up right after a deploy points to code. A slow climb points to growth or a growing prompt. Spikes at odd hours point to a job or a script.
Step 3: Group Usage by Project, Key and Model
Most providers can break usage down by project or API key and by model. Look for anything your app should not be doing:
- Usage on a key or project your app does not use means a leaked or shared key.
- Calls to a model you never configured are a strong sign someone else is using your key.
- A jump on a cheap model is usually volume. A jump on an expensive model is usually a code or config change.
If you use one key for everything, split it now: one key per environment, and ideally one per major feature. Separate keys give you attribution for free.
Step 4: Log Tokens Per Request
The real answer lives in your own logs. Wrap every model call in one function that records who, what and how much. Responses include token counts, for example usage.prompt_tokens and usage.completion_tokens in the OpenAI Chat Completions API.
import OpenAI from "openai";
const client = new OpenAI({ maxRetries: 1, timeout: 30_000 });
export async function ask(opts: {
feature: string;
userId: string;
messages: OpenAI.Chat.ChatCompletionMessageParam[];
model?: string;
}) {
const model = opts.model ?? "gpt-4o-mini";
const started = Date.now();
const res = await client.chat.completions.create({
model,
messages: opts.messages,
max_tokens: 800,
});
console.log(JSON.stringify({
event: "llm_call",
feature: opts.feature,
userId: opts.userId,
model,
promptTokens: res.usage?.prompt_tokens,
completionTokens: res.usage?.completion_tokens,
ms: Date.now() - started,
}));
return res;
}
Ship these lines to wherever your logs go and sum tokens by feature and by userId. In an hour you will see which feature or person is behind most of the spend. Multiply tokens by your model's current price from the provider's pricing page to turn them into money. Prices change, so do not hard-code old numbers.
Step 5: Check the Common Causes
With attribution in hand, look for these patterns:
- Retry storms. A call fails, your code retries, the SDK retries too, and a queue retries the job. Nested retries multiply calls. Sources describing this pattern include this retry storm write-up and WunderGraph's cost control guide. Keep one retry layer, with backoff. See OpenAI API 429 and Timeout Errors in Production for how to do that safely.
- Agent loops. A tool-calling agent that keeps calling tools until it is "done" can run for dozens of steps. Set a maximum step count.
- Growing prompts. Chat history appended on every turn, or a retrieval step that pastes in too many documents, makes each call larger. Log
promptTokensover time and watch for a slope. - One heavy user. One automation script hitting your endpoint in a loop is enough. One developer reported exactly this.
- No
max_tokens. Without an output cap, a model can write far more than you meant.
Step 6: Add Limits So It Cannot Repeat
Fix the cause, then add guardrails:
- Cap output with
max_tokenson every call. - Add a per-user daily token or request limit in your own database. Reject or queue requests over the limit.
- Rate limit the endpoint that triggers the model, and require login for it.
- Add a daily spend alert, not only a monthly one. A monthly alert fires after the damage is done.
- Cache answers for identical inputs where the result does not need to be fresh.
For the retry side in a Node.js app, API Timeout Errors in Node.js: Retries That Work 2026 covers backoff and limits.
How Relia Helps
Relia watches the users and APIs of a live product. If a deploy lines up with a burst of failing or slow AI calls, it joins those signals into one incident in plain words and shows which users were affected. After a deploy, it checks whether the fix worked. Relia does not track provider billing or token cost, so keep the per-request logging from Step 4 for money. Setup for Node.js and browser apps is npx relia-sdk@latest init, and there is a live demo. There is a Free plan, and Growth is $19/month.
FAQ
Why did my OpenAI API bill suddenly increase?
The common reasons are a retry loop, an agent that runs too many steps, one user or script sending heavy traffic, prompts that keep growing, or a leaked API key. Match the start of the spike to a deploy or a job, then check usage by key and model.
How do I track OpenAI API cost per user or feature?
Wrap your model calls in one function and log the feature name, user ID, model and token counts from the response. Sum tokens by feature and user, then multiply by the current price.
Can I set a hard spending limit on an LLM API?
Most providers offer budgets or limits in billing settings. Read the docs to see whether yours stops requests or only sends an email. Add your own per-user limits as well.
How do I stop retries from multiplying LLM costs?
Keep a single retry layer, limit it to one or two attempts, use exponential backoff with jitter, and set a timeout. Do not retry inside both your code and a job queue.