Agent Tool-Call Recovery and Idempotency Design
Design reliable AI agent tool execution that survives timeouts, retries, duplicate delivery, partial effects, stale approvals, and uncertain recovery.
Published: Jul 27, 2026 · Updated: Jul 27, 2026
You are a senior distributed-systems and AI-agent reliability architect specializing in tool contracts, side-effect safety, durable orchestration, idempotency, retries, reconciliation, compensation, approvals, and production recovery. Your task is to assess the supplied agent and tool architecture and produce an implementation-ready design for safe tool execution when requests time out, responses are lost, calls are duplicated, effects complete partially, approvals become stale, or automated recovery cannot determine what happened. The result must define the system boundary, execution guarantees, durable state model, operation identity, idempotency contract, retry policy, authoritative reconciliation process, compensation behavior, human recovery controls, fault-injection tests, and operational rollout plan. ## Context to Provide Replace every bracketed placeholder with the best available evidence. If critical information is missing, ask for it in one consolidated list before recommending a production design. If a missing detail is non-critical, continue only with a clearly labeled assumption. - [Agent objective, users, and operating boundaries] - [Tool inventory and contracts] - [Orchestration and persistence architecture] - [Side effects, business objects, and criticality] - [Failure, timeout, and incident evidence] - [Retry, queue, and delivery semantics] - [Idempotency and deduplication behavior] - [Confirmation, approval, and authorization rules] - [Status, reconciliation, and compensation capabilities] - [Observability, audit, privacy, and security requirements] - [Service objectives and recovery constraints] - [Definition of done] ## Evidence and Analysis Rules - Do not invent tool behavior, delivery guarantees, identifiers, schemas, error meanings, transaction boundaries, status endpoints, incidents, metrics, owners, approvals, test results, or recovery capabilities. - Separate confirmed evidence, inference, assumption, hypothesis, unknown, risk, recommendation, and decision. - Record the environment, version, date, source, and limitation of material evidence where available. - Preserve conflicting evidence until a specific check resolves it. - Do not describe an inspection, command, query, test, reconciliation, approval, or recovery action as completed unless its result was supplied. - Use `Not provided`, `Not inspected`, `Not run`, `Unknown`, or `To be agreed` where appropriate. - Redact credentials, tokens, personal data, customer records, confidential payloads, and unnecessary business values. - Prefer sanitized schemas, request fingerprints, state records, identifiers, and structured event examples over complete production payloads. - Tie every recommendation to a finding, accountable owner, verification method, and observable acceptance condition. - Identify whether each conclusion applies to one tool, one environment, one tenant, one workflow, or the complete agent system. - Do not extrapolate a guarantee from one component to the complete end-to-end workflow. ## Required Terminology and Boundaries Model the following concepts separately: 1. **Business intent:** The outcome the user or authorized system intended. 2. **Logical operation:** One durable instruction to pursue that intent. 3. **Execution attempt:** One transmission or invocation of the operation. 4. **External effect:** A change created in the tool or downstream system. 5. **Acknowledgment:** Evidence that a request was received or accepted. 6. **Authoritative result:** Evidence that establishes the final effect state. 7. **Compensation:** A separate business action intended to offset an earlier effect. 8. **Recovery action:** An automated or human action used to resolve an unknown or failed state. Do not use request ID, trace ID, correlation ID, attempt ID, operation ID, provider object ID, and idempotency key as though they are interchangeable. Do not claim “exactly once” execution without defining: - what occurs once; - within which system boundary; - for which identity and retention period; - under which failure and replay assumptions; - whether the guarantee covers invocation, durable state change, external effect, notification, or complete business outcome. Where appropriate, use narrower language such as: - at-most-once effect within the deduplication window; - at-least-once delivery with idempotent effect handling; - effectively-once business outcome under stated assumptions; - duplicate suppression within a defined scope; - unknown outcome requiring reconciliation. ## Core Safety Principles - A timeout proves only that the caller did not receive a timely response. It does not prove that the tool performed no effect. - An accepted, queued, or dispatched response is not authoritative completion unless the tool contract explicitly defines it that way. - Generate or assign one stable idempotency key for each logical operation and durably reuse it across retries, restarts, workers, and redeliveries. - A random high-entropy key is acceptable when generated once per logical operation. Never generate a new key merely because an attempt is retried. - Do not place secrets, personal data, email addresses, account details, or sensitive business values inside idempotency keys. - Scope idempotency by the identities needed to prevent cross-tenant, cross-environment, cross-tool, or cross-operation collisions. - Bind the key to a canonical fingerprint of the material request. Reject reuse of the same key with materially different intent or parameters. - Make the retention period longer than the maximum queue delay, retry window, replay window, outage recovery period, late-response window, and expected manual recovery delay—or explicitly block and reconcile attempts that arrive after expiry. - Where possible, record the idempotency decision and commit the protected state change atomically. - When atomicity cannot span the external tool, identify the crash gap and use an appropriate pattern such as a transactional outbox, inbox deduplication, effect journal, provider operation identifier, or reconciliation worker. - Place automatic retries at one deliberate layer unless evidence justifies retries at multiple layers. - Bound retries by attempt count, elapsed deadline, backoff, jitter, retry budget, dependency health, and business validity. - Cancellation does not prove that an already-dispatched effect was prevented. Reconcile an in-flight operation before declaring it cancelled. - Compensation is not equivalent to rollback. It may be partial, delayed, externally visible, chargeable, irreversible, or independently unsuccessful. - Bind consequential approval to the exact action, target, material parameters, object version, authority, approver, and expiry. - Require new approval if user intent, recipient, amount, permissions, object state, policy, price, or another material input changes. - Make automated and manual recovery concurrency-safe so two workers or operators cannot apply conflicting remedies. - Require authoritative reconciliation before converting an unknown outcome into confirmed success or confirmed failure. ## Required Design Workflow ### 1. Establish Input Sufficiency and System Boundary Define: - the agent’s objective and authorized users; - the systems and environments in scope; - where the logical operation begins and ends; - which component owns orchestration state; - which system is authoritative for each business object and effect; - the delivery semantics of every queue, scheduler, webhook, and worker; - the assumptions under which safe recovery is expected; - critical missing information that prevents a reliable design. ### 2. Inventory Tool Effects and Contracts For every tool operation, identify: - action and business purpose; - input and output contract; - effect type; - preconditions and postconditions; - synchronous, asynchronous, or mixed completion; - authoritative completion evidence; - reversibility and compensation capability; - financial, destructive, permission, privacy, customer, or external communication impact; - native idempotency support; - provider operation or object identifiers; - status lookup and reconciliation capability; - error taxonomy; - timeout and rate-limit behavior; - owner and escalation path. Distinguish read-only operations from operations that create, update, delete, publish, send, charge, reserve, approve, grant access, or otherwise produce external effects. ### 3. Define the Execution Guarantee State the strongest guarantee that the available architecture can honestly support. Compare: - at-most-once invocation; - at-least-once delivery; - idempotent processing; - duplicate suppression; - effectively-once effect; - eventual reconciliation; - manual resolution of unknown outcomes. Explain what the guarantee does not cover. If the architecture cannot prevent or reconcile duplicate consequential effects, state this as a blocker rather than describing the workflow as reliable. ### 4. Design Durable Identity Define the purpose and lifecycle of: - agent run ID; - workflow or orchestration ID; - logical operation ID; - idempotency key; - request fingerprint; - execution attempt ID; - trace or correlation ID; - provider request ID; - provider object or effect ID; - approval ID and approval digest; - compensation operation ID; - recovery case ID. For each identifier, specify: - creator; - generation method; - scope; - persistence location; - uniqueness requirement; - propagation path; - retention period; - lookup use; - security restrictions. The request fingerprint must include only material fields and must use a documented canonicalization and versioning rule. ### 5. Design the Execution State Machine Adapt the state names to the supplied system, but explicitly represent: - planned; - awaiting approval; - ready; - dispatching; - accepted or pending; - outcome unknown; - reconciling; - confirmed succeeded; - confirmed failed without effect; - compensation pending; - compensating; - compensated; - manual review; - cancelled before dispatch; - terminal unresolved or abandoned under approved policy. For every state, define: - durable record; - entry condition; - permitted events; - permitted next states; - timeout behavior; - retry eligibility; - responsible component; - authoritative evidence; - customer or operator visibility; - prohibited transitions. Do not permit an in-flight or unknown operation to transition directly to “cancelled with no effect,” “failed with no effect,” or a new execution attempt without the evidence required by the tool contract. ### 6. Design the Idempotency Contract Specify: - logical operation identity; - idempotency-key generation and reuse; - tenant, account, environment, tool, and action scope; - request fingerprint and conflict behavior; - first-request concurrency handling; - duplicate behavior while the first request is in progress; - result storage and semantically equivalent replay; - failure-result treatment; - persistence and atomicity boundary; - retention period and expiry behavior; - late-arriving request behavior; - replay after expiry; - resource deletion or mutation after the original operation; - downstream propagation; - migration and versioning behavior. For concurrent calls with the same key, define whether the system uses locking, leasing, single-flight execution, a uniqueness constraint, compare-and-set, or another proven control. For the same key with a different material fingerprint, require an explicit conflict response. Do not silently reinterpret it as a new operation. ### 7. Design Retry and Ambiguous-Outcome Handling Classify each result according to whether the effect is known not to have started, known to have succeeded, known to have failed, or remains unknown. Address at least: - local validation failure before dispatch; - authentication or authorization failure; - connection failure known to occur before request transmission; - connection loss after transmission may have begun; - client timeout after dispatch; - rate limiting and `Retry-After`; - transient dependency failure; - permanent validation or semantic failure; - duplicate or operation-in-progress response; - asynchronous acceptance; - partial multi-step effect; - late success after the caller timed out; - out-of-order webhook or queue delivery; - unavailable status endpoint; - expired idempotency record; - cancelled request whose execution status is uncertain. For each class, choose one action: - correct input; - stop; - retry with the same operation identity; - wait; - query authoritative status; - reconcile; - compensate; - request fresh approval; - open manual recovery; - escalate an incident. Specify maximum attempts, overall deadline, backoff, jitter, retry budget, circuit or load-shedding behavior, and the component permitted to retry. ### 8. Design Reconciliation Define an evidence hierarchy for determining whether an effect occurred. Possible evidence may include: - authoritative provider status; - provider object ID; - effect receipt; - immutable ledger entry; - business-object state; - signed webhook; - transaction or event journal; - downstream confirmation; - local logs and traces. Do not treat local logs or an agent-generated summary as authoritative when a downstream system owns the effect. For each unknown state, specify: - first reconciliation check; - polling or event schedule; - maximum reconciliation duration; - stale-read and eventual-consistency handling; - conflicting-evidence handling; - matching identifiers; - owner; - escalation threshold; - terminal policy when reality remains unknowable. ### 9. Design Approval and Authorization Freshness For every consequential tool call, define: - who can request the action; - who can approve it; - whether separation of duties is required; - exact parameters covered by approval; - object version or state covered by approval; - approval expiry; - cancellation and revocation behavior; - conditions requiring reapproval; - authorization recheck immediately before dispatch; - evidence recorded for audit. Do not infer approval from silence, prior unrelated consent, an earlier materially different request, or an AI-generated interpretation of authority. ### 10. Design Compensation and Manual Recovery For every compensatable operation, define: - effect being compensated; - compensation action; - business and technical preconditions; - authorization and approval; - idempotency identity for the compensation itself; - expected residual effects; - financial or customer impact; - evidence of completion; - retry and reconciliation behavior; - failure handling; - owner and escalation path. Identify irreversible points of no return. For manual recovery, define: - queue and priority; - case payload; - minimum necessary evidence; - sensitive-data restrictions; - claim, lease, or locking behavior; - permitted operator actions; - maker-checker requirement where relevant; - duplicate-operator protection; - service target; - customer communication owner; - closure evidence; - post-recovery reconciliation. ### 11. Design Observability and Auditability Define the logs, traces, metrics, events, dashboards, alerts, and audit records needed to answer: - What did the user intend? - What operation was authorized? - Which attempts were made? - Which component initiated each attempt? - Which idempotency key and fingerprint were used? - What did the tool acknowledge? - What effect is authoritatively confirmed? - Which operations remain unknown? - Was compensation attempted or completed? - Who performed or approved manual recovery? - Did duplicate suppression or a key conflict occur? - Did the retry or reconciliation deadline expire? Keep sensitive payloads and credentials out of telemetry. Use references, hashes, redacted summaries, or protected evidence stores where appropriate. ### 12. Design Tests and Rollout Include deterministic and fault-injection tests for: - duplicate concurrent requests; - worker crash before dispatch; - crash after dispatch but before recording the response; - effect completion followed by response loss; - response recording without effect completion; - asynchronous acceptance followed by later failure; - timeout followed by late success; - retry after process restart; - redelivery after idempotency expiry; - same key with changed parameters; - out-of-order status events; - partial multi-step effects; - stale or revoked approval; - cancellation during dispatch; - compensation failure; - two operators attempting recovery; - tenant-boundary and authorization violations; - unavailable or stale reconciliation sources. Use sandbox, simulation, fault injection, or controlled fixtures before production. Never create real financial, destructive, permission, communication, or customer-visible effects merely to prove the recovery design. ## Output Format Use the following markdown sections. Use tables for mappings, state transitions, ownership, decisions, and test coverage. ### Input Sufficiency and System Boundary Provide: - scope; - systems and environments; - authoritative systems; - supplied evidence; - critical missing inputs; - assumptions; - blockers; - definition of done. ### Guarantee Statement State the proposed end-to-end guarantee, its exact boundary, retention period, assumptions, exclusions, and unresolved limitations. ### Tool Risk Inventory Use: | Tool operation | Effect | Criticality | Completion evidence | Native idempotency | Status lookup | Compensation | Approval | Owner | |---|---|---|---|---|---|---|---|---| ### Identity and Correlation Model Use: | Identifier | Purpose | Created by | Scope | Persisted where | Retention | Propagation | Security restriction | |---|---|---|---|---|---|---|---| ### Execution State Machine Use: | Current state | Event or evidence | Guard condition | Next state | Durable update | Owner | Prohibited alternative | |---|---|---|---|---|---|---| Also identify unreachable, ambiguous, terminal, and manually recoverable states. ### Idempotency Contract Define key generation, reuse, scope, fingerprinting, concurrency, atomicity, replay response, conflicts, retention, expiry, late requests, downstream propagation, and unsupported guarantees. Include implementation-neutral pseudocode for the receive, reserve, execute, record, replay, and conflict paths where useful. ### Retry and Ambiguous-Outcome Matrix Use: | Condition | Effect certainty | Automatic retry | Required identity | Delay or deadline | Reconciliation | Approval consequence | Final fallback | |---|---|---|---|---|---|---|---| ### Approval and Authorization Contract Show how approval is bound to material action details, how freshness is checked, which changes invalidate approval, and who can authorize execution or recovery. ### Reconciliation Protocol Use: | Unknown condition | Authoritative source | Lookup key | Check cadence | Conflict rule | Deadline | Escalation owner | Terminal policy | |---|---|---|---|---|---|---|---| ### Compensation and Human Recovery Plan Separate: 1. Automated compensation 2. Human-assisted recovery 3. Irreversible or non-compensatable effects Document residual effects and secondary failure handling. ### Scenario and Fault-Injection Tests Use: | Test | Failure point | Setup | Expected state | Prohibited effect | Evidence to collect | Pass criteria | |---|---|---|---|---|---|---| Do not mark a test as passed unless its result was supplied. ### Observability and Operations Plan Define: - required events and fields; - dashboards and alerts; - unknown-outcome queue; - reconciliation schedule; - service objectives; - incident roles; - recovery access controls; - retention and redaction; - review cadence; - feedback into tool contracts and agent policy. ### Prioritized Implementation Plan Separate: 1. Immediate containment 2. Design and implementation 3. Controlled validation 4. Production rollout 5. Continuous monitoring For each action, include the finding addressed, owner, prerequisites, validation method, approval gate, rollback or containment method, and acceptance condition. ### Decisions, Risks, and Follow-Up Questions Record unresolved design decisions, accepted risks, blocking questions, accountable owners, and target decision dates. ## Verification Checklist Before finalizing, confirm that: - the business intent, logical operation, attempt, external effect, and compensation are modeled separately; - the end-to-end guarantee has a precise scope and is not described casually as “exactly once”; - a timeout or missing response is not treated automatically as failure; - accepted or queued work is not treated automatically as completed; - one stable idempotency identity survives retries, restarts, redelivery, and worker changes; - the same key with materially different parameters produces a conflict; - idempotency retention covers the documented replay and late-delivery horizon; - the atomicity boundary and every remaining crash gap are explicit; - retry eligibility is based on error class and effect certainty; - retries are bounded and do not multiply uncontrolled across system layers; - unknown consequential effects are reconciled before retry; - approval is bound to material action details and revalidated before execution; - cancellation is not treated as proof that an in-flight effect was prevented; - compensation is treated as a separate fallible operation rather than guaranteed rollback; - automated and human recovery are concurrency-safe and auditable; - credentials and sensitive payloads are excluded from keys, telemetry, and recovery interfaces; - tests cover duplicates, partial effects, late responses, stale approval, expiry, and recovery races; - no unrun test, uninspected artifact, unresolved conflict, or unapproved action is described as complete; - the recommended next action is the smallest safe step that materially reduces uncertainty or risk. ## Final Instruction to Begin Begin by reviewing the supplied context for blocking gaps. If any exist, request them in one consolidated list. Otherwise, establish the system boundary and evidence inventory, then complete the design in the required order.
Variables to Replace
- Agent objective, users, and operating boundaries
- Tool inventory and contracts
- Orchestration and persistence architecture
- Side effects, business objects, and criticality
- Failure, timeout, and incident evidence
- Retry, queue, and delivery semantics
- Idempotency and deduplication behavior
- Confirmation, approval, and authorization rules
- Status, reconciliation, and compensation capabilities
- Observability, audit, privacy, and security requirements
- Service objectives and recovery constraints
- Definition of done
How to Use This Prompt
Replace every placeholder with sanitized architecture details, tool contracts, schemas, state records, retry settings, queue behavior, failure traces, approval rules, status APIs, and recovery procedures.
Run the completed prompt in Claude. Have the resulting design reviewed by the agent engineering team, tool owners, distributed-systems or reliability engineers, security and privacy owners, and the business owners accountable for consequential effects.
Validate the proposed state transitions and idempotency behavior with controlled fault-injection tests before enabling autonomous financial, destructive, permission-changing, customer-facing, or externally visible actions.
Example Use Case
An order-management agent can reserve inventory, charge a payment method, and create a shipping label. A payment request times out after transmission, leaving the agent unable to determine whether the customer was charged. The team supplies the tool contracts, provider status APIs, orchestration records, retry implementation, approval rules, incident traces, and recovery responsibilities so Claude can design safe reconciliation and prevent a duplicate charge.