DEV Community

Cover image for Your AI agent can overshoot its budget.
Dhrubojyoti Gangopadhyay
Dhrubojyoti Gangopadhyay

Posted on

Your AI agent can overshoot its budget.

On 23 November 2026, OpenAI Node SDK v7.63.0 added spending limits for agent sessions. In the Agents API session guide, a positive spend_control.limit is expressed in US cents. The guide describes the limit as best effort. Readings can lag, in-flight work can accrue costs, and work can continue when a spend check is unavailable.

Use the provider limit as a second layer. For expenses your application controls, make the decision before dispatch and keep a record of money already committed.

Reserve against the same balance

Here is an illustrative race. The shared allowance has 60¢ free and two workers each request 40¢. If both read the balance before either updates it, each sees enough room. Together they commit 80¢.

A database transaction can serialize the decision. Below is the balance update for one operation with a positive amount in cents.

UPDATE job_budgets
SET reserved_cents = reserved_cents + :amount
WHERE job_id = :job_id
  AND limit_cents - spent_cents - reserved_cents >= :amount
RETURNING job_id;
Enter fullscreen mode Exit fullscreen mode

In the same transaction, create a reservation keyed by a unique operation ID and link it to the job and amount. An existing ID returns the existing decision after checking that the job and amount match. If the balance update returns no row, deny the reservation and skip dispatch. Commit the reservation before any paid call.

Reconcile the call after dispatch

Have one worker claim the operation for dispatch. Pass its ID to the provider as an idempotency key where supported. Store the provider's operation ID when you receive it.

If the call times out, keep the reservation open while you establish the outcome. A lost response can leave a paid job running. With provider idempotency, reuse the same key when a retry is safe under the provider's contract. If you cannot establish the outcome and lack idempotency, route the operation for reconciliation.

Once the charge is final, move the actual cost into settled spend and remove the reservation in one transaction. Make settlement idempotent too so a repeated callback cannot debit the budget twice.

Test the boundary

Run two workers against an allowance that can cover exactly one reservation. Confirm exactly one paid call leaves the application. Repeat with a lost response after the provider accepts the call. Confirm the reservation stays open and a retry cannot start a second logical operation.

This control depends on a defensible maximum for each dispatched operation and on routing every paid call through it. An estimate alone cannot guarantee a ceiling. For a job whose cost cannot be bounded, split the work into bounded steps or escalate before dispatch.

Applied AI, Day 39. Tomorrow's check covers what an agent should record before handing unfinished work to a human.

Top comments (2)

Collapse
 
robertpaschal profile image
Odinaka Robert Nnamani •

Reserving against the same balance the vendor checks is the detail that makes this work. A limit the agent can outrun is just a number on a dashboard.

Collapse
 
dhrubojyoti profile image
Dhrubojyoti Gangopadhyay •

Exactly right — if the reservation ledger sits outside the dispatch transaction, parallel workers still race the limit before the meter updates. Making reserved_cents part of the same conditional UPDATE is what turns a soft ceiling into an actual gate. Appreciate you reading!