01 / 05
How hard is a shared limit across agents?
The first version
Read how much is left, compare it with the amount, call the provider, then add the amount to a counter.
Where it breaks
Check, then act
Two agents read $100 left at the same moment. Both see room for an $80 refund and both dispatch. The counter ends at $160 against a $100 limit, and every single-agent test passed.
An allowance per session
A limit held in the agent's memory, or a counter keyed to one API key, starts again with every new session or key. Adding an agent adds an allowance.
Releasing on a timeout
The call times out, so the code releases the reservation. The refund did land. It no longer counts, and the next one goes through.
What has to hold
An atomic reservation before dispatch, in one store every agent shares, settled from the outcome and still held while the outcome is unknown.
With Anlyon
Every agent, session and key draws from the same limit, reserved before dispatch, and a new session or key does not get a new allowance. Scope a limit to users and each of your users gets their own counter. A limit counts money or a unit you declare, such as emails, and an unknown outcome keeps its allowance held.
// The version that passes every single-agent test.const { remaining } = await db.one('select remaining from limits where id = $1', [limitId]);if (amount > remaining) throw new Error('over the limit');await stripe.refunds.create({ payment_intent: pi, amount }); // two agents both get hereawait db.none('update limits set remaining = remaining - $1 where id = $2', [amount, limitId]);// The shape that holds: reserve atomically before dispatch, settle after.const claim = await db.oneOrNone( 'update limits set reserved = reserved + $1 where id = $2 and consumed + reserved + $1 <= amount returning id', [amount, limitId],);if (!claim) throw new Error('refused before dispatch');// Dispatch. On success, move the amount from reserved to consumed.// On a lost response, keep it reserved until you know what happened.