How to Cap AI Agent Spend Before a Runaway Agent Blows Your Budget
A runaway agent does not send a warning. It just keeps calling the model. A retry loop that should have stopped after three attempts runs three thousand times overnight, and you find out when the invoice arrives. The fix is not more discipline. It is a hard cap that lives somewhere the agent cannot edit around.
TL;DR: Cap AI agent spend in three layers. Set max_tokens and a turn limit in the agent code. Track cumulative session cost and abort when it crosses a threshold. Then enforce a real dollar ceiling at the proxy layer, outside the agent, so a crash, a fresh process, or a forgotten check cannot bypass it. The first two layers are good practice. The third is the one that actually saves you at 3am.
Why do agents blow through budgets?
Almost every agent is a loop: observe, think, act, repeat until done. The word “done” is where the money goes. A few common failure modes:
Retry loops. The agent hits a failing tool call and keeps rephrasing the same request. Each pass burns input and output tokens with nothing to show for it.
No output ceiling. With no max_tokens, a single response can run to thousands of tokens. Multiply across hundreds of calls and the total climbs quietly.
No session memory of spend. The agent does not know the last thirty calls cost four dollars. It has no concept of a budget, so it never stops on its own.
Fan-out. Concurrent sub-agents each accumulate cost independently. One bug in shared logic multiplies across every instance at once.
The pattern is always the same: no natural pause point, and no ceiling that the agent cannot route around.
How much can a runaway agent actually cost?
Run the numbers. A capable model at roughly $3 per million input tokens and $15 per million output tokens looks cheap per call. A typical turn of 2,000 input and 800 output tokens is a fraction of a cent. But a 50-turn task is about $0.90. A single stuck session that runs 500 turns instead of 50 is $9 or more, from one bug, in one loop. Now run that across a fleet overnight. That is how a $15-in-8-minutes story becomes a $2,000 morning.
Layer 1: cap spend inside the agent
Start with the cheapest guardrails. Always set max_tokens on every request, and add a turn counter that hard-stops the loop. Then track cumulative cost in the session and throw when it crosses a limit:
let totalInput = 0;
let totalOutput = 0;
const SESSION_BUDGET_CENTS = 50; // $0.50 per session
function checkBudget(usage: { input_tokens: number; output_tokens: number }) {
totalInput += usage.input_tokens;
totalOutput += usage.output_tokens;
const costCents =
(totalInput / 1_000_000) * 300 +
(totalOutput / 1_000_000) * 1500;
if (costCents > SESSION_BUDGET_CENTS) {
throw new Error('Session budget exceeded');
}
}This works, and every agent should have it. But it has a real weakness: it lives in the agent. If the process crashes and restarts, the counters reset to zero. If a teammate ships a new agent and forgets the check, there is no net. Changing the limit across a fleet means deploying code to every service. The guardrail is only as reliable as the code that remembers to run it.
Layer 2: enforce a real ceiling at the proxy
The durable fix is to move the ceiling out of the agent entirely, to a proxy that every request passes through. Now enforcement happens in one place, consistently, no matter which agent sent the call or how many times it restarted.
This is the layer RelayPlane is built for. In practice it gives you:
Hard daily and per-request caps. Set a dollar ceiling per day, per hour, or per request. When it is hit, the proxy returns an error instead of forwarding the call. The agent gets a clear signal to stop, and the bill stops growing.
Enforced max_tokens. The proxy applies an output cap even when the calling code forgot to. You set the default once in config, not in every agent.
A kill switch. One flag stops all outbound calls. When something looks wrong, you cut spend to zero instantly rather than hunting for the right process to kill.
Per-request cost attribution. Every call is priced into a local ledger with its token counts, so you can see which agent and which session spent what, before the monthly bill instead of after.
Setup is three lines, no Docker and no database:
npm install -g @relayplane/proxy
relayplane init
relayplane startPoint your agents at the proxy endpoint instead of the provider, and set your caps in config. Because the ledger and the limits live outside the agent, a crash, a restart, or a missing check cannot bypass them.
In-code checks vs proxy enforcement
| In-code checks | Proxy enforcement | |
|---|---|---|
| Where it lives | Inside each agent | Outside the agent, at the gateway |
| Survives a crash or restart | No, counters reset to zero | Yes, state lives in the proxy |
| Covers a forgotten check | No, only agents that opt in | Yes, every request passes through |
| Change limits fleet-wide | Deploy code to every service | Edit one config |
| Kill switch | Manual, per process | One flag stops all calls |
What limits should I actually set?
Start conservative and loosen later. A per-session cap slightly above your worst legitimate task catches infinite loops without blocking real work. A daily cap sized to your expected volume plus headroom catches fleet-wide problems. Set both. The per-session cap stops one broken agent. The daily cap stops the case where every agent misbehaves at once.
Rule of thumb: if losing the whole daily cap in an hour would hurt, your daily cap is too high, or you have not set an hourly one.
Bottom line
Capping AI agent spend is a solved problem once the guardrails sit in the right place. In-code checks are a good start and cost nothing to add. But the ceiling that actually saves you has to live outside the agent, at a layer a broken loop cannot edit, restart past, or forget. Set max_tokens and turn limits, track session cost, and put a proxy in front that enforces a hard cap and gives you a kill switch.
For the deeper background on why this happens, see agent runaway costs. If you are still choosing a gateway underneath all this, the LiteLLM vs OpenRouter comparison covers the two most common options.
RelayPlane is free and MIT-licensed. Start with the quickstart and put a cap on the agents that scare you.
Matt Turley builds RelayPlane, a free open-source LLM proxy that meters spend per request and enforces hard caps. Source at github.com/RelayPlane/proxy. Last updated 2026-09-06.
Meter every LLM request on your own machine
RelayPlane is a free, MIT-licensed npm proxy. It prices every request into a local ledger, enforces a hard daily cap, and has a kill switch. No Docker, no account.