Designing Idempotent Side-Effect Contracts for AI Agents
A timed-out agent tool may already have changed the world. Design explicit side-effect contracts, idempotency, reconciliation, and safe recovery.
When an AI agent uses a payment provider, it sends a refund request. If the response is lost during a network timeout, the runtime retries the request. The agent's trace shows that both refund attempts succeed, but the customer ends up with two refunds. This example shows a common issue in distributed systems - how to distinguish an unsuccessful response from an unsuccessful effect.
To address this problem, the runtime needs a tool contract that clearly defines the behavior of a tool. The contract specifies whether the tool is read-only, an idempotent write, a deduplicated write, or a non-repeatable write. This distinction matters when an agent can change the world, such as sending a message, creating a ticket, issuing a credit, booking a trip, deleting a record, or triggering a deployment.
Developers often describe a tool with an input and output schema, but this schema only validates the shape of the data, not the behavior. The tool contract should focus on the intended business effect, not just the response status code. The Model Context Protocol includes tool annotations like readOnlyHint, destructiveHint, and idempotentHint, but these are just untrusted hints, not security or correctness guarantees.
A runtime still needs to enforce its own policy about retry behavior. The key is to define the side-effect contract as part of the tool definition, rather than scattering it across prompts and catch blocks. By doing this, we can create a more reliable and predictable tool behavior.
Written by urgent.news from HackerNoon's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.