How to · Agent cost

AI Agent Cost Control, Budgeting the Everyday Agent Run

By Ghiles Asmani, founder of Weckr

Most writing about agent cost is about disasters: the runaway loop that fires a thousand calls overnight. Those are real, and you should catch them. They are also not where most of your agent money goes.

Most of it goes to agents working exactly as designed. Normal multi step tasks that quietly cost more than you think, every day, with no bug in sight.

This is about managing that everyday spend: tracking cost per run, budgeting per task, and knowing when to nudge yourself versus when to pull the plug.

Even a working agent is expensive

The thing that makes agents useful is also what makes them costly. A single task is not a single call, it is a little cascade of them.

The agent plans an approach, calls a tool, reads the result, decides what to do next, maybe reflects on whether it is done, and sometimes retries a step that came back messy. Every one of those is a model call with its own tokens. A task you picture as one action can be fifteen or twenty calls under the hood, and nothing about that is a malfunction.

So the cost per task is a multiple of the cost per call, and it is easy to underestimate by an order of magnitude. The emergency version of this, when the cascade never ends, is covered in how to detect and stop agent reasoning loops.

The unit that matters is the run, not the call

You think in tasks. Summarize this repository. Draft these five replies. Research this company. But your bill is itemized in calls, and the two do not line up.

That mismatch is why agent cost feels slippery. The fix is to measure in the same unit you think in, the run, which is one full task from start to finish. Once you know what a run costs on average, everything else gets easier: you can price it, budget it, and spot the ones going wrong.

How to track cost per run

The mechanic is simple. Mint a run id when a task starts, attach it to every call the task makes, and sum cost by run id when it finishes.

// one run = one task; accumulate its cost and cap it
const runId = crypto.randomUUID()
let runCost = 0
const RUN_BUDGET = 0.5 // dollars a single task is allowed to spend

async function step(params) {
  if (runCost >= RUN_BUDGET) {
    throw new Error('run budget exceeded, stopping task ' + runId)
  }
  const res = await openai.chat.completions.create(params)
  runCost += costOf(res.usage, params.model)
  return res
}

Now a task is a number, not a mystery. You can log the average cost per run, watch it over time, and put a ceiling on any single task so a bad one cannot spend your afternoon.

Set a budget per run, and per user

A per run budget stops one task from running away. It does not stop a user from starting five hundred tasks. You want both granularities.

The per run budget bounds the worst single task. The per user cap bounds the worst customer across all their tasks, which is the subject of per user spending caps for LLMs. Together they cover the two ways agent cost gets away from you: one task going deep, and one user going wide.

When to alert, and when to kill

Not every expensive run is a problem. Some tasks are genuinely hard and deserve the tokens. So use two thresholds, not one.

  • Soft threshold, alert. The run is pricier than a typical one but still plausible. Let it finish and send yourself a heads up, so you learn which tasks are heavy without breaking them.
  • Hard threshold, kill. The run has blown past anything a real task could justify. That almost always means it is stuck, so stop it and return what you have.

The gap between the two thresholds is your tolerance for a hard task versus a broken one. Tune it from the real distribution of run costs once you are tracking them.

How this complements loop detection

It helps to hold the two ideas as different instruments. Loop detection is the emergency brake, and cost control is the speedometer and the budget.

The brake catches the rare disaster, a run spiking tokens fast, and stops it. The budget manages the common case, the steady everyday spend of healthy runs adding up. Weckr gives you both: velocity and loop detection for the spike, plus cost per user and per feature and per plan caps for the everyday budget. The end to end view of agent spend is in how to manage AI agent costs.

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: cost is tracked per user and feature,
// velocity spikes are flagged, and the plan cap bounds the everyday spend
const response = await wk.chat(openai, {
  model: 'gpt-4o-mini',
  messages,
  userId: user.id,
  feature: 'agent',
  plan: user.plan,
})

You still thread a run id for the finest grain, but Weckr covers the user level budget and the emergency detection for you. Set it up from the docs, see it on seeded data at the demo, and place it in the wider system in the guide to AI cost and margin management.

FAQ

What is AI agent cost control?

It is the everyday management of what your agents spend, as opposed to the emergency of catching a runaway loop. Even a perfectly working agent fires many model calls per task for planning, tool use, and reflection, so cost adds up fast without any bug. Cost control means tracking spend per run, setting a budget per task, and deciding when to alert versus when to stop a run.

Why does one agent task cost so much?

Because a single task is not one call. An agent plans, calls tools, reads the results, reflects, and often retries, and each of those steps is a model call with its own tokens. A task you think of as one action can quietly be twenty calls, so the cost per task is many times the cost of a single request.

How do I track cost per agent run instead of per call?

Give each task a run id, tag every model call made during that task with it, and sum cost by run id. That turns a pile of individual calls into a per task number you can actually reason about and budget against. Weckr gives you the per user and per feature half of this automatically, and you thread a run id for the finest grain.

When should I alert on an agent run versus kill it?

Alert when a run is more expensive than usual but might just be a genuinely hard task, so you let it finish and get a heads up. Kill when a run blows past any budget a real task could justify, which almost always means it is stuck. A soft threshold that notifies and a hard threshold that stops covers both cases.

How is cost control different from loop detection?

Loop detection is the emergency brake: it catches a runaway agent burning tokens fast and stops the disaster. Cost control is the speedometer and the budget: it manages normal everyday spend across healthy runs. You want both, because most of your agent cost is not emergencies, it is ordinary multi step work adding up.

Keep reading

Budget the everyday, not just the emergency

The runaway loop gets the headlines, but the steady drip of normal agent runs is where most of the money quietly goes. Measure per run, budget per task and per user, and alert before you kill.

Weckr handles the user level budget and the loop detection so you only build the run id piece. Start at the demo.

See the dashboard with real data, no signup needed.

Try the demo →