How to · Agent cost

Your AI Agent Is Burning Through Your Budget. Let Us Stop It

By Ghiles Asmani, founder of Weckr

You built an agent, it works, and then you glanced at the usage graph and felt your chest tighten. It is making far more calls than you pictured, and the line is still climbing while you stare at it.

This is the scary kind of cost, so let us move quickly. First why agents do this, then a circuit breaker you can paste in tonight, then how to make sure it never happens quietly again.

You are not being reckless. Agents are just a different animal from a normal API call, and nobody warns you until the graph does.

Why agents are the dangerous kind of cost

A normal endpoint is predictable. One request, one call, one cost. You can multiply it out and know roughly what a busy day looks like.

An agent throws that out. It decides for itself how many calls to make, looping and chaining tool calls until it thinks it is done. When the reasoning goes sideways, done never arrives, and it keeps calling.

The autonomy is the whole point and also the whole danger. Because no human is in the loop, a runaway agent can fire hundreds of calls in minutes and nobody is there to say stop.

A loop scenario, and the cost per minute

Picture an agent trying to complete a task that depends on a tool call. The tool returns something slightly malformed, the agent decides to try again, and its own retry logic never gives up.

Say it fires about two calls a second, and because it keeps stuffing the growing history back into the prompt, each call is around 4,000 tokens on a flagship model. That is roughly 8,000 tokens a second, or nearly half a million tokens a minute.

At flagship input prices that is comfortably over a dollar a minute, call it $70 an hour, from one stuck agent. Kick it off before you close the laptop and by morning a single loop has spent a few hundred dollars, and every individual call looked completely normal. The math behind this is the same one in how to detect and stop agent reasoning loops.

The manual circuit breaker

The core idea is simple: watch how many tokens a user burns in a short window, and pull the plug if it crosses a line. This is a token velocity check.

// rough token velocity circuit breaker (per user)
const WINDOW_MS = 5 * 60 * 1000  // 5 minutes
const LIMIT = 50_000             // tokens allowed in the window

const buffer = new Map() // userId -> [{ t, tokens }]

function recordAndCheck(userId, tokens) {
  const now = Date.now()
  const events = (buffer.get(userId) ?? []).filter(e => now - e.t < WINDOW_MS)
  events.push({ t: now, tokens })
  buffer.set(userId, events)

  const total = events.reduce((sum, e) => sum + e.tokens, 0)
  if (total > LIMIT) {
    throw new Error('token velocity exceeded, halting agent')
  }
}

Call recordAndCheck after every step, and a loop trips the breaker within minutes instead of running all night. For one agent, this is genuinely enough.

Why the manual version breaks down

The problem is not the idea. The idea is correct. The problem is that the naive version rots the moment you grow.

  • The state is per process. On serverless it resets on every cold start and is not shared across regions, so your five minute window is full of holes and misses real spikes.
  • You have to wire it everywhere. Every agent, every user path, every new feature needs the same check bolted on, and one you forget is the one that runs away.
  • No alert, no history. The breaker stops the call, but you never find out it happened, so you cannot see which user or which agent keeps tripping it.

It works for one agent on one server. Add a second agent, a few thousand users, and a multi region deploy, and the homegrown version quietly stops protecting you. Managing agent cost as you scale is a different job from catching one loop.

The automated version: loop detection that watches everything

The reliable version moves the velocity check out of each process and into one place that sees every user and every agent at once. That is what Weckr does.

You wrap your existing client, and Weckr tracks token velocity per user centrally. When someone crosses the threshold, the default being 50,000 tokens in a 5 minute window, it fires a Slack or email alert so a runaway agent surfaces in minutes, not at month end. It can also enforce a hard cap that blocks or downgrades the user automatically.

import { Weckr } from '@weckr/sdk'

const wk = new Weckr({
  apiKey: process.env.WECKR_API_KEY,
  plans: { free: 0, pro: 29 },
})

// every agent step goes through wk.chat, so velocity is tracked for you
const response = await wk.chat(openai, {
  model: 'gpt-4o-mini',
  messages,
  userId: user.id,
  feature: 'agent',
  plan: user.plan,
})

Same idea as your hand rolled breaker, except the state is shared, the threshold is tunable, and you actually get told when it trips. You can watch the loop alert fire on seeded data at the live demo, and pair it with per user spending caps so a tripped agent is contained, not just flagged.

FAQ

Why do AI agents burn through budget so fast?

Because an agent decides for itself how many calls to make. A normal endpoint makes one call per request, but an agent can loop, retry, and chain tool calls dozens or hundreds of times chasing a goal. A single bad reasoning loop can fire hundreds of calls in minutes, and because it is autonomous, no human is watching while it happens.

How much can a runaway agent cost per minute?

More than you would guess. An agent stuck in a loop firing two calls a second at around 4,000 tokens each on a flagship model can burn over a dollar a minute, roughly $70 an hour. Left running overnight, one stuck agent can quietly spend a few hundred dollars before anyone logs in and notices.

How do I add a circuit breaker to an AI agent?

Track tokens per user in a rolling time window and stop the agent if the total crosses a threshold. A simple version keeps a list of recent token counts per user, sums the ones inside the last five minutes, and throws to halt the loop if that sum passes a limit like 50,000 tokens. It is a dozen lines and it will save you from the worst case.

Why do manual circuit breakers stop working as you scale?

Because the state lives in one process. On serverless it resets on every cold start and is not shared across regions, so the window is unreliable. You also have to wire the same check into every agent and every user path, tune thresholds by hand, and you still get no alert and no dashboard. It works for one agent and rots as you add more.

What is a good token velocity alert threshold?

A common default is 50,000 tokens in a 5 minute window per user. That is high enough that normal heavy use does not trip it, and low enough that a real loop is caught within minutes rather than at invoice time. Weckr uses that default out of the box and lets you tune it per project.

Keep reading

Put a limit on the thing that has no limit

An agent will happily call until it succeeds or until your budget runs out, whichever comes first. Left alone, it does not know which one you would prefer.

Weckr gives every agent a live token velocity watch, an alert when one runs hot, and a cap so a loop cannot empty your account overnight. See it working first at the demo, or read the whole approach in the AI cost and margin guide.

See the dashboard with real data, no signup needed.

Try the demo →