Inferensys

Prompt

Idempotency Check Prompt for Retried Tool Calls

A practical prompt playbook for using Idempotency Check Prompt for Retried Tool Calls in production AI workflows.
Developer doing prompt engineering on laptop, prompt variations visible on screen, casual coding session.
PROMPT PLAYBOOK

When to Use This Prompt

Define the job, reader, and constraints for the idempotency check prompt.

This prompt is for integration developers and reliability engineers who manage agent workflows that retry tool calls. The core job-to-be-done is preventing double-execution of non-idempotent operations—such as creating a duplicate payment, sending a second email, or provisioning a redundant resource—when a tool call fails and the orchestrator decides to retry. The ideal user is someone who already has a retry decision prompt in place but needs a safety gate before the retry is actually dispatched. Required context includes the original tool request payload, the error or timeout response, any partial state or side effects observed, and the proposed retry payload. Without this context, the prompt cannot assess whether the previous attempt left the system in a state that makes a naive retry dangerous.

Do not use this prompt when the tool operation is provably idempotent by contract (e.g., a PUT request with a client-supplied UUID, or an API that explicitly guarantees idempotency keys). In those cases, a standard retry decision prompt is sufficient and this check adds latency without benefit. Also avoid this prompt when the cost of double-execution is negligible or easily reversible—adding an idempotency gate for a read-only query or a cache-write is over-engineering. The prompt is most valuable in high-stakes domains like payments, provisioning, notifications, and database mutations where a duplicate operation creates a customer-facing incident or requires manual reconciliation. If the tool's side effects are opaque or the system cannot observe partial state, the prompt should default to a conservative 'do not retry' recommendation and escalate for human review rather than guessing.

After reading this section, you should know whether your retry workflow needs an idempotency safety gate. If it does, proceed to the prompt template section to copy the implementation-ready prompt. Before deploying, ensure you have a way to capture and pass the previous attempt's observable state—without that, the prompt cannot do its job. If your tool lacks observability into partial side effects, invest in that instrumentation first, then return to this playbook.

PRACTICAL GUARDRAILS

Use Case Fit

Where the idempotency check prompt adds value and where it introduces risk. Use this prompt before retrying any non-idempotent tool call to prevent double-execution and state corruption.

01

Good Fit: Payment, Write, and Mutate Operations

Use when: retrying tool calls that create charges, send emails, update databases, provision resources, or trigger external workflows. These operations carry double-execution risk that can corrupt state or cost money. Guardrail: Always run the idempotency check before retrying any POST, PUT, PATCH, or DELETE operation where the tool does not natively support idempotency keys.

02

Bad Fit: Read-Only and Idempotent-by-Design Tools

Avoid when: retrying GET requests, search queries, read-only database calls, or tools that already accept client-supplied idempotency keys. Adding an idempotency check adds latency and token cost without reducing risk. Guardrail: Skip this prompt when the tool's HTTP method, documentation, or contract confirms safe retry semantics.

03

Required Inputs: Previous State and Retry Payload

What to watch: The prompt cannot assess safety without knowing what the previous attempt did and what the retry will send. Missing partial state leads to false safety assessments. Guardrail: Always supply the previous tool call's response body, status code, any side-effect indicators, and the exact retry payload before invoking this prompt.

04

Operational Risk: False Safety Assessment

What to watch: The model may incorrectly declare a retry safe when the previous attempt partially succeeded but the response was lost or truncated. This is the highest-severity failure mode. Guardrail: When the previous attempt's outcome is unknown (timeout with no response, network error after request sent), default to unsafe and escalate to human review rather than trusting the model's safety assessment.

05

Operational Risk: Latency Amplification

What to watch: Adding an idempotency check prompt before every retry doubles the LLM calls in your retry loop, which can push time-sensitive workflows past their deadlines. Guardrail: Set a retry deadline budget and skip the idempotency check when remaining time is insufficient. Fall back to abort or human escalation instead of rushing an unsafe retry.

06

Operational Risk: Stale State Reconciliation

What to watch: The previous attempt's partial state may be stale if other processes or users modified the target resource between attempts. The model cannot detect external state changes. Guardrail: For resources that can change outside the agent's control, add a pre-retry state read (if safe and cheap) and include that fresh state in the idempotency check prompt context.

PROMPT PLAYBOOK

Copy-Ready Prompt Template

A reusable prompt template that analyzes tool side effects, partial state, and retry payloads to determine whether a retry is safe or requires state reconciliation first.

The prompt below is designed to be dropped into an agent's retry decision loop before re-invoking a non-idempotent tool. It forces the model to reason about what the tool already did, what state might exist from a partial execution, and whether replaying the same payload would cause double-execution, corruption, or inconsistency. Use this when the tool's documentation or API contract does not guarantee idempotency, or when the failure occurred mid-execution and you cannot determine the server-side outcome.

text
You are an idempotency safety checker for retried tool calls in a production agent system.

Your job is to analyze a failed tool call and determine whether retrying it with the same or modified arguments is safe, or whether state reconciliation must happen first.

## INPUT
- Tool name: [TOOL_NAME]
- Tool description: [TOOL_DESCRIPTION]
- Original arguments: [ORIGINAL_ARGUMENTS]
- Retry arguments (may differ): [RETRY_ARGUMENTS]
- Error context: [ERROR_CONTEXT]
- Known partial state or side effects from the failed attempt: [PARTIAL_STATE]
- Idempotency guarantees from tool documentation (if any): [IDEMPOTENCY_GUARANTEES]
- Business impact of double-execution: [BUSINESS_IMPACT]

## OUTPUT_SCHEMA
Return a JSON object with these fields:
{
  "retry_safe": boolean,
  "risk_level": "none" | "low" | "medium" | "high" | "critical",
  "double_execution_risk": boolean,
  "state_corruption_risk": boolean,
  "recommended_action": "retry_as_is" | "retry_with_reconciliation" | "verify_state_first" | "abort_and_escalate",
  "reconciliation_steps": [string] | null,
  "verification_queries": [string] | null,
  "modified_arguments": object | null,
  "rationale": string,
  "confidence": 0.0-1.0
}

## CONSTRAINTS
1. If the tool creates resources (POST), assume retry may create duplicates unless the tool explicitly supports idempotency keys.
2. If the error is a timeout or network failure, the original request may have succeeded server-side. Flag this explicitly.
3. If [PARTIAL_STATE] contains evidence that the operation partially completed, reconciliation_steps must be non-empty.
4. If [BUSINESS_IMPACT] is "high" or "critical" and retry_safe is false, recommended_action must be "verify_state_first" or "abort_and_escalate".
5. Never assume idempotency without explicit evidence from [IDEMPOTENCY_GUARANTEES].
6. If confidence is below 0.7, recommended_action must not be "retry_as_is".
7. modified_arguments should only be populated if a safe modification (e.g., adding an idempotency key, deduplication token, or conditional check) can reduce risk.

## EXAMPLES
[EXAMPLES]

## RISK_LEVEL
[RISK_LEVEL]

Adapt this template by replacing the square-bracket placeholders with data from your agent's execution context. The [PARTIAL_STATE] field is the most critical input—if your system cannot provide any evidence about what the tool did before failing, set this to "unknown" and expect the model to default to conservative recommendations. The [EXAMPLES] placeholder should contain 2-3 few-shot examples covering common scenarios: a safe retry on a read-only tool, a dangerous retry on a payment or write operation with unknown server state, and a reconciliation-required case where partial state is known. The [RISK_LEVEL] placeholder should reflect your domain's tolerance: set to "high" for financial operations, "medium" for CRM updates, or "low" for internal logging tools. Wire this prompt into your retry orchestrator so it executes before every non-idempotent retry attempt, and log the full JSON output to your audit trail for post-incident review.

IMPLEMENTATION TABLE

Prompt Variables

Required inputs for the idempotency check prompt. Each variable must be populated from the tool execution context before the prompt is assembled. Missing or null values will cause the safety analysis to fail closed.

PlaceholderPurposeExampleValidation Notes

[TOOL_NAME]

Identifies the tool being retried so the model can reason about its known side effects

send_payment

Must match a tool in the agent's registry; reject unknown tools

[TOOL_DESCRIPTION]

Describes the tool's behavior, side effects, and idempotency guarantees

Initiates a bank transfer. Not idempotent. Duplicate calls create duplicate transfers.

Must include side-effect language; null allowed only if tool is stateless and read-only

[PREVIOUS_REQUEST_PAYLOAD]

The exact arguments sent in the original (possibly failed) tool call

{"amount": 100.00, "recipient": "acct_123"}

Must be valid JSON matching the tool's input schema; reject if malformed

[PREVIOUS_RESPONSE_PAYLOAD]

The raw response or error received from the original call, if any

{"error": "timeout", "request_id": "req_456"}

Null allowed if no response was received; otherwise must be valid JSON

[RETRY_REQUEST_PAYLOAD]

The arguments prepared for the retry attempt

{"amount": 100.00, "recipient": "acct_123"}

Must be valid JSON; compare structurally to [PREVIOUS_REQUEST_PAYLOAD] for equivalence

[OBSERVED_STATE]

Any state observed after the original call that indicates partial completion

GET /transfers?request_id=req_456 returned status: completed

Null allowed; if provided, must be a string summarizing observed state from a read path

[IDEMPOTENCY_KEY]

A client-generated key intended to make the operation idempotent, if the tool supports one

idem_key_abc123

Null allowed; if present, check whether the tool actually respects this key in its contract

PROMPT PLAYBOOK

Implementation Harness Notes

How to wire the idempotency check prompt into a production retry loop with validation, logging, and safe defaults.

The idempotency check prompt is not a standalone safety net—it is a decision gate inside your tool execution harness. Wire it into the retry path immediately after a non-idempotent tool call fails or times out, but before any retry is issued. The harness must supply the prompt with three concrete inputs: the original tool request payload, the raw error or timeout context, and any partial state or side-effect evidence the system can observe (e.g., a created resource ID in the error body, a database record that appeared despite the timeout, or a log entry from an upstream system). Without this evidence, the prompt will default to a conservative 'do not retry' recommendation, which is the correct production posture.

Build the harness to call this prompt synchronously and block the retry until a decision is returned. Parse the output into a structured decision object with at minimum three fields: retry_safe (boolean), required_reconciliation_steps (array of strings), and rationale (string). If retry_safe is false, route to a human review queue or a fallback tool—never silently drop the operation. If retry_safe is true and required_reconciliation_steps is non-empty, execute those steps before retrying. Reconciliation steps might include checking for duplicate records, reversing a partial state change, or fetching the current resource state to compare against the original intent. Log every decision, the full prompt input, and the raw model output for auditability. For high-risk operations (payments, provisioning, destructive actions), add a hard requirement that retry_safe must be accompanied by explicit evidence references in the rationale before the harness allows the retry to proceed.

Choose a model with strong reasoning capabilities for this prompt—small or fast models often miss subtle side-effect indicators in error bodies. Set temperature to 0 to maximize determinism. Implement a strict output validator that rejects any response missing the required fields or containing ambiguous boolean values. If validation fails, default to retry_safe: false and escalate. Add an eval suite that tests the harness end-to-end with scenarios including: duplicate creation risk (the operation partially succeeded), state corruption risk (the operation wrote invalid data), clean failure (nothing happened, retry is safe), and ambiguous evidence (the error body hints at partial success but provides no resource ID). Measure both the correctness of the retry decision and whether the harness correctly blocked retries when the prompt output was malformed. The goal is a harness that fails closed, never open.

IMPLEMENTATION TABLE

Expected Output Contract

Fields, types, and validation rules for the idempotency check output. Use this contract to parse and validate the model's response before allowing a retry to proceed.

Field or ElementType or FormatRequiredValidation Rule

retry_safe

boolean

Must be true or false. If null, treat as unsafe and escalate.

idempotency_assessment

string enum

Must be one of: idempotent, safe_to_retry, unsafe_duplicate_risk, unsafe_state_corruption_risk, unknown. Reject any other value.

previous_side_effects_summary

string

Must be a non-empty string summarizing detected side effects from the prior attempt. If no prior state is available, must state 'No prior attempt state provided'.

state_reconciliation_required

boolean

Must be true if the assessment is unsafe_duplicate_risk or unsafe_state_corruption_risk. If true, reconciliation_steps must be non-empty.

reconciliation_steps

array of strings

Required when state_reconciliation_required is true. Each string must describe a concrete, ordered action. Reject empty arrays when required.

retry_payload_modifications

object or null

If present, must be a valid JSON object with only the fields that should change from [ORIGINAL_PAYLOAD]. Must not include the original payload verbatim. Null if no modifications needed.

confidence_score

number

Must be a float between 0.0 and 1.0 inclusive. Scores below 0.7 should trigger a human review gate before retry.

rationale

string

Must be a non-empty string explaining the reasoning, referencing specific fields from [PREVIOUS_ATTEMPT_STATE] and [RETRY_PAYLOAD]. Reject generic rationales with no evidence grounding.

PRACTICAL GUARDRAILS

Common Failure Modes

Idempotency checks fail silently when the model assumes safety, ignores partial state, or misjudges side effects. These cards cover the most common production failure patterns and how to prevent them.

01

Assuming Read-Only When Write Occurred

What to watch: The model classifies a tool as safe to retry because its primary description sounds read-only, but the implementation wrote a log, updated a last_accessed timestamp, or incremented a counter. Guardrail: Require the prompt to inspect the tool's full side-effect documentation, not just its name or summary. Add a mandatory field in the output schema for side_effect_evidence with explicit citations from the tool spec.

02

Ignoring Partial Success State

What to watch: A payment or provisioning tool returned a timeout, but the operation partially completed (e.g., charge authorized but not captured). The idempotency check treats the call as a clean failure and permits a naive retry, causing a double charge. Guardrail: The prompt must require analysis of any partial state indicators in the error body, response headers, or previous step outputs. Add a partial_state_risk boolean to the output and block retry when true without human review.

03

Retry Payload Mutation Breaks Idempotency Keys

What to watch: The retry logic modifies arguments (e.g., regenerates a timestamp or nonce) but the downstream system uses those arguments for idempotency, not a separate key. The retry creates a new resource instead of recovering the original. Guardrail: The prompt must compare the original request payload with the proposed retry payload field-by-field. Flag any difference in fields that the target API uses for idempotency. Add a payload_stability_check to the output.

04

Confusing Retry Safety with General Error Recovery

What to watch: The model conflates 'is this error retryable?' with 'is this operation idempotent?'. It correctly identifies a transient network error but fails to check whether the operation itself is safe to repeat. Guardrail: Separate the concerns in the prompt. Require two distinct output fields: error_retryable (based on error type) and operation_idempotent (based on side-effect analysis). Only permit retry when both are true.

05

Overlooking Downstream Side Effects

What to watch: The tool being retried appears idempotent, but it triggers a webhook, publishes an event, or calls a non-idempotent downstream service. The idempotency check stops at the direct tool boundary. Guardrail: The prompt must request the tool's dependency graph or downstream call documentation. Add a downstream_side_effect_risk assessment. If downstream effects exist and are non-idempotent, require a compensating transaction plan before permitting retry.

06

State Reconciliation Skipped Before Retry

What to watch: The model permits a retry but doesn't check whether the system state has changed since the original call (e.g., a related resource was deleted, a quota was exhausted by another process). The retry succeeds but leaves the system in an inconsistent state. Guardrail: Add a pre-retry state check step to the prompt. Require the model to list any state dependencies that must be verified before retry and output a state_reconciliation_required boolean with specific checks to perform.

IMPLEMENTATION TABLE

Evaluation Rubric

Criteria for evaluating the idempotency check prompt's output before integrating it into a production retry loop. Each row targets a specific failure mode that can cause double-execution or state corruption.

CriterionPass StandardFailure SignalTest Method

Idempotency Classification

Output correctly classifies the operation as idempotent, non-idempotent, or conditionally idempotent based on [TOOL_DESCRIPTION] and [PREVIOUS_PAYLOAD]

Classification contradicts the documented side effects of the tool or ignores a known state mutation

Run against a golden set of 10 tool descriptions with known idempotency properties; require >= 95% accuracy

Side Effect Identification

Lists all documented side effects from [TOOL_DESCRIPTION] and flags any that are likely to have partially executed based on [PREVIOUS_ERROR]

Omits a documented write operation or fails to flag a side effect that the error context suggests may have partially completed

Parse output for a structured side_effects array; validate that every write operation in the tool spec appears in the list

Partial State Assessment

Correctly identifies which parts of the previous attempt may have succeeded using [PREVIOUS_ERROR] and [PARTIAL_RESULT] context

Assumes the entire operation failed when the error code indicates a partial success, or assumes success when the error is ambiguous

Test with 5 error scenarios (timeout after write, mid-stream disconnect, 500 after commit, etc.); require correct partial state inference in 5/5 cases

Retry Safety Decision

Outputs a boolean [SAFE_TO_RETRY] with a clear rationale that accounts for idempotency, partial state, and [RETRY_PAYLOAD] differences

Returns true for a non-idempotent operation where the previous attempt may have partially succeeded and the new payload would duplicate the effect

Evaluate against 8 scenarios with known safe/unsafe outcomes; require 100% recall on unsafe retries (no false negatives)

State Reconciliation Instructions

When retry is unsafe, provides concrete reconciliation steps that would make the retry safe, referencing specific state checks from [SYSTEM_STATE_SCHEMA]

Omits reconciliation steps when retry is unsafe, or provides generic advice without referencing available state fields

For each unsafe retry scenario, check that output contains a non-empty reconciliation_steps array with at least one field reference from the system state schema

Payload Divergence Detection

Compares [PREVIOUS_PAYLOAD] and [RETRY_PAYLOAD] and flags any differences that would change the operation's effect on the system

Fails to detect a changed resource ID, amount, or target that would create a new side effect rather than retrying the original operation

Test with 4 payload pairs containing deliberate differences; require the diff_summary field to list all semantically meaningful differences

Double-Execution Risk Score

Outputs a numeric risk score between 0.0 and 1.0 that correlates with the actual probability of double-execution given the evidence

Scores a known high-risk scenario below 0.5 or a known safe scenario above 0.5

Calibrate against 10 scenarios with known outcomes; require risk score to rank scenarios in the correct order (Spearman correlation >= 0.9)

Audit Trail Completeness

Output includes all fields required by [OUTPUT_SCHEMA] with non-null values for decision-critical fields

Missing the rationale field, null risk score, or truncated reconciliation steps when the decision is unsafe

Validate output against the JSON schema; reject any response that fails schema validation or has null values in required decision fields

ADAPTATION OPTIONS

Adapt This Prompt

How to adapt

Add the full [PREVIOUS_PARTIAL_STATE] field, require a structured reconciliation plan, and include explicit checks for side-effect idempotency. Wire the output into a retry guard that blocks execution when safe_to_retry is false.

code
Before retrying this tool call, determine if the retry is safe:

Operation: [OPERATION_DESCRIPTION]
Previous Partial State: [PREVIOUS_PARTIAL_STATE]
Error Context: [ERROR_CONTEXT]
Retry Payload: [RETRY_PAYLOAD]
Idempotency Key: [IDEMPOTENCY_KEY]

Output schema:
{
  "safe_to_retry": boolean,
  "risk_level": "none"|"low"|"medium"|"high"|"critical",
  "double_execution_risk": boolean,
  "state_corruption_risk": boolean,
  "reconciliation_required": boolean,
  "reconciliation_steps": string[],
  "recommended_action": "retry"|"reconcile_first"|"abort"|"human_review",
  "evidence": string
}

Watch for

  • Silent format drift in the reconciliation_steps array
  • Missing idempotency key propagation to downstream systems
  • Retry storms when the guard prompt itself fails and defaults to retry
Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.