Runaway AI agent cost: how to cap an agent's API spend
An agent that loops, retries or fans out keeps calling the API until something stops it. Provider limits are that something, but most of them are monthly and some lag. Here's what each layer really caps, and in what order to set them up.
Last reviewed 30 Oct 2026
The short version
- Cap the run in code: a turn limit and a dollar budget per run. The Claude Agent SDK has both built in; both default to no limit.
- Give each agent its own key and its own cap: an OpenAI project with a hard limit, an Anthropic workspace with a spend limit, or an OpenRouter key with a daily credit limit.
- Know the gaps: OpenAI and Anthropic limits are monthly, OpenAI's enforcement can lag, and Anthropic's Default Workspace can't have a spend limit at all.
- Don't retry billing errors. A 429 from a spend limit is a stop sign, not a rate limit to back off from.
- Get told within the day, not at the end of the month: a daily threshold or a spike alert outside the agent.
1. What the provider caps actually do
| Provider | Control | Period | What to know |
|---|---|---|---|
| OpenAI | Organization or project hard spend limit | Monthly | A spend alert alone only notifies. Turn on “Enforce a hard limit”. Enforcement is not instantaneous, so recorded spend can slightly exceed the cap (OpenAI docs). |
| Anthropic | Workspace spend limit | Monthly | Not available on the Default Workspace. The Claude Code workspace is the only one with per-user monthly spend limits (Anthropic docs). |
| OpenRouter | Per-key credit limit | Daily, weekly, monthly or lifetime | The only one of the three with a daily reset on the key itself. BYOK usage isn't counted unless you turn it on (OpenRouter docs). |
The problem with a monthly cap is the size of it. A $300 monthly limit is the right number for a normal month and the wrong number for one bad night: a loop can spend most of it before anyone looks. Use the monthly cap as the backstop, and put a smaller ceiling closer to the agent.
2. Cap each run inside the agent
This is the cheapest layer and the one most often missing.
Claude Agent SDK
const run = query({
prompt: "Fix the failing tests",
options: { maxTurns: 30, maxBudgetUsd: 5 },
});
// Python: ClaudeAgentOptions(max_turns=30, max_budget_usd=5)According to the agent loop docs, both options default to no limit. When one is reached the run ends with a ResultMessage subtype of error_max_turns or error_max_budget_usd, and subagent spend counts toward the budget. The budget is compared after each response arrives, so spend can pass it by the cost of that last response plus anything running subagents spend before they stop. Leave headroom.
OpenAI Agents SDK
Runner.run(agent, input, max_turns=30) raises MaxTurnsExceeded when the run goes past the turn limit, and max_turns=None turns the limit off (running agents docs). There is no dollar budget option, so track cost yourself from each response's token usage and stop the run when it crosses the amount you'd be comfortable losing.
Any agent, any framework
- Set
max_tokens(or the provider's output limit) on every call. - Cap retries, and stop when the same tool call with the same arguments repeats several times in one run.
- Cap concurrency, and don't let a scheduled job start while the previous run is still going.
- Have a switch every loop checks before each call (an environment flag or a database row) so you can stop a job without a deploy.
All of these live in the same process as the bug. If the agent crashes and restarts, or someone ships a new agent without them, they're gone. That's why the next layers sit outside the code.
3. One key and one cap per agent
- OpenAI: put each agent or app in its own project, give it its own key, and set a project hard limit with “Enforce a hard limit” on. See OpenAI spend limit not working for the exact steps.
- Anthropic: move agent keys out of the Default Workspace into a workspace with a spend limit (Console → Workspaces → select it → Spend limits). See Anthropic Default Workspace limits. For Claude Code with Console billing, the Claude Code workspace takes the spend limit.
- OpenRouter: give the agent its own key with a credit limit and a daily reset. OpenRouter recommends a limit on every key; a key with no limit can spend your whole balance, including auto top-ups. Details in OpenRouter key limits.
Separate keys also mean you can see which agent spent the money and revoke one without taking the others down.
4. Handle limit errors without a retry storm
Make your retry logic treat these as final, not as rate limits:
- OpenAI:
429witherror.codeorganization_spend_limit_exceeded,project_spend_limit_exceeded,organization_usage_limit_exceededorcredit_balance_exhausted. - OpenRouter:
402/403witherror.metadata.limit_sourceset (for exampleopenrouter_key_limit). - Claude Agent SDK:
error_max_budget_usdanderror_max_turnsresult subtypes.
Log it, stop the job and tell a human. A loop that retries a spend-limit error forever won't cost money, but it will hide the fact that your app is down.
5. Hear about it the same day
Provider alerts are tied to the same monthly totals, so “80% of the monthly limit” can arrive days after the loop started, or minutes before the cap cuts off your production traffic. What catches a runaway agent early is a daily number: a daily threshold a bit above a normal day, or a check that compares today with your recent average. Add one outside the agent, on the provider's billing data, so it still works when the agent's own code doesn't.
Checklist
- Every agent run has a turn limit and a dollar budget (or your own cost check before each call).
max_tokensset on every call; retries and concurrency capped.- Each agent has its own key in its own OpenAI project, Anthropic workspace or OpenRouter key.
- OpenAI: “Enforce a hard limit” is on for the org and for each agent's project.
- Anthropic: no agent keys left in the Default Workspace; agent workspaces have spend limits.
- OpenRouter: every key has a credit limit, with a daily reset for anything automated.
- Spend-limit errors stop the job instead of being retried.
- Something tells you the same day when spend is far above normal.
Not sure where your settings stand? The free AI key limit audit checks OpenAI, Anthropic and OpenRouter in a few minutes (no sign-up, no key needed).
What these limits don't do
- A cap that's reached stops your own app as firmly as the loop. Size it so a normal busy day fits.
- None of them stops a request that is already running.
- A leaked key is still usable up to its cap. If that's what happened, follow the leaked key response plan first.
Where KeyBark fits (and where it doesn't)
KeyBark reads your OpenAI, Anthropic and OpenRouter spend from our servers about every 15 minutes during the beta and emails you when a daily or monthly threshold you set is crossed, or when today's spend jumps above your 7-day average. On paid plans you can opt in to a kill switch (beta) that disables the one key you named when a threshold is crossed.
Limits, honestly: checks run on a schedule and provider cost data lags (OpenAI's costs come in daily buckets), so an alert can arrive after money has been spent. KeyBark is not a proxy, so it can't block a single request or cap a run. Set the provider limits and the in-code caps above first; KeyBark is a second line of warning on top of them.
FAQ
Does an OpenAI hard spend limit stop a runaway agent?
Eventually. Once the organization or project reaches its monthly hard limit, requests fail with a 429 and the code organization_spend_limit_exceeded or project_spend_limit_exceeded. The limit is monthly, and OpenAI says enforcement is not instantaneous, so recorded spend can slightly exceed it. A spend alert on its own never stops traffic.
Can I set a daily spend limit on an AI API key?
Of the three providers, only OpenRouter lets you put a daily reset on a key's credit limit. OpenAI project and organization hard limits and Anthropic workspace spend limits are monthly. For a daily ceiling on OpenAI or Anthropic, cap the run in your agent code and add a daily alert outside the agent.
How do I cap a Claude Agent SDK run?
Set maxTurns and maxBudgetUsd (max_turns and max_budget_usd in Python). Both default to no limit. When one is hit the run ends with error_max_turns or error_max_budget_usd. The budget is checked after each response arrives, so spend can pass it by the cost of that response plus any subagents still running; leave headroom.
Sources (read 30 Oct 2026): OpenAI Spend limits; Anthropic Workspaces; Claude Code How the agent loop works and Manage costs; OpenAI Agents SDK Running agents; OpenRouter Authentication and Capping shared agent keys. KeyBark is not affiliated with OpenAI, Anthropic or OpenRouter.
KeyBark watches your OpenAI, Anthropic and OpenRouter spend from the server and flags a crossed threshold or spike by email and on your dashboard. Free for one key.