An agent that reads several tools sees records at different moments. Compare composite reads, snapshot tokens, re-reads and version checks on the write.
One plan, several reads, several moments
A request handler can read related records from one database snapshot, but a transaction alone does not guarantee that. PostgreSQL Read Committed takes a new snapshot for each statement. A single joined query, or a transaction using an appropriate repeatable-read snapshot, can keep the order, refund ledger and shipment consistent across reads. An agent does not work that way. It calls a tool, reads the answer, decides which tool to call next, and so on. Each tool call is its own request, often to a different service, and each one sees the data as it is at that moment. Between the first read and the last, other parts of the business keep committing changes.
When one of those changes touches several of the records the agent is reading, the agent can see the order before the change and the ledger after it. Database people call this read skew. No single read is wrong, yet the combination never existed. A plan built on it can be wrong in ways that no check on a single record will catch.
The running example is the returns desk at Belgavi's Book Store. To decide a refund, the agent reads three things: the order, the refund ledger and the return shipment. Finance posts partial refunds in one transaction that updates the ledger and sets the order status to partially refunded. If that transaction commits between the agent's order read and its ledger read, the agent sees an order that is still fully paid and a ledger that already shows the refund. It may then refund the remaining amount twice, or deny a valid return because the totals do not add up.
Worked example: two hundred cases on the returns desk
The companion lab and the example agent_read_skew_snapshot model this with synthetic data. Each case reads three tools 800 milliseconds apart, so the reads span 1,600 milliseconds. The agent then plans for 4 seconds before it writes. Cross-record transactions commit about 6 times a minute. The figures below are outputs of that model, not measurements of any production system.
| Design | Plans on mixed versions | Stale writes applied | Writes rejected by a version check | Tool reads |
|---|---|---|---|---|
| Separate reads, no checks | 16 | 40 | 0 | 600 |
| Separate reads, version check on the write | 16 | 0 | 56 | 600 |
| Validate versions, re-read up to twice | 0 | 41 | 0 | 651 |
| One snapshot for every read | 0 | 56 | 0 | 600 |
| One snapshot, version check on the write | 0 | 0 | 56 | 600 |
Two problems are visible here, and they need different fixes. Mixed versions come from commits that land inside the read window. Stale writes come from commits that land between the moment the plan's data was read and the moment the agent writes. A snapshot removes the first problem completely and makes the second slightly worse, because the plan is based on the oldest read. The lab uses an atomic case-level version check against the first read, covering every record the plan depends on. That rejects all 56 changed cases, including the 16 mixed views, before a write. It cannot stop the model reasoning over a mixed view earlier in the loop. Checking only the last record read would be weaker and is not the guarantee modeled here. The mixed-view column records final read sets, including ones later rejected; outcome columns must not be summed with it.
Both the read-window length and the commit rate matter. With five tools 3 seconds apart, the model reports 120 of 200 plans built on mixed versions. Slow tools and long chains of reads are where read skew stops being rare.
Three ways to give the agent one consistent view
The first option is a composite read. Replace three tools with one that reads the order, ledger and shipment with one joined query, or multiple queries in a repeatable-read transaction, and returns them together. This is the simplest fix when the records live in one database. It also shortens the read window to one round trip and cuts the token cost of three calls.
The second option is a snapshot token. The first read returns a token naming the version of the data it saw, for example a commit sequence number from an event log or a projection version. Later tools accept the token and answer as of that version. This needs a store that can serve history: an event-sourced read model, temporal tables or a versioned key-value store.
from dataclasses import dataclass
@dataclass(frozen=True)
class CaseView:
as_of: int # commit sequence the whole view reflects
order: dict
ledger: dict
shipment: dict
def read_case(store, case_id: str, as_of: int | None = None) -> CaseView:
"""Every part of the view comes from one commit position."""
seq = store.current_sequence() if as_of is None else as_of
return CaseView(
as_of=seq,
order=store.order_at(case_id, seq),
ledger=store.ledger_at(case_id, seq),
shipment=store.shipment_at(case_id, seq),
)
def issue_refund(store, case_id: str, amount: int, based_on: int) -> str:
"""Reject the write if anything this case depends on changed after the plan's view."""
# Adapter contract: version comparison and append happen atomically.
# A separate last_change() read followed by append_refund() has a race.
applied = store.append_refund_if_unchanged(case_id, amount, based_on)
return "refund recorded" if applied else "rejected: case changed; re-plan"The third option is to validate and re-read. Each tool response carries the version of the record it returned. Before planning, the agent's harness checks that the versions are consistent, for example that every record reflects the same last transaction ID. If not, it reads again, up to a budget. Unrelated per-record counters and ordinary ETags cannot be compared numerically across services. This needs a shared commit position or an atomic validation of the full read set; it costs extra reads. Under heavy write traffic it can fail to converge. The example store methods above specify adapter requirements, not a supplied database API. Implement the conditional append with a transaction or compare-and-swap, never a check followed by an unguarded write. In the busy preset of the lab, with 60 commits a minute, 151 of 200 cases exhaust two re-reads and go to a person.
Whatever you choose for reads, put a conditional check on the write. The write tool receives the version the plan was based on and refuses to act if the case moved after it. This is ordinary optimistic concurrency, and it is the only one of these measures that catches staleness.
What stays deterministic
The harness, not the model, decides consistency. The model should never be asked to notice that two tool results disagree in time; it will sometimes explain the gap away. The version comparison, the re-read loop and its budget, the snapshot token passing and the conditional write are ordinary code. They run in the tool layer or in the workflow that calls the agent, and their behaviour is the same whichever model is plugged in. The model's job is the decision: whether the return qualifies and how much to refund, given a view the harness guarantees is coherent.
Migration steps for an existing service
- List every agent plan that reads more than one record before it writes, and note which records a single business transaction updates together.
- Where those records share a database, add a composite read tool and retire the separate tools for that plan.
- Where they do not, add a version field to each tool response if one is missing, and log the versions the plan used.
- Add a conditional write: the write tool takes the version the plan used and returns a typed rejection when the case has moved.
- Teach the workflow to treat that rejection as re-plan, not as an error to retry blindly, and to send repeated rejections to a person.
- Measure mixed-version plans and rejections per day before and after, using the logged versions.
Failure modes to watch
- A composite read that calls three services in sequence is still three reads. It only helps when the reads happen inside one transaction or at one commit position.
- Snapshot tokens that outlive the retention of the history store produce errors on long cases. Bound the age of a token and re-read from the start when it expires.
- A version check on the wrong record. Checking the order version does not protect a decision that also depended on the ledger. Check every record the plan relied on, or check one case-level version that every relevant transaction increments.
- Re-read loops that never end under load. Keep the budget small and make exhaustion a visible outcome.
- Caches in front of tools. A cached shipment record can be older than the order read seconds later; include cache age in the version check or bypass the cache for planning reads.
When an agent is unnecessary
If the refund decision is a formula over the three records, write the formula in the service and run it in one transaction. Read skew is a cost you pay for letting a model choose which reads to make and in what order. Pay it where that choice adds value, such as unusual returns that need judgement, and keep the routine decisions in code that reads one snapshot.
How to verify it
Inject commits between tool calls in a test environment and assert the outcome: a composite read returns one version for all parts, a snapshot token returns the same data on repeated reads, and the write tool rejects a plan whose basis is older than the case. Count, in production logs, how often the versions in one plan differ. The lab lets you vary the commit rate, the number of tools, the gap between calls and the planning time to see which of these numbers moves when.
Interactive experiment
Agent read skew: tool reads across concurrent writes. Synthetic browser-local fixtures, adjustable inputs, decision traces and JSON export.