Two workers, one balance, and a surprisingly expensive gap between checking and spending.

An AI agent sends two six-dollar requests through one budget gate under a ten-dollar limit.

Give an AI agent a $10 budget and it sounds like a very small system design problem. Keep a counter, subtract each charge, and stop when the money runs out.

Then let the agent do two things at once.

Both workers see $10 available. Both decide that a $6 operation is affordable. Both send the request, and now your beautifully documented budget has become a $12 bill.

Nobody had to ignore an instruction. Each worker made a reasonable decision using information that stopped being safe the moment another worker used it too.

This is the kind of interview exercise I like because you can feel the bug before anybody names a database or draws six services. Let’s build a budget that actually participates in the decision to spend.

We’ll use an original practice scenario, not a reported company question. The dollar amounts are deliberately simple examples, not real model prices.

Start by defining what the $10 covers

For this exercise, give one agent run a fixed budget of $10. Every paid model or tool call must pass through a service we control. Workers cannot call those providers directly with their own credentials.

Also assume we can put a trustworthy maximum charge on each authorized operation. We’ll return to that assumption because it is doing quite a lot of work.

Three amounts matter.

  • Spent is the cost we have confirmed and settled.
  • Reserved is money committed to operations whose final costs are not settled yet.
  • Available is the budget minus both spent and reserved.

The rule is that spent plus reserved must never exceed the budget. Counting only completed calls leaves every in-flight call spending money that still looks available.

Two workers independently read ten dollars and spend six each, exceeding the shared budget by two dollars.

A notification after the balance crosses $10 might help someone investigate. It cannot stop a request that has already been accepted elsewhere. The admission decision needs to happen before dispatch.

Reserve the money before asking the provider

Worker A requests a $6 reservation. The budget service checks the available amount and creates that reservation as one atomic operation.

After it commits, the ledger shows $0 spent, $6 reserved, and $4 available. Worker B’s $6 request now fails the check. It can wait, choose a cheaper permitted operation, or return the useful work already completed.

Please don’t make the fallback another unbounded loop that keeps asking for money. We have enough problems already.

An atomic six-dollar reservation leaves four dollars available and blocks another six-dollar request.

One implementation can use a database transaction that locks the run’s budget row, checks for an existing operation ID, verifies available funds, and creates the reservation while updating the reserved total. Every writer must follow the same locking protocol. Commit before making the network call so a slow provider doesn’t hold the database lock.

Store a unique operation ID within the run, plus the requested maximum, input fingerprint, state, and provider reference when available. If the caller repeats the same operation after losing our response, return its existing reservation. Reject reuse of the same ID with different inputs.

For a useful companion exercise, try the reserved-transfer stage of this Airbnb-tagged banking ledger question. Trace the available balance before and after a transfer is accepted, then explain which money is merely held. It is a reported interview exercise; our AI-budget example here is separate.

That distinction between held and settled money is the foundation. Now we need to handle the part that happens outside our database.

A timeout does not give the money back

Suppose A reserved $6, sent the request, and then lost its connection.

We know our client stopped waiting. We don’t yet know whether the provider finished the operation, charged for part of it, or did nothing at all.

AWS’s discussion of timeouts and retries makes this distinction explicit. Failure to receive a response does not prove that a side effect never happened.

So the reservation stays in an unresolved state while we reconcile it. Query the provider using its operation reference when supported, or wait for authoritative usage information. An age alert can flag a stuck reservation for investigation, but age alone is not evidence that releasing it is safe.

Settle a confirmed charge, release confirmed unused funds, and keep an unknown outcome reserved.

If the confirmed final charge is $4, settle the reservation atomically by moving $4 into spent and removing the $6 hold. Available funds become $6. If we can establish that no charge happened and no further dispatch can occur, release the entire hold.

A repeated settlement notification must not perform that transition twice. Match it to the operation, verify the state and amount, and return the recorded outcome if it has already been applied.

Notice the trade-off. Keeping uncertain funds reserved can temporarily stop useful work. Releasing them optimistically can break the budget. For a hard spending cap, I prefer making that interruption visible rather than quietly turning uncertainty into free money.

One reservation must not authorize two paid calls

An internal operation ID prevents duplicate reservations. It does not automatically stop two network requests from creating two charges.

Use the provider’s idempotency mechanism when it supports the required operation and guarantees. A retry must preserve the same provider key and request meaning, within that mechanism’s documented limits.

Without that protection, a worker that crashes around dispatch leaves an awkward uncertainty. Don’t blindly send the request again under the old reservation. Resolve the original outcome, or treat a further potentially billable attempt as additional exposure that needs its own budget authorization.

The same warning applies to expiring worker leases. An old worker may still be running after another worker takes over. Your dispatcher needs to prevent stale attempts from sending new paid requests, and it must not equate a lost lease with a refunded provider charge.

For an interview, describe this boundary clearly. A database transaction protects your ledger. It cannot make an unrelated provider join that transaction.

The maximum charge has to mean something

So far, $6 has represented a real upper bound, not our best guess.

For a model call, that bound might require a known input size, an enforced output limit, a recorded pricing version, and allowances for every separately billable feature. For a tool, it might require an explicit maximum quantity or a fixed-price operation. Nested calls and automatic SDK retries must be included or separately authorized.

Use exact monetary arithmetic with units fine enough for the provider’s prices. Integer cents are convenient for our drawings, but actual usage may need micro-units or decimal arithmetic rather than rounding each small charge to cents.

If a provider cannot bound the charge, our local reservation cannot honestly guarantee an exact invoice ceiling. We can refuse that operation under a hard-cap policy, obtain an enforceable provider-side limit, or offer a clearly described soft budget instead.

That is a much stronger answer than quietly replacing maximum cost with average cost and hoping nobody notices.

Test the awkward cases before adding more architecture

Start with a tiny ledger model and a fake provider you can pause at chosen points. Real requests cost money and make failures harder to reproduce, so keep this practice local.

Four tests cover concurrent reservations, duplicate operations, duplicate settlements and uncertain provider outcomes.

Run two $6 reservation requests against a $10 budget at the same time. Exactly one should succeed, and the total exposure should remain $6.

Then repeat that successful operation ID. The ledger should return the original reservation without holding another $6. Repeat its settlement too, and verify that spent increases only once.

Finally, simulate a timeout after dispatch. Confirm that the hold remains, another unaffordable operation is rejected, and a later confirmed $4 charge produces $4 spent, $0 reserved, and $6 available.

Add a crash just before dispatch as a separate case. Releasing those funds is safe only after recovery can establish that the operation was never sent and cannot still be sent by a stale worker.

These checks are small enough to explain on a whiteboard, but they force you to connect concurrency, retries, and accounting in one answer. If you implement them, inspect both the ledger state and the fake provider’s call count; a tidy balance can hide duplicate external work.

I find this more useful than debating whether the agent is clever enough to respect a budget. The model can propose an action, but the system decides whether that action is authorized.

Before your next design practice session, take one tool the agent can call and follow a single dollar through reservation, dispatch, uncertainty, and settlement. If you can explain where that dollar lives at every step, your diagram is doing something much more valuable than looking complete.

~ Emily