Runaway LLM cost: how to get alerted before an agent burns money
On this page
To catch runaway LLM cost, have every agent run report what it cost, either as a dollar figure or as the model name and token counts, and alert on three things: a run that costs several times its usual amount, a run that passes a hard per-run limit, and a day whose runs pass a daily limit. Your provider's bill arrives too late to stop a loop that started this morning. Per-run reporting tells you within the run or the day.
Why provider dashboards are too slow on their own
A runaway agent is usually a loop: the same tool call, a retry with no end, a prompt that grows each turn. It costs real money in minutes, and it often finishes with exit code 0. A monthly bill, or a usage page you check on Fridays, shows it after the fact. Set your provider's own budget or spend limit as a backstop, and add a check that looks at each run as it happens.
Step 1: Report the cost of each run
On the finish ping, send one of these in a JSON body:
| Field | Use it when |
|---|---|
cost_usd | You already know the cost, for example from your provider's response or your own price table. It wins over tokens |
model, input_tokens, output_tokens | You know the tokens. Stallbell works out the cost from its own list-price table |
Stallbell's price table covers common models from OpenAI, Anthropic, Google, Mistral and DeepSeek, for example gpt-4.1-mini, claude-sonnet-4 and gemini-2.5-flash. A model name that isn't in the table gets no computed cost, so send cost_usd for those.
Here's a standard-library Python wrapper. Replace call_model() with your agent:
# cost_report.py: report each run's model and tokens to Stallbell.
import json, sys, uuid, urllib.request
PING = "https://stallbell.com/p/TOKEN"
run_id = str(uuid.uuid4())
def ping(path, **fields):
body = json.dumps({"run_id": run_id, **fields}).encode()
req = urllib.request.Request(PING + path, data=body, method="POST",
headers={"content-type": "application/json"})
try:
urllib.request.urlopen(req, timeout=10).read()
except Exception as exc: # never let monitoring break the job
print("stallbell ping failed:", exc, file=sys.stderr)
def call_model():
# Your agent here. Return the answer and the usage your provider reported.
return "Summary: 3 new leads", {"model": "gpt-4.1-mini", "input": 1200, "output": 80}
ping("/start")
try:
answer, usage = call_model()
ping("/0", output=answer[:2000], model=usage["model"],
input_tokens=usage["input"], output_tokens=usage["output"])
except Exception as exc:
ping("/fail", msg=str(exc)[:500])
raiseFor a long agent, send the cost of each stretch of work on /progress pings with the same run_id. Stallbell adds them to the run and to the day's total, so the daily cap sees spend before the run ends.
Step 2: Turn on the three cost rules
All three live in the monitor's settings:
| Setting | Default | What it does |
|---|---|---|
| Cost spike | 3 | Alerts when a finished run costs more than 3 times its usual cost |
| Per-run cost cap | blank (off) | Alerts when a finished run's cost reaches the amount you set |
| Per-day cost cap | blank (off) | Alerts when the day's runs together reach the amount you set |
"Usual cost" is the median of the last 14 runs that reported a cost (Usual cost sample). The cost spike rule waits until at least 5 of them exist (Minimum cost samples), and ignores runs under $0.01 (Ignore costs under), so a cheap run that doubles from a fraction of a cent doesn't page you.
How to pick the caps:
- Per-run cost cap: a little above the most a normal run has ever cost. A run that hits it is doing something it has never done.
- Per-day cost cap: the amount you'd be unhappy to find spent by lunchtime. The day is counted in the project's timezone, and the cap alerts once per day.
What you get
A cost alert names the monitor and puts the run's cost next to its usual cost or the cap it reached. A spike incident is titled like this: "Cost spike: $0.42 on one run (median $0.04, 10.5×)". A spike or per-run alert closes on its own when a later run comes back to normal, and a daily cap alert closes when the day rolls over.
What this doesn't do
- It doesn't stop spending. A cap sends an alert. It doesn't cancel the run, revoke a key or touch your provider account. Pair it with a step limit in your code (see how to detect an agent stuck in a loop) and your provider's own spend limit.
- It doesn't read your bill. Stallbell only knows the cost or tokens your job reports. Its table holds public list prices, so discounts, cached-token pricing and batch pricing aren't reflected unless you send
cost_usdyourself. - The spike and per-run rules judge finished runs. A run that never finishes is caught by the Stuck run rule and the daily cap, not by these two.
FAQ
Should I send cost_usd or tokens? Send cost_usd if you have it, because it reflects your real prices. Send the model and tokens if you don't, and Stallbell prices them at list rates.
Does the cost spike rule work on day one? No. It needs 5 runs with a cost before it has a usual cost to compare against. The per-run and per-day caps work from the first run.
What about agents built with LangChain or CrewAI? Both report token usage after a run. See how to monitor LangChain and CrewAI agents for where to read it.
What does it cost? There's a Free plan with no card. Plans and limits are on the pricing page.
Related: Detect an agent stuck in a loop · How to monitor AI agents that run on a schedule · Alert on empty LLM output