AI
AgentOps: validate tool idempotency before enabling production writes
A runbook for preventing repeated or concurrent agent calls from multiplying the effects of a production action.
An operations agent is approved to create a maintenance intervention for a service. The API response is slow, so the client retries while a workflow recovery submits the same request. Without a suitable contract, two interventions may be created when the user intended one. This fictional scenario also applies to MCP tools, internal APIs and agent-triggered automation.
The goal is not to promise end-to-end “exactly once” execution. It is to establish a stable intent, recognize repeats and block cases where the actual effect remains unknown.
Define the unit of work
Before enabling writes, document the business action, scope and expected effect: create an intervention for a given target and window, with at most one intervention for that intent. Identify the authorized caller, environment, canonical target, parameters that change the effect, approval and post-execution checks.
Assign a durable operation ID before the first send. A trusted component, not the model, creates or validates it; the ID remains unchanged across retries and recovery of the same intent. A legitimate new request gets a new ID even when its parameters are identical. Matching text, a conversation ID or a timestamp alone cannot reliably distinguish a repeat from a new intent.
Bind the ID to a fingerprint of normalized business parameters, scope and contract version. Reject and investigate a reused key with a different fingerprint; never silently treat it as a duplicate. The ID does not replace authentication, authorization or approval valid for the specific action and target.
Reserve atomically, then reconcile
The execution service keeps a durable ledger containing the key, fingerprint, state, target-side operation ID, redacted result and timeline. A uniqueness constraint or atomic conditional insert reserves the key; a read-then-create check alone can let two concurrent calls through. Partition keys by environment and tenant, restrict ledger access and retain records for at least the agreed retry, recovery and reconciliation window.
On receipt, the service first checks identity, authorization and the contract:
- New key: reserve atomically, then initiate the authorized action.
- Same key and fingerprint, confirmed result: return the recorded result after an access check, without executing again.
- Same key and fingerprint, operation in progress or outcome unknown: return a nonterminal state; do not start a second write.
- Same key with a different fingerprint: reject, log the conflict and require a new decision.
When the ledger and business effect share a transaction, both can commit together. For an external API, a local reservation does not prove the target was called only once: a crash can occur after acceptance but before the response is recorded. Pass a stable key to the target if it guarantees deduplication itself, or use a queryable operation ID to reconcile its state. Check the target guarantee’s scope and retention period. Without target-side deduplication or a reliable lookup, keep the outcome unknown and require human review; do not automatically expire the lock and repeat an uncertain write.
Make expiry and crash recovery explicit: an abandoned reservation is not proof that nothing happened. Preserve evidence and identifiers without placing secrets, sensitive arguments or unnecessary business data in traces.
Test the boundaries, not just the happy path
In an isolated environment, use an instrumented fake target and count effects actually created, not just HTTP responses. At minimum, test:
- Two concurrent calls with the same key and parameters: one reservation and at most one effect.
- The same key with a changed target or parameters: conflict and no additional effect.
- A response lost after target acceptance, followed by a retry and service restart: reconciliation before any new write.
- A crash before sending and a crash after sending but before persisting the result: distinguish proven absence from an unknown outcome.
- Recovery after the agreed expiry, revoked access or expired approval: no write without renewed valid authorization.
- Two distinct intents with identical content: two keys; verify business rules permit both effects.
Inject duplicates through the actual queue or workflow too. An intermediary’s duplicate detection may have a limited window and does not remove the consumer’s responsibility to protect its own effect. Compare identifiers end to end and verify that replaying a result cannot disclose data to another caller.
Canary, decision and rollback
Before the canary, name an owner, low-impact target, volume, observation window and stop thresholds. Start the new path in read-only or simulation mode, then allow one bounded write and inspect the ledger, target log and business state. A successful response alone is not sufficient evidence.
Expand only when concurrency, recovery and rejection tests pass and unknown states can be resolved. If duplicates appear, a conflict is unexplained or reconciliation is impossible, block new tool writes and return to the approved manual path. Preserve the ledger: reverting the code must not erase keys or make in-flight actions executable again. Inventory effects already produced and compensate only after business approval; a code rollback does not undo them.