An intern's agent loop drains a team's monthly model budget over a weekend: what should the gateway do when a budget runs out mid-stream or mid-run, and what should callers see?
answer
- admission, not interruption
- in-flight calls already hold their cost
- a budget stop is not a throttle
- the stream's status is already sent
- a smaller key under the team cap
basics
~20 sRefuse the next call, not the one in flight: admitted calls finish within their reservation, and new ones get a distinct budget-exhausted error that clients treat as terminal. Giving each agent run its own small capped key stops one loop draining the team.
solid answer
~50 sWith reservations a cap is an admission rule: each call in flight already holds its maximum cost, so the gateway lets it finish and refuses the next. Cutting a stream saves at most the unspent part of a hold that already fits the cap, and it hands the user half an answer. The refusal must not look like provider throttling: it needs a distinct error type naming the budget and a retry hint set to the reset time, or the agent's retry logic hammers the gateway all weekend. If a gateway does cut a stream, the success status has already gone out, so it must end with an in-band error event and a truncated finish reason. The agent should checkpoint and report "stopped: budget". The real fix is a per-run or per-key cap under the team's, so a loop exhausts its own allowance rather than production's.
code
json · 10 lines{
"event": "error",
"data": {
"type": "budget_exhausted",
"scope": "team",
"message": "Monthly spend cap reached for this team",
"resets_at": "2026-11-01T00:00:00Z",
"retryable": false
}
}go deeper
Recall that a budget-exhausted refusal differs from provider throttling, and that a client should stop rather than retry it.
Explain why the cap applies at admission: in-flight calls already hold their reservation, so they finish and the next call is refused.
Show the stream case: the success status has already been sent, so a cut needs an in-band error event, and the agent must checkpoint and stop.
Argue for per-run and per-key caps under the team cap, and for overdrafts as explicit approved decisions rather than an automatic spill into a parent budget.
## The weekend A scenario: a team shares one gateway key, with a $2,000 monthly spend cap, between its production assistant and its experiments. On Friday an intern starts an **agent loop** against that key, a program that calls a model repeatedly and picks its next step and tool from each response. A bug keeps it from ever reaching its stop condition. It makes about three calls a minute, averaging $0.18 each as its context grows. Over 60 hours that would be 3 x 60 x 60 = 10,800 calls, about $1,944. The team had already spent part of its budget earlier in the month, so the cap is reached on Sunday night, the loop is refused, and on Monday morning so is the production assistant. Two questions follow: what should the gateway do at the moment the budget runs out, and what should every caller see? ## Refuse at admission; let admitted calls finish With a reservation scheme, every call in flight already holds its maximum cost against the budget, so it is already paid for within the cap. The clean rule is that **the cap governs admission**: calls already running finish, and the next call is refused. Cutting an admitted stream mid-response helps nobody: - the saving is at most the unspent part of holds that already fit the cap, and the provider has usually billed the tokens generated before the cut; - a user receives half an answer, and an agent receives a half-finished step it may misread; - the call's hold is released at settlement anyway. A gateway that does not reserve, and instead counts tokens as they stream, may genuinely have to cut a stream. That is a reason to reserve, not a reason to cut. ## What the caller sees | Situation | What the gateway sends | What the client should do | |---|---|---| | New call, budget exhausted | An error status and body naming the budget, its scope and its reset time | Stop; do not retry before the reset | | New streamed call, budget exhausted | The same error, before any stream starts | The same as above | | Stream cut mid-response (no reservation) | A final in-band error event, then close | Keep the partial text, mark it incomplete | | Budget nearly spent | Remaining-budget metadata on ordinary responses | Wrap up: summarise and checkpoint | The stream row has a trap. A streamed response sends its status line and headers, as success, before the first tokens go out. After that the status cannot change, so the only way to say why the stream ended is **in-band**: a final error event inside the stream and a finish reason that marks the answer as truncated. A client that checks only the status records a cut stream as a success. ## Not a throttle The refusal must not look like provider throttling. Client retry logic (covered with client-side backoff and concurrency) usually treats a throttling response as transient and tries again after a short backoff. If an exhausted budget arrives looking like that, a budget that resets in three weeks becomes an endless retry storm against the gateway. So: - give the error a **distinct type** in the body, such as `budget_exhausted`, naming which level ran out (key, team or organisation); - if the gateway reuses the throttling status, set its retry hint to the reset time rather than a few seconds; some gateways use a different status that clients already treat as terminal; - make client and agent retry policies key on the error type, not on the status alone. ## Mid-run: what the agent does An agent refused mid-run should: 1. stop issuing model calls for this run: no retry and no switching to another key; 2. persist its state so the run can resume once budget is available; 3. report a plain "stopped: budget reached" status to the person or job that launched it. Switching keys is the worst option: **key-hopping** defeats the cap and hides the overrun from whoever owns the budget. A deliberate overdraft, an owner approving a temporary raise, is fine; an automatic spill into a parent or neighbouring budget is not. ## Keeping one loop from draining the team The weekend was less a refusal problem than a scoping problem. The team cap did its job and stopped spend at $2,000, but it stopped the production assistant too. The fix is smaller caps underneath it: - a **separate key per environment or purpose**, so experiments cannot eat production's share; - **per-run caps** on agent runs and batch jobs, so one loop exhausts only its own small allowance; - a **daily cap** beside the monthly one, so a weekend runaway hits a wall on Saturday rather than on Monday. Watching spend trends so a person notices sooner is a telemetry concern; caps are what hold when nobody is watching.
- Why is returning a plain throttling response for an exhausted budget a mistake?Client retry logic usually treats throttling as transient and tries again after a short backoff, so a budget that resets in three weeks becomes an endless retry loop that floods the gateway and its logs. If the gateway reuses the throttling status, the body must carry a distinct budget error type and a retry hint set to the reset time; some gateways use a status their clients already treat as terminal.
- Should the gateway draw on the organisation's budget when a team's budget runs out?Not silently. An automatic spill into a parent or neighbouring budget defeats the team cap and hides the overrun from whoever owns it. A deliberate overdraft, such as an approved temporary raise or a soft line with a hard ceiling above it, is fine, but it should be an explicit decision recorded against the team rather than a fallback in the request path.
- What should an agent do when a step's model call is refused for budget?Treat it as terminal for the run: stop issuing calls, persist the run's state so it can resume, and give the person or job that launched it a clear "stopped: budget reached" status. It should not retry, switch to another key, or swallow the error and carry on looping, which is how a runaway loop keeps running.
- Kill every in-flight stream the instant the counter crosses the cap.
- Return the same throttling response the provider uses so existing clients cope.
- Once a stream has started, the gateway can still change its status to an error.
- When a team runs out, spill into the organisation budget so features keep working.
- A team-wide cap alone is enough to contain one runaway agent loop.