Getting Your First Agent Live

Agent Cost Runaway Detection: Stopping Infinite Loops Before They Hit Your Bill

Matt Doughty Matt Doughty CEO & Co-Founder, Prefactor
6 min read
Abstract illustration: Agent Cost Runaway Detection: Stopping Infinite Loops Before They Hit Your Bill

What you will build

By the end of this article you will have a working design for three controls: a per-agent token budget tracked in a shared registry, a cost circuit breaker that evaluates spend rate before each API call, and an enforcement gate that halts execution when the budget ceiling is reached. Together they stop runaway loops in under 60 seconds instead of 11 days.


Why the incident keeps repeating

In November 2025, a four-agent LangChain market research pipeline entered an infinite loop between its Analyzer and Verifier agents. The two agents ping-ponged requests with no budget cap and no enforcement mechanism. The billing dashboard surfaced the damage 11 days later: $47,000 gone. The post-mortem named two root causes: no per-agent budget ceiling, and no pre-execution enforcement.

That pattern is not unusual. 85% of companies miss AI cost forecasts by more than 10%, and nearly 25% underestimate by 50% or more, largely because instrumentation stops at the model API level rather than at the individual agent. When you cannot see what each agent is consuming, you cannot act before the bill arrives.

The structural reason loops are expensive is how token costs accumulate in multi-agent systems. A 2026 Concordia University study found a 2-to-1 input-to-output token ratio as a communication tax between agents, meaning every message an agent sends triggers roughly twice as many tokens in context overhead. A loop that completes 100 iterations before anyone notices does not cost 100 times the price of one iteration. It costs more, because each iteration carries the accumulated context of everything before it.

Understanding how agents consume tokens at each reasoning step makes clear why detection has to happen in the execution path, not in a reporting dashboard checked the next morning.


The three controls

1. Per-agent token budget in a shared registry

A per-agent budget is a numeric ceiling, denominated in tokens, assigned to a named agent identity before any run begins. The registry records the current consumption for each agent identity and the ceiling assigned to it. Every agent queries the registry before making an API call and after receiving a response.

Store budgets in a fast key-value store accessible to all agents in the system. The agent identifier is the key. The value holds three fields: the ceiling, the tokens consumed in the current run, and the tokens consumed in the current rolling window (hourly or daily, depending on your workflow).

A ZopDev production deployment in May 2026 used exactly this structure, adding hourly token caps alongside per-run limits. In the first two weeks, the registry revealed nine agents consuming 180,000 tokens per day against an expected 20,000. That discovery came from the registry, not from the billing dashboard. The first runaway was caught within 60 minutes with $200 in damage rather than an estimated $5,000.

When you instrument your agent observability layer to feed every token count back to the registry immediately after each API response, the registry becomes the single source of truth for agent spend state.

2. Cost circuit breaker on spend rate

A token budget ceiling catches absolute overruns. A circuit breaker catches rate overruns, situations where an agent is consuming tokens faster than any legitimate task could require.

The circuit breaker measures tokens consumed per unit time, for example per minute, and compares that rate against a threshold you set per agent type. A reference implementation tested in June 2026 used a threshold of 10,000 tokens per minute and caught a runaway loop within 60 seconds with no meaningful cost accumulation.

flowchart TD
    A[Agent requests API call] --> B{Registry: budget remaining?}
    B -- No --> C[Halt execution, write state]
    B -- Yes --> D{Circuit breaker: rate OK?}
    D -- No --> E[Trip breaker, alert on-call]
    D -- Yes --> F[API call proceeds]
    F --> G[Response received]
    G --> H[Update registry: consumed tokens]
    H --> A

The circuit breaker has three states: closed (calls proceed normally), open (calls are blocked), and half-open (one test call is allowed through to check whether the rate has returned to normal). When the breaker trips, the agent writes its current task state to persistent storage before any blocking occurs. That preserves the ability to resume the run after a human reviews what happened.

Set rate thresholds by running your agent against real workloads and observing the 99th-percentile token rate during normal operation. Multiply that by three to four to get a threshold that distinguishes a loop from a legitimately large task.

3. Enforcement gate in the hot path

The budget check and the circuit breaker are only useful if they run before the next API call, not after. The enforcement gate is the code that sits between your orchestrator and your model API client.

flowchart TD
    A[Orchestrator schedules agent step] --> B[Enforcement gate]
    B --> C{Budget check passes?}
    C -- No --> D[Block step, emit BUDGET_EXCEEDED event]
    C -- Yes --> E{Rate check passes?}
    E -- No --> F[Block step, emit RATE_EXCEEDED event]
    E -- Yes --> G[Execute agent step]
    G --> H[Update registry]
    H --> A

In LangChain-based systems, this gate is a callback handler registered on the agent executor. In CrewAI, it wraps the tool call layer. In systems built on the Model Context Protocol, the gate lives in the MCP server middleware before any tool invocation reaches the model. Tools like Prefactor implement this gate as a managed layer so you do not have to maintain the enforcement logic yourself.

The gate must be synchronous relative to the call it guards. An asynchronous check that fires a webhook and hopes the agent pauses is not an enforcement gate. It is a notification with no teeth.

Align your enforcement gate design with your broader AI governance framework so that budget policy is defined centrally and the gate simply enforces what the policy specifies. That separation lets you adjust ceilings without touching agent code.


Setting budget ceilings that do not throttle legitimate work

A ceiling set too low creates false positives that interrupt real work and erode team trust in the system. The AI governance principles that govern your agent deployment should include a budget calibration process: run each agent against a representative workload sample, measure median and 95th-percentile token consumption per task, and set the ceiling at twice the 95th percentile. Adjust after the first 30 days of production data.

Budget enforcement is also the control that makes agent governance auditable. When every agent run has a recorded ceiling, actual consumption, and a halted or completed status, you have the data needed to demonstrate cost controls to finance and security reviewers.

Gartner expects more than 40% of agentic AI projects to be canceled by end of 2027 due to escalating costs, unclear business value, or inadequate risk controls. Per-agent budgets with enforced ceilings directly address the cost and risk control factors in that forecast.


Where to start

Audit your current agent deployments for three things: whether each agent has an assigned token ceiling, whether that ceiling is checked before each API call, and whether a rate-based circuit breaker is in the execution path. If any of those are missing, the $47,000 incident pattern is still possible in your environment. Take the agent readiness assessment to get a structured gap analysis across your deployment.

Matt Doughty Matt Doughty CEO & Co-Founder, Prefactor

Founder of Prefactor, writing on the operational reality of getting AI agents into production — evaluation, observability, governance, and the plumbing assistants never needed.

Frequently asked questions

How is a token budget different from a spending alert?

A spending alert fires after the cost has accumulated and your monitoring system has processed the data, which can be hours later. A token budget is checked before each API call completes, so it can block the call rather than report on it after the fact.

Which agent frameworks support budget enforcement natively?

Most frameworks, including LangChain and CrewAI, do not enforce per-agent token budgets by default. You add enforcement as middleware or a pre-execution hook. The circuit breaker pattern described in this article works across frameworks because it sits between your orchestrator and the API client.

How do I set a starting budget for an agent I have never run in production before?

Run the agent against a representative sample of your actual workload in staging, measure median and 95th-percentile token consumption per task, then set your production budget ceiling at twice the 95th percentile. That gives you room for legitimate variance while still catching runaway conditions, which typically exceed expected consumption by an order of magnitude.

What happens to in-flight work when a circuit breaker trips?

The agent should write its current state to a persistent store before the enforcement gate blocks the next call. The run is then marked as halted rather than failed, so you can inspect the state, adjust the budget if the work was legitimate, and resume without losing completed steps.

Stay ahead of the curve

No spam. Unsubscribe anytime. A resource by Prefactor.

Almost there — check your inbox to confirm your subscription.