← All guides

Agents

Make agent actions safe under retries, races, and partial failure

Bind approval to a concrete action, enforce authorization at execution, make retries idempotent, and recover multi-step workflows without inventing an exactly-once guarantee.

agentsidempotencyapprovalstransactionstool security
Bind approved intent, execute with an idempotency record and reconcile the actual side effects.
These are application design controls. Plan retries, changed arguments and partial completion before allowing write actions.View full-size diagram

An agent submits a change, the destination commits it, and the network drops the response. The model sees an error and tries again. Without an execution contract, one approved intent can become two external actions. This guide applies established distributed-system patterns to agent tool execution. The approval binding, state machine, and tests below are application design recommendations, not automatic guarantees supplied by MCP, a model, or an agent framework.

Define the action outside the conversation

Represent a proposed action as a structured record containing the tenant, initiating principal, target resource, operation, canonical arguments, expected resource version, authorization context, expiry, and application-generated operation ID. The model may propose fields, but trusted application logic resolves identities and validates allowed values.

Before asking for approval, show the concrete consequence: which object changes, which recipient receives data, and what permissions or value are involved. Bind the recorded approval to a digest of that canonical proposal plus its authorization context. If the target, payload, recipient, or privilege changes, require a new decision. Do not treat “yes” in an unrelated conversation message as authorization for an arbitrary later tool call.

A digest protects the association between approval and proposal; it does not prove that the proposal is safe. Apply business validation, scope checks, and required independent review before producing it. Keep sensitive parameters in a restricted action store rather than exposing them in general trace metadata.

Recheck authority at execution time

Approval can outlive a user’s permissions, the target resource, or the tool configuration. At execution, validate the approval’s expiry, exact proposal binding, current principal authority, current resource version, and selected tool implementation. Resolve destinations through the approved adapter; do not let a tool response redirect the next privileged action to a new recipient.

Our recommended workflow separates proposed, authorized, executing, committed, failed, unknown-outcome, and compensating states. Unknown outcome is essential: a timeout after submission does not establish failure. Only trusted destination evidence should move an action into committed or definitely failed. Store state durably and use conditional transitions so concurrent workers cannot both claim a fresh operation.

Make duplicate delivery an expected case

Microsoft’s Retry pattern warns that an operation may have succeeded even when its response was lost. Retries need to account for idempotency, the kind of error, cumulative delay, and nested retry mechanisms. Retrying every failure at every layer can amplify load and duplicate side effects. Retry semantics.

For an operation the application owns, use a durable idempotency record keyed by tenant and operation ID, with a payload digest. Reusing the key with different arguments must fail. Atomically store the result with the business change where the data system permits it. Concurrent identical requests should receive the same result or a clear in-progress status, not create independent executions.

For an external API, use its documented idempotency or operation-status mechanism. If neither exists, a local “sent” flag cannot guarantee exactly-once execution across a crash. Place ambiguous outcomes into reconciliation and inspect the destination before retrying. Define key retention long enough to cover the real replay window; expiry of the local record must not turn a delayed duplicate into a new action unnoticed.

Situation Recommended decision
Validation or authorization denied Stop; repeated attempts do not repair the request
Documented transient failure before acceptance Retry within a bounded policy
Timeout after submission Query destination status or reconcile before repeating
Same key and same canonical payload Return the recorded outcome or in-progress state
Same key with a changed payload Reject the conflict
Approval expired or resource version changed Re-evaluate and obtain a new decision where required

Design compensation before adding more steps

An agent workflow may create a resource, grant access, send a notification, and update an inventory. These operations do not form one atomic transaction across providers. Classify each as reversible, compensable with business consequences, or irreversible. Schedule irreversible steps only after required validations and approvals are complete.

The Compensating Transaction pattern requires recording enough state to recover completed work, handling concurrent changes, and making compensation itself resilient. Compensation can fail and may need human intervention; it does not necessarily restore the original world or run in exact reverse order. Compensation limits.

Our recommended incident record links each forward action to its compensating action and resulting state. Revoke an unintended grant using its actual assignment identifier rather than issuing a broad “remove all access” operation. A sent message cannot reliably be unsent, so treat correction and notification as business recovery, not a technical rollback promise.

Verify security and reliability together

Run these tests against isolated accounts and disposable resources. Inspect committed destination state, not only the assistant’s final answer.

Test Required outcome
Two workers execute the same approved action One intended effect with a consistent recorded result
Destination commits but response is lost Reconciliation identifies success without a duplicate effect
Worker crashes between submission and record update Recovery follows the unknown-outcome procedure
Model changes an approved recipient Proposal binding fails before execution
Permission revoked after approval Current authorization check denies the action
Target changes concurrently Version conflict is handled without overwriting unrelated work
Compensation fails halfway Progress persists and an operator receives actionable evidence

Track operation IDs, approval references, authorization decisions, destination IDs, retry counts, and reconciliation outcomes. Keep credentials and full payloads out of routine telemetry. Combine this execution contract with tool trust boundaries and adversarial evaluation so a robust retry loop never becomes a reliable way to repeat an unauthorized action.

Go to the source

References & further reading

  1. Microsoft Learn — Retry pattern
  2. Microsoft Learn — Compensating Transaction pattern