Transactions across a model call: the lock you hold is priced in inference latency.
A read-modify-write that is invisible in a web handler becomes your throughput ceiling the moment the "modify" is a model call, because the row lock is now held for p99 generation latency rather than for the microseconds your code takes. Lock-hold time multiplied by arrival rate is concurrency, so a twenty-second hold on a contended row caps that row at three writes a minute and exhausts the connection pool well before it caps anything else. The rule that follows is absolute and cheap: never hold a database transaction across a model call — read with a version, close the transaction, let the model decide, then write under a compare-and-swap — and treat a version conflict as a new turn rather than a retry of the old one.
Step 1The arithmetic nobody does, because in a web handler it never mattered.
Pessimistic locking is the default shape of correct code in every transactional system, and it is correct because of an assumption so reliable that it is never written down: the interval between acquiring a lock and committing is bounded by code you wrote. A handler reads a row, adds a number, writes it back. The hold is a few hundred microseconds, contention is theoretical, and nobody measures it.
An agent breaks the assumption in one line. Put a model call between the read and the write — because the whole point is that the model decides what the new value should be — and the hold time becomes the latency of a tool-using generation. Not the time-to-first-token your dashboard shows, but the full turn: prompt assembly, prefill over a long context, thinking tokens, the completion, and any tool round-trip the model makes before it answers. On a heavily tooled turn that is seconds; with a high thinking budget, or a retry after a 429, it is tens of seconds.
Little's law then does the rest, and the numbers are brutal because they are linear:
# concurrency = arrival rate x hold time hold = 20s # p99 of one tool-using turn, measured end to end rate = 3/s # agent turns that touch the same account row waiters = 20 * 3 = 60 # transactions queued on one row, steady state # and the ceiling on that row, regardless of how many workers you add: 1 / 20s = 3 writes per minute
Two consequences are worth separating, because teams usually discover the second one first and misdiagnose it as the first. The row-level ceiling is a correctness-preserving slowdown: writes serialise, nothing is lost, latency climbs. The pool is the one that actually pages you. Under a transaction-pooling proxy such as PgBouncer in transaction mode, a server connection is held for the duration of the transaction, so sixty in-flight agent turns pin sixty server connections — against a pool that is typically sized in the tens, because it was sized for handlers that hold a connection for a millisecond. The symptom is that unrelated queries start timing out while the database itself is almost idle, which is why this gets filed as a database incident when it is an agent design bug. Concurrency, queues and scaling is the general shape; this is the specific thing inside it that an agent does and a service does not.
Step 2The shipped defaults do not bound this, and the two timeouts that exist are owned by different teams.
The reflex is to assume a timeout catches it. Check the defaults before relying on that. PostgreSQL ships both idle_in_transaction_session_timeout and lock_timeout at 0, which means disabled — so a transaction that opens, stalls on an HTTPS call to a model provider, and never hears back holds its locks until the connection itself dies. MySQL's InnoDB is the opposite extreme and barely better: innodb_lock_wait_timeout defaults to 50 seconds, after which the waiting statement is rolled back. That turns a latency problem into a correctness problem under load, and it arrives as a rollback in the middle of a tool call rather than as a slow query in a dashboard.
Now put the model's own timeout beside those. A provider SDK's default request timeout is typically minutes, not seconds, precisely because long generations are legitimate. So the ordering you actually have, in a default deployment, is: the lock wait gives up first, or nothing gives up at all, and in neither case does the component that knows about the deadline tell the component holding the lock.
The database timeout and the inference timeout are two independent configurations, usually owned by two different teams, and neither is a bound on the other. There is no setting anywhere that says "do not let an LLM call happen inside a transaction." That constraint has to be written in your code, and the only reliable place is a check at the boundary — see STEP 6.
One more default is worth naming because it hides the problem in development: a connection pool with a generous timeout and a single developer produces zero contention, so the bug is invisible until the row is contended in production. This is the class of defect that load testing agents exists to find, and the specific test is concurrency on one entity, not throughput in aggregate.
Step 3Optimistic concurrency fits an agent better, and the reason is a reversal of two assumptions.
Pessimistic locking is the right choice when the decision is fast and conflicts are common. Optimistic concurrency is the right choice when the decision is slow and conflicts are rare. An agent inverts both inputs at once — the decision is the slowest thing in the system, and the conflict rate on any single entity is normally low — so the answer is not a tuning question.
The shape is three short interactions with the database instead of one long one, with the model outside all of them:
# 1. read, with the version, and COMMIT before you call the model row = db.query_one("SELECT id, balance, version FROM account WHERE id = %s", aid) db.commit() # transaction is over; no locks held # 2. the model decides, holding nothing plan = agent.decide(row) # seconds to tens of seconds # 3. write under a compare-and-swap on the version you read n = db.execute( "UPDATE account SET balance = %s, version = version + 1 " "WHERE id = %s AND version = %s", plan.new_balance, aid, row.version) if n == 0: raise StaleDecision(row.version) # somebody else wrote. NOT a retry.
Three details carry the pattern. The version column must be incremented by the same statement that writes, or the check is a time-of-check-to-time-of-use window rather than a guard. The zero-row result is the entire conflict signal, so the call site must inspect the affected-row count and not merely the absence of an exception — an UPDATE that matches nothing is a success as far as the driver is concerned. And the version must be read in the same statement as the data the model will see, otherwise you are comparing a version against a decision made from a different snapshot.
If your schema has no version column, updated_at works as a weaker one, and a hash of the fields the model was shown works as a stronger one: the strongest form of the check is "re-apply only if what I showed the model is still true", which is also the form that survives a schema where several writers touch different columns of the same row.
Step 4A version conflict is a new turn, not a retry — and this is where most implementations go wrong.
The reflex on a failed compare-and-swap is to loop: re-read, re-apply, retry. For a handler that adds a number, that is exactly right, because the operation is a pure function of the row and re-running it against fresh state produces the correct answer. For an agent it is wrong, and wrong in the direction that loses money quietly.
The model's output was conditioned on the row it read. If the balance changed between the read and the write, re-applying plan.new_balance computed against the old balance does not resolve the conflict — it overwrites a concurrent decision with a stale one, and it does so with a successful-looking UPDATE. The lost-update anomaly that optimistic concurrency exists to catch is reintroduced by the retry that was supposed to handle it.
So on conflict, go back to the model. Re-read the row, tell the agent what changed, and let it decide again. This is not free — it is a second full turn, with its own tokens and its own latency — and the honesty of that price is the point: the cost of contention on an agent write path is denominated in inference, which is roughly four orders of magnitude more expensive per occurrence than the retry a service would do. That is the number that should drive you to reduce contention rather than to absorb it.
This is a different problem from the one idempotency and retries solves, and you need both. An idempotency key answers "has this intent already been applied?" and prevents a duplicate. A version check answers "is this intent still valid?" and prevents a stale one. A write path with idempotency and no version check applies exactly one copy of the wrong decision; a write path with a version check and no idempotency applies the right decision an unknown number of times.
Bound the re-decision count — two is a defensible ceiling — and escalate rather than loop. A row under genuine contention will starve an agent that re-decides forever, and each lap costs a turn, so an unbounded loop is a retry amplification channel with an inference bill attached. After the second conflict the task is no longer about the row; it is about the fact that two writers want it, which is a human-in-the-loop decision or a queue, not a third attempt.
Step 5When the write genuinely must be atomic across rows, move the decision out — not the transaction in.
Sometimes a single compare-and-swap is not enough: the action debits one account and credits another, or it books a seat and charges a card, and the invariant spans rows. The wrong fix is to widen the transaction until it contains the model. Three patterns keep the transaction short and the model outside it, in increasing order of how much they cost to build.
- Propose, then apply deterministically. The model emits a structured plan — amounts as integers, account IDs, a stated expectation of current state — and a deterministic function validates it and applies every row in one short transaction, rejecting it outright if the stated expectation no longer holds. The transaction is now milliseconds of your own code again. This is the same boundary argued in the deterministic boundary for finance agents, with the worked implementation in deterministic finance workflow in code.
- Reserve, then confirm. Two short transactions with a reservation row and an expiry between them: the first claims the resource and records a deadline, the model deliberates, the second converts the reservation or lets it lapse. This is what every booking system already does, and it is the correct shape whenever a human might also be holding the thing the agent wants. The reaper that expires stale reservations is not optional — it is the component that makes the pattern safe.
- Serialise by entity in a queue. Key the work queue by the entity being written so exactly one worker ever holds a given account, and concurrency becomes a property of the queue rather than of the database. This gives up cross-entity atomicity, so it fits a write-heavy agent on many independent entities far better than one on a shared ledger. Parallel writes and silent merges is the multi-agent version of the same argument, and serving agent traffic has the queue mechanics.
Across all three, notice what the model stopped being. It stopped being a participant in a transaction and became a proposer of a change that a short, deterministic, auditable piece of code either applies or refuses. That is also the shape that gives you something to replay when a write turns out to be wrong — see repairing agent side effects, where the plan object is the artefact you need and the transaction log is not.
Step 6Operate it: one assertion, four numbers.
The assertion is what keeps this from regressing, because the defect is not visible in review — a model call three stack frames below a with transaction(): block looks like ordinary code. Set a flag when a transaction opens, check it in the client wrapper every agent uses to reach a model, and raise in development and test:
# the guard that makes the rule enforceable rather than aspirational def complete(messages, **kw): if tx_depth.get() > 0: raise RuntimeError("model call inside an open transaction") return provider.complete(messages, **kw) # in production, log-and-proceed is the safer rollout: you want the # inventory of violations before you start failing requests on them.
Then four numbers, and the fourth is the one most teams do not have:
- p99 transaction duration on agent write paths. Alarm above one second. This is a proxy for "did a model call get inside a transaction", and it catches the regression the assertion missed in a path nobody tested.
- Conflicts per hundred writes. The contention rate, and the input to whether STEP 5's queue is worth building. Rising conflicts with flat traffic usually means two agents were given overlapping scopes.
- Re-decisions per completed task. Inference spent on contention rather than on work. A number above about 0.1 says the entity partition is wrong.
- Connection-pool wait time, segmented by caller. The number that would have identified the incident in STEP 1 in minutes rather than hours, and the one that is usually aggregated across the whole service so the agent's contribution is invisible. Segment it.
Do it in this order. Grep for every model call that is lexically or dynamically inside a transaction scope and fix those paths first — they are already capping throughput on whatever row they touch. Add the version column and the affected-row-count check to every agent write path, and make the zero-row case raise rather than pass. Then wire the conflict handler to go back to the model with the new state, cap it at two re-decisions, and escalate on the third. Add the open-transaction assertion to the model client in log-and-proceed mode, read the inventory it produces for a week, then flip it to raise in CI. Segment pool wait time by caller before you need it. And if you keep one sentence: the version check is what makes the write correct, and going back to the model instead of retrying the write is what makes the version check mean anything.