See what your AI agent really costs

How to Stop an AI Agent Loop From Running Up a Huge API Bill

Updated 2026-10-25. Checked against official provider docs and pricing pages on 2026-10-25.

An agent that gets stuck will keep paying for tokens. The usual cause is a tool that keeps failing, so the agent retries the same call, or a context that grows with every step. Developer forums have plenty of stories about a loop that ran all night. We haven't verified any of those figures, so we don't repeat them here. Still, the pattern is real, and so is the fix. You need two layers:

  1. Provider spend limits. These are a backstop that stops traffic at a dollar amount.
  2. In-code guardrails. These stop a single run long before it reaches that amount.

Provider limits alone aren't enough. All three major providers say their caps are enforced with some lag, or reset monthly. A cap protects the month, not the run.

Layer 1: Provider spend limits

OpenAI: hard spend limits per organization or project

OpenAI separates spend alerts, which only send a notification while "API traffic continues," from hard spend limits. You can set a monthly limit at the organization level (Organization limits → Spend → Edit spend limit) or per project (Project settings → Limits → Spend → Edit spend limit). Then switch on "Enforce a hard limit" so requests actually fail.

What happens when you hit it:

Agent tip: give each agent or environment its own project and API key with its own hard limit. A runaway staging agent then can't use up production's budget.

Anthropic: tier caps, your own limit, and workspace limits

Every Anthropic usage tier has a monthly spend cap: Start $500, Build $1,000, Scale $200,000. Custom-tier organizations have no cap. On the Billing page of the Claude Console you can set your own lower limit under Spend limits → Adjust limit.

How it behaves:

Workspaces let you isolate agents. You can set custom spend and rate limits per workspace, but "you can't set spend limits on the default Workspace," and organization-wide limits always apply. Put each agent in its own non-default workspace with its own key.

Agent tip: make sure your retry logic recognizes these errors. A 429 for spend is not a temporary rate limit, and retrying it in a tight loop just burns time.

Google Gemini API: project spend caps

Google AI Studio has monthly project spend caps (on the Spend page, under Monthly spend cap → Edit spend cap). Google labels the feature experimental and warns:

Billing accounts also have tier caps (Tier 1 $250, Tier 2 $2,000, Tier 3 $20,000–$100,000). Some accounts get spend-based rate limits over a rolling 10-minute window (Tier 1 $10, Tier 2 $50, Tier 3 $200), which return 429 RESOURCE_EXHAUSTED. On Google Cloud, a Cloud Billing spend cap budget can pause the Gemini API for the rest of the month, but "in-flight requests" still finish and are charged.

If you use Claude Code on a subscription rather than the API, the cap you hit is a usage limit instead of a bill; see Claude Code usage limits.

Layer 2: In-code guardrails

The provider cap is your smoke alarm. These guardrails are the sprinkler system. They're general engineering practice, not provider requirements, so tune the values to your workload.

1. Max steps per run. Count model calls and tool calls, and end the run with a clear error when it passes a ceiling. Pick the ceiling from your own traces, for example a little above the longest successful run you've seen.

2. A token or dollar budget per run. After every call, add up the reported usage (input, cached, and output tokens) and convert it to dollars using your model's prices. Stop when the run passes its budget. TokenTab's AI agent cost calculator shows what a normal run should cost, which gives you a sensible budget to start from.

3. Cap output length. Set max_tokens (Anthropic), max_output_tokens (OpenAI Responses API) or max_completion_tokens (OpenAI Chat Completions) on every request so one call can't run away. Reasoning tokens are billed as output, so leave room for them (see are reasoning tokens billed as output tokens?).

4. Wall-clock timeouts. Give each run a deadline as well as each HTTP request. A loop of fast calls can stay under every per-request timeout and still run for hours.

5. Repeated-action detection. Fingerprint each tool call (the tool name plus a normalized hash of its arguments). If the same fingerprint repeats several times in a short window, stop or escalate. This catches the classic case of retrying a broken tool forever.

6. Bounded retries with backoff. Cap retries per tool and per model call. Treat spend-limit errors (OpenAI *_spend_limit_exceeded, Anthropic enforced_spend_limit_reached or a 400 usage-limit error, Gemini spend-based 429s) as fatal, not retryable.

7. Context growth limits. If history grows each step, input cost grows too. Trim or summarize old tool output, and cap how much context any single step can send.

8. Alerts a human will see. Use provider alerts (OpenAI spend alerts, Google Cloud's 50/80/100% spend-cap budget emails) plus your own per-run alert when a run is stopped by a guardrail.

A sensible default setup

None of this makes a loop impossible. It turns a monthly surprise into a small, logged, per-run failure.

Sources

Retrieved 2026-10-25.

Get the TokenTab newsletter

Model price changes and agent cost tips, straight to your inbox. Unsubscribe anytime; we only use your email for this newsletter.