DEV Community

Anatolii Maltsev
Anatolii Maltsev

Posted on Originally published at nullrun.io

Agent Budget is a Permission not a Limit

A budget that reports at the end of the period is accounting.
A permission constrains the future, before the call.

The concurrency failure is the whole story.
5 workers check whether $4000 is under a $5000 cap, each sees yes, each spends $1200. Every individual check was correct.

Reserve in-flight calls instead:

def authorize(cost_cents, period):
    spent = counters.spent(period)
    held  = reservations.outstanding(period)
    if spent + held + cost_cents > CAP:
        return Decision("block")
    reservations.create(cost_cents, period, ttl=300)
    return Decision("allow")
Enter fullscreen mode Exit fullscreen mode

Two settlement skews to design for up front: actual cost differing from estimate, and the settlement write failing outright.
Both are ordinary.

Full writeup with the field-classification problem: here

Top comments (0)