Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Prevent an AI agent from running up an uncontrolled API bill by enforcing limits inside the agent’s orchestration logic before each call, then backing those limits with bounded retries, provider spend controls, and useful usage records. A provider’s monthly spend cap is a backstop—not a precise budget for one run—and a prompt asking the agent to stop is not an enforcement mechanism.

The “$2,000” in the original headline is an illustrative scenario, not a documented typical loss. The controls below are designed to stop a run from continuing unchecked and help you find out what happened if one gets stuck.

Why one limit is not enough

An agent can spend money through more than its visible final answer. It may make multiple model calls, retry failed requests, call tools, delegate work to subagents, or cycle through actions without advancing its task. A request-rate limit can constrain how quickly calls happen, but it does not decide whether those calls are useful. A provider spend limit constrains aggregate account or project spending, but may not stop a particular run at a precise amount.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use controls at different scopes. The run-level guard is the first line of defense because it can reject the next action before dispatch. Workspace, project, and organization limits provide additional protection if the run-level controls fail or the workload exceeds its expected budget.

Control Scope and what it limits When it acts What it does not establish
Application run budget One run; for example, iterations, elapsed time, calls, tokens, or an estimated cost ceiling. Before dispatching the next model or tool action, if your orchestrator checks the budget there. It cannot account perfectly for charges your application does not meter or know about.
Retry policy A particular operation; limits repeated attempts and retry duration. After a failed request and before another attempt. It does not stop a separate logical loop that keeps issuing new operations.
Provider rate limit Requests or tokens over a time interval, as set for the account or service. When provider throughput limits are reached. It is not a currency budget or a test of task progress.
Provider spend limit Aggregate spending at the provider’s configured scope, such as a project, workspace, or organization. According to the provider’s billing and enforcement behavior. It is not necessarily immediate or precise enough to cap a single run at an exact amount.
Tracing and billing records Activity and usage attribution across runs, calls, tools, and services. During operation for traces; through provider billing records for billed usage. A trace is not automatically a complete or authoritative invoice.

Set a finite budget for every run

Choose limits based on the workload and enforce them in the orchestration code. The key design rule is to check the budget before dispatch—not only after a response arrives—so a run that has reached a limit cannot start one more expensive action.

Choose independent ceilings

  • Steps or iterations: Set the maximum number of agent decisions or loop iterations.
  • Elapsed time: Set a wall-clock deadline so a slow or stalled run cannot persist indefinitely.
  • Model calls: Count every dispatched model request, including retries and calls made by delegated agents.
  • Tokens: Track input and output usage where available. Include cached input and reasoning usage when the provider reports them; do not treat a cached-input percentage as the run’s total cost.
  • Currency: Where your system can estimate or meter charges, set a per-run ceiling as an additional guard. Label estimates as estimates and allow for billing details that may not be available at dispatch time.
  • Tool actions: Limit costly or potentially repeating tools separately where appropriate, especially if their charges are billed outside the model provider.

Do not assume that a model’s requested output limit is a run-wide token budget. A multi-call run can consume input and output repeatedly, and a workflow with tools or subagents can generate usage beyond the top-level response.

Check before every dispatch

Keep a single run budget shared by all work spawned from that run. Before sending a model request, retry, tool action, or subagent task, check whether the relevant ceilings permit it. Reserve expected usage for the action when practical, then reconcile that reservation against actual usage after the response. If usage is unknown or the remaining budget is insufficient, stop or require an explicit operator decision rather than silently proceeding.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A conceptual guard might look like this:

async def dispatch(run, action):
    if run.deadline_passed():
        raise RunStopped("time limit reached")
    if run.iterations >= run.max_iterations:
        raise RunStopped("iteration limit reached")
    if run.calls >= run.max_calls:
        raise RunStopped("call limit reached")
    if run.remaining_token_budget < action.estimated_tokens:
        raise RunStopped("token budget reached")

    run.reserve(action.estimated_cost)
    try:
        result = await action.execute()
        run.record_usage(result.usage)
        return result
    finally:
        run.record_attempt(action)

This illustrates the placement of checks, not a provider-specific API or complete cost-metering implementation. Production code should persist run state, handle concurrent child work safely, release or reconcile reservations, and record why a run stopped.

Stop loops that make no progress

Numeric ceilings catch runaway activity even when its pattern is unfamiliar. Add application-level signals for common loops as well: repeated identical tool arguments, recurring errors, revisited states, and actions that fail to change the task state. Decide which signals should terminate a run and which should pause it for review. A prompt can encourage the agent to report a problem, but only the orchestrator can reliably block the next dispatch.

Make retries bounded and rate-limit aware

A 429 response is not a single diagnosis. It can indicate a temporary rate limit, an exhausted credit balance, or a configured usage or spend limit. Inspect the provider’s response and error code before retrying: waiting may help with a temporary rate limit, while a spend-limit or exhausted-credit error requires an account or configuration change.

  1. Classify the response. Read the error details and distinguish a rate-limit response from a spend-limit, usage-limit, or credit error.
  2. Honor Retry-After when provided. Do not retry earlier than the server’s requested delay.
  3. Otherwise, use exponential backoff with jitter. Increase the delay between attempts and add randomness so concurrent workers do not all retry together.
  4. Set both an attempt limit and a time limit. End retries when either ceiling is reached, and pass every retry through the run budget.
  5. Make side-effecting operations idempotent where possible. A retry should not create duplicate work or transactions if the first attempt succeeded but its response was lost.

Unsuccessful requests can contribute to rate-limit pressure. Also check whether your SDK already retries: application-level retries layered on top of SDK retries can multiply attempts unless both are included in the same finite policy. OpenAI’s 429 troubleshooting guidance describes these distinctions and recommends respecting retry timing rather than repeatedly sending requests.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use provider limits as a secondary circuit breaker

Set provider-side limits in addition to—not instead of—the run budget. Where the provider supports separate project or workspace controls, isolate workloads that have different owners or risk profiles. An organization-wide ceiling can provide another boundary, but consider what happens if it blocks legitimate production traffic.

OpenAI API controls

OpenAI documents monthly spend alerts and configurable organization- and project-level spend limits. Both levels can apply to a request. Alerts notify you; they do not stop spending. OpenAI also warns that hard-limit enforcement is not instantaneous, so recorded spend can slightly exceed the configured amount while the provider’s state propagates. If a request is rejected, use the returned error code to distinguish a spend limit from an approved-usage limit or exhausted prepaid credits. See OpenAI’s Spend limits documentation.

Rank #4
API Security in Action
  • API Security in Action
  • Manning Publications
  • ABIS BOOK

Anthropic Claude API controls

Anthropic documents organization and workspace spend limits, alongside request and token rate limits. API usage pauses when a configured spend cap is reached and remains paused until reset unless the limit is raised sooner. Rate-limit and spend-limit failures have different response details. Because tiers and limits can change, use the current account console and Anthropic’s Claude API rate limits documentation rather than relying on a copied tier table.

Amazon Bedrock AgentCore controls

Amazon Bedrock AgentCore documents harness controls including maxIterations, timeoutSeconds, and maxTokens, as well as idle-session timeout and maximum session lifetime. Its documentation lists defaults of 75 iterations, 3,600 seconds, no default token budget, 900 seconds, and 28,800 seconds, respectively. These are documented service defaults, not recommended safe settings for every workload; choose explicit values for your use case. See Amazon Web Services’ Amazon Bedrock AgentCore observability and cost controls documentation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Record enough to investigate an incident

For each run, record a stable run or session ID, the model used, timestamps and duration, each model call and tool action, reported token usage, retries, delegated-agent activity, and the stop reason. Preserve relevant error codes and response timing so you can tell a repeated rate-limit retry from a loop of new actions.

Provider usage fields may be incomplete or unavailable for some events. For Agents API activity, OpenAI’s dashboard can expose sessions, events, turns, tool calls, and subagent activity, while some usage values may be best-effort or null when unknown. Cost can involve input and cached input tokens, output and reasoning tokens, retries, subagents, tools, sandbox compute, third-party charges, and cache-write charges where applicable. Reasoning tokens are billed as output tokens, according to OpenAI’s Agents API observability and usage documentation. Reconcile trace data against provider billing records and any third-party service invoices.

AWS likewise distinguishes operational observability from billed usage: its AgentCore documentation directs users to Cost Explorer or the Cost and Usage Report to inspect billed usage. A trace helps explain activity; it should not be treated as the bill.

Test the stop paths before relying on them

Exercise guardrails in a controlled environment with representative failure cases. Confirm that each case blocks the next dispatch, produces an understandable stop reason, and leaves enough records for an operator to diagnose it.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Quick Recap

  • Make a tool return the same error repeatedly and verify that retries stop at the configured attempt or time ceiling.
  • Simulate a transition that revisits the same state or repeats the same tool call.
  • Start nested or delegated work and confirm that it shares the parent run’s limits rather than receiving an unbounded fresh budget.
  • Exhaust the iteration, time, token, and estimated-cost budgets individually; verify that no further action is sent after each limit is reached.
  • Simulate a provider rate limit and a provider spend-limit error separately; check that the first follows its retry policy and the second is surfaced for operator action.

When a run has already gone out of control

  1. Stop the source of new work. Cancel or pause the affected run using your application’s controls, and disable the triggering job or queue consumer if it is continually starting more runs.
  2. Identify the affected scope. Use run and session IDs, model and tool records, timestamps, and provider project or workspace attribution to separate the incident from unrelated traffic.
  3. Inspect both traces and billing data. Follow the call sequence to find repeated actions, retries, delegation, or a stuck state; then compare the activity with provider billing records and third-party charges.
  4. Restore service deliberately. If a provider cap has blocked legitimate requests, assess the impact before changing it. Raising a limit may restore service, but it also removes that particular backstop at the higher ceiling.
  5. Close the failure path. Add or correct the run guard, retry bound, progress signal, or workload isolation implicated by the incident, then exercise the corresponding stop test before re-enabling automation.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.