← sources

Why a simple budget check lets parallel agent calls blow past the cap

source: reddit (reddit:AI_Agents)published 2026-09-28 08:55ingested 2026-09-29 02:36

retained text

If you put a dollar cap on an agent job, the first version usually looks like this:

    spent = db.get_spent(job_id)
    if spent + estimate <= ceiling:
        result = call_model()        # takes a few seconds
        db.add_spent(job_id, actual)

With one call at a time it works. Now run 5 tool calls in parallel on a job at $4.90 of a $5.00 cap. All 5 read $4.90, all 5 see room for a $0.10 call, all 5 go out. You spend $5.40 on a $5.00 cap, and the check said yes every time.

The fix is to reserve instead of read. One conditional update that only succeeds if there's room, counting what's already in flight:

    UPDATE task_budgets
    SET reserved = reserved + :estimate
    WHERE job = :job
      AND used + reserved + :estimate <= ceiling
    RETURNING ...

No row back means no room, so the call doesn't go out. After the call you settle: move the actual cost from reserved to used and release the rest.

Three things bit me after that:

1. A worker that crashes never settles, so its reservation holds budget forever. Reservations need an expiry and a sweeper that reclaims them.
2. A late settle after the sweeper already reclaimed the reservation must not subtract a second time. Decrement by what was actually still held, not by what you asked for.
3. Retries. If the reserve or the record isn't idempotent, a retried request reserves or counts twice.

A quick test for any cap you build: fire 40 calls at once at a $1.00 job with a $0.10 estimate. Exactly 10 should pass, every time.

Disclosure: I build AgentBill, which does this as a hosted preflight check, so I ran into all three of these. Happy to go into the edge cases in the comments.

document fetched 2026-09-29 00:19 · canonical https://reddit.com/r/AI_Agents/comments/1ws9f81/why_a_simple_budget_check_lets_parallel_agent