A tool that checks and reserves in one call reserves again on every retry, duplicate and planning check. Split it into a query and a command keyed by request.
An old rule meets a new caller
Command-query separation is one of the oldest rules in object design. Bertrand Meyer stated it for methods: a routine either answers a question or changes state, never both. Asking a question should not change the answer. Most service APIs drift away from it over time. An endpoint called check_and_reserve saves a round trip. get_or_create saves a race. A "lookup" that also records the customer's interest feeds a dashboard. Human-driven screens already need duplicate-submission and retry protection; an agent adds planning calls and parallel calls to those existing risks.
An agent is a different caller. It calls tools while it is still deciding. It may call the same tool twice in one turn when the model emits parallel calls. The tool host retries after a client-side timeout without knowing whether the first call landed. Some hosts cache tool results to save tokens and latency. Those behaviours do not duplicate business effects for a pure query. A state-changing tool needs an explicit repeat-call contract; otherwise each invocation can add another effect. This guide shows the failure with a small, replayable example and then lays out how to split existing tools without rewriting the service behind them.
The running example is the holds desk at Belgavi's Book Store. A customer asks to hold a copy of a popular title, and an agent handles the request. The existing API has one endpoint, check_and_reserve(isbn), which returns availability and reserves a copy if one is free. The shelf has 3 copies. The companion example query_command_tools replays real tool-call shapes against that endpoint and against a split design. Every number below comes from it.
Worked example: one hold request, many calls
Geetha asks for a hold on request REQ-501. A few minutes later Jenya asks for the same title on REQ-502.
| Tool-call sequence for one hold | Mixed tool: copies reserved | Split tools: copies reserved |
|---|---|---|
| One call | 1 | 1 |
| Client timeout, then a retry | 2 | 1 |
| Model emits the call twice in parallel | 2 | 1 |
| Two availability checks while planning, then the decision | 3 | 1 |
| Host caches the planning check, then the decision | 2 | 1 |
| A busy turn for Geetha, then Jenya asks | Geetha 3, Jenya 0 | Geetha 1, Jenya 1 |
With the mixed tool, every call the agent makes for any reason takes a copy off the shelf. A timeout retry holds two copies for one customer. Asking "is it available?" twice while planning, which is exactly what a careful agent does, holds all three copies before the agent has even decided. The cache row is the subtle one. The host serves the second planning check from its cache, so it looks as if caching protected the shelf, but the first check had already reserved a copy and the final decision reserves another. In the busy-turn row, Jenya finds an empty shelf although only one hold was ever intended.
The split design has two tools. get_availability(isbn) is a pure query. Retries and parallel reads do not reserve extra copies. Caching still needs a freshness limit and a key scoped to the caller's access. reserve_copy(request_id, isbn) is a command keyed by the customer's request, not by the tool call. A retry or duplicate carries the same request id and replays the first outcome. Geetha gets one hold and Jenya gets the next.
Designing the two halves
The query half returns facts and a version, never side effects.
def get_availability(isbn: str) -> dict:
row = stock.read(isbn) # no write, no audit record, no interest signal
return {"isbn": isbn, "available": row.copies, "version": row.version}The command half takes a key that identifies the customer's intent, records the first outcome under that key and replays it on repeats. The following framework-neutral sketch assumes the database helper serializes operations for each request key; it is not a complete database implementation.
def reserve_copy(request_id: str, isbn: str, customer: str) -> dict:
with db.transaction():
prior = holds.find_by_request(request_id)
if prior is not None:
return {"reserved": prior.reserved, "replayed": True}
if not stock.decrement_if_positive(isbn):
holds.record(request_id, isbn, customer, reserved=False)
return {"reserved": False, "replayed": False}
holds.record(request_id, isbn, customer, reserved=True)
return {"reserved": True, "replayed": False}The real database must enforce a unique request key and serialize competing claims before changing stock, or roll back a losing transaction completely. Merely checking for a prior row inside a default-isolation transaction does not prevent two callers seeing no row. Bind the stored key to the validated arguments and reject conflicting reuse. The request id comes from the conversation or case, so the model cannot invent a fresh key on each attempt. And the outcome is recorded even when no copy was free, so a retry after "sold out" does not reserve a copy that came back a second later.
What stays deterministic
The agent decides whether to place a hold. Everything about how a hold is made stays in code: the key, the transaction, the replay rule, and the stock check. The tool description should say plainly which tools are safe to repeat, and the host should treat that as configuration it owns, not as a hint the server may set. MCP tool annotations such as readOnlyHint and idempotentHint describe this property, but the specification says clients "MUST consider tool annotations to be untrusted unless they come from trusted servers". The host's own allowlist decides which tools it retries or caches.
What the model should read in the tool descriptions
Descriptions are part of the contract, because the model plans with them. The query's description should say that it changes nothing and may be called as often as needed. The command's description should say that it places a hold, that request_id must be the case's request id, and that calling it again with the same id returns the first result instead of placing a second hold. Avoid words like "check" in a command's name or description. A model that wants to know whether a copy is free will reach for anything named like a check, which is how the planning-check row in the table happens.
Migrating an existing mixed endpoint
- Inventory side effects. For each tool an agent can call, list every write it performs, including audit rows, counters and emails. A "read" that writes an interest record is a mixed tool.
- Add the query first. Expose a pure read with the fields the agent needs. Point the agent's planning prompts at it. This alone removes the planning-check row from the table.
- Wrap the command with a request key. If the service cannot change yet, an adapter can store request ids and outcomes, but a network call and a local record are not one transaction. An ambiguous timeout needs downstream idempotency or a status lookup and reconciliation. Without either, the adapter cannot promise exactly one effect.
- Retire the mixed tool from the agent's tool list. Human screens can keep using the old endpoint. The agent should not see it.
- Turn on host retries and caching only for the query. Configure the tool host to retry and cache by tool name, from an allowlist you maintain.
Failure modes after the split
A key that changes on retry. If the host generates the request id per call, the split design reserves twice like the mixed one. Derive the key from the case or conversation and pass it through.
Stale reads used as decisions. A cached availability answer can be out of date. That is acceptable for planning, because the command re-checks stock inside its transaction and returns "not reserved" when the shelf is empty. The agent must read the command's result, not assume the query's answer still holds.
Replays across different arguments. If REQ-501 is replayed with a different ISBN, return an error instead of the old outcome. A key bound to one intent must not silently answer a different one.
Hidden side effects that remain. Logging every query is fine. Sending a "your book is waiting" email from a query is not. Re-run the inventory whenever a tool changes.
When the split is unnecessary
If a tool is only ever called by a deterministic workflow that calls it exactly once, the mixed endpoint may be fine. If the side effect is naturally idempotent, such as setting a flag to a fixed value, retries and duplicates are harmless. The split earns its cost when a model decides how often and when to call, which is the usual case for agents.
How to verify it
Write tests that replay tool-call sequences, not just single calls: a retry after a timeout, a duplicate in the same turn, planning checks before a decision, and a cached read. Assert the number of state changes, not the number of calls. The companion example runs six such sequences against both designs. In production, count commands per request id. More than one applied change for the same id means a key is being regenerated somewhere between the model and the service.