Inferensys

Prompt

Idempotency Key Injection Prompt for Write Tools

A practical prompt playbook for using Idempotency Key Injection Prompt for Write Tools in production AI workflows.
Developer demonstrating multi-agent tool use, agent tool selection interface on laptop, casual tech demo moment.
PROMPT PLAYBOOK

When to Use This Prompt

Identifies the exact integration scenario where injecting an idempotency key into a write tool call prevents duplicate side effects during retries.

This prompt is designed for integration developers who are wrapping payment, provisioning, or state-mutating endpoints behind an AI tool interface. The core job-to-be-done is ensuring that when an AI model calls a write tool, the execution layer can safely retry that request without duplicating the side effect. This is critical in distributed systems where network timeouts, model re-invocation, or execution-layer retry logic can cause the same logical operation to be submitted multiple times. The prompt instructs the model to generate a unique, traceable idempotency key and attach it to the tool call arguments, shifting the responsibility for safe retries from the model to the downstream API.

You should use this prompt when your tool definitions include operations like create_payment, provision_user, update_order_status, or any endpoint where a duplicate call would result in a double charge, duplicate resource, or corrupted state. The ideal user is an engineer building the integration layer between an AI orchestrator and backend services, who needs the model to participate in the idempotency contract. Do not use this prompt for read-only tool calls (e.g., get_balance, list_users) or when the downstream API already manages its own idempotency natively via a header or separate endpoint. Applying it to read operations adds unnecessary complexity and key-generation overhead with no safety benefit.

Before deploying, you must ensure that your execution harness can extract the generated idempotency key from the tool call arguments and pass it to the downstream API according to that API's contract (e.g., as an Idempotency-Key header or a top-level field). The prompt includes instructions for the model to use a predictable key format—typically a combination of an operation type prefix, a unique request identifier, and a timestamp—which makes the key both traceable and collision-resistant. You should also implement eval checks that verify key uniqueness across a test suite, confirm format compliance, and simulate replay scenarios to ensure the downstream system correctly returns the stored result rather than re-executing the operation. The next step is to pair this prompt with a tool call validation guardrail that rejects any write tool call missing an idempotency key before it reaches your backend.

PRACTICAL GUARDRAILS

Use Case Fit

Where the Idempotency Key Injection Prompt delivers value and where it introduces risk. Use these cards to decide if this prompt fits your operational context before integrating it into a production write path.

01

Good Fit: State-Mutating Write Endpoints

Use when: your tool calls payment APIs, provisioning systems, database inserts, or any endpoint where duplicate execution causes double-spend, double-provisioning, or data corruption. Guardrail: The prompt must inject a key before the write, and the execution layer must check it atomically.

02

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

Avoid when: the tool performs a GET, a pure lookup, or is already idempotent at the API level. Injecting keys here adds latency, storage cost, and key management overhead with no safety benefit. Guardrail: Gate key injection behind a tool classification check for write vs. read operations.

03

Required Input: Deterministic Key Material

Risk: A random or non-deterministic key defeats replay detection. Guardrail: The prompt must derive the key from stable inputs such as a request ID, a hash of the payload, or a user-provided idempotency token. Validate that the same logical request always produces the same key.

04

Required Input: Key Lifecycle Contract

Risk: The model generates a key but the execution layer doesn't know how long to retain it or when to expire it, leading to false duplicate rejections or storage leaks. Guardrail: The prompt must include a TTL or retention policy in the tool call arguments, and the harness must enforce it.

05

Operational Risk: Key Collision Across Tenants

Risk: Two different tenants or workflows generate the same key, causing one request to be incorrectly rejected as a duplicate. Guardrail: The prompt must prefix or namespace the key with tenant ID, tool name, or workflow instance. Validate uniqueness across the collision domain in eval tests.

06

Operational Risk: Replay Without Conflict Detection

Risk: The execution layer receives a key but doesn't return whether the call was a new execution or a replay, leaving the model and user uncertain about the outcome. Guardrail: The prompt must instruct the tool response to include a replay boolean field, and the model must surface this to the user.

PROMPT PLAYBOOK

Copy-Ready Prompt Template

A reusable system prompt that instructs the model to inject a generated, traceable idempotency key into every write tool call and return replay status.

This prompt template is designed to be pasted directly into your system or tool-use prompt for any AI workflow that wraps state-mutating endpoints—such as payment capture, provisioning, or database writes. It forces the model to generate a unique, formatted idempotency key before calling a write tool, declare whether the call is a new execution or a replay, and provide the execution layer with clear instructions on key lifecycle. The template uses square-bracket placeholders so you can adapt it to your specific tool names, key format, and risk level without rewriting the core logic.

text
You are an integration agent responsible for calling write tools safely. Every write operation must be idempotent.

## TOOL SELECTION RULES
- Before calling any tool listed in [WRITE_TOOLS], you MUST generate an idempotency key.
- Read tools in [READ_TOOLS] do not require an idempotency key.
- If the user request does not clearly map to a single write tool, ask for clarification instead of guessing.

## IDEMPOTENCY KEY GENERATION
- Generate a key using the format: [KEY_FORMAT] (e.g., "evt_" + UUIDv4, or "idem-" + SHA256 hash of [IDEMPOTENCY_INPUTS]).
- The key MUST be unique per logical operation. Use these inputs to derive it: [IDEMPOTENCY_INPUTS] (e.g., user_id, operation_type, idempotent_payload_hash, timestamp).
- Do not reuse keys across different operations.

## TOOL CALL OUTPUT CONTRACT
When calling a write tool, produce a JSON object with this exact schema:
{
  "tool_name": "string",
  "arguments": { ... },
  "idempotency_key": "string",
  "is_replay": false,
  "key_lifecycle": {
    "generated_at": "ISO8601 timestamp",
    "expires_at": "ISO8601 timestamp or null",
    "storage_instruction": "One of: 'persist_until_ack', 'cache_ttl_24h', 'derive_only'"
  },
  "replay_detection_note": "Explain how you determined this is a new or replayed call."
}

## REPLAY DETECTION
- If the user provides an existing idempotency key in [REPLAY_KEY_FIELD], treat the call as a replay.
- Set `is_replay` to true and populate `replay_detection_note` with the source of the key.
- Do not generate a new key for replays.

## CONSTRAINTS
- [CONSTRAINTS]
- If the risk level is [RISK_LEVEL], require human approval before dispatching the tool call. Set `key_lifecycle.storage_instruction` to "persist_until_ack" and include an `approval_required` flag.

## EXAMPLES
[EXAMPLES]

To adapt this template, replace the square-bracket placeholders with your concrete values. [WRITE_TOOLS] should list the tool names that mutate state (e.g., create_payment, provision_instance). [KEY_FORMAT] defines the expected key structure, which your execution layer will validate. [IDEMPOTENCY_INPUTS] specifies which fields the model should hash or combine to produce a deterministic key for the same logical operation. [REPLAY_KEY_FIELD] tells the model where to look for an existing key in the user input. [CONSTRAINTS] can include rate limits, cost caps, or permission boundaries. [RISK_LEVEL] controls whether human approval is required—set it to high for payments or provisioning, low for internal state updates. [EXAMPLES] should include at least one new-execution example and one replay example with the correct output shape. After pasting this prompt, validate that your tool execution layer actually enforces idempotency using the generated key—the prompt alone does not guarantee safe retries.

IMPLEMENTATION TABLE

Prompt Variables

Required inputs for the idempotency key injection prompt. Each placeholder must be populated before the prompt is assembled and sent to the model.

PlaceholderPurposeExampleValidation Notes

[TOOL_SCHEMA]

The JSON schema of the write tool being called, including its parameters and description

{"name": "create_payment", "parameters": {"amount": "number", "currency": "string"}}

Must be valid JSON Schema. Required fields must be marked. Schema must include a description field for the model to reason over.

[IDEMPOTENCY_KEY_PREFIX]

A namespace or service identifier prepended to the generated key to prevent cross-service collisions

pay_

Must be a non-empty string. Should match the regex ^[a-z][a-z0-9_]{0,15}$ to avoid key parsing issues downstream.

[KEY_FORMAT]

The expected format specification for the generated idempotency key

uuid_v4

Must be one of: uuid_v4, ulid, ksuid, or a custom regex pattern. If custom, provide the exact regex and max length constraint.

[KEY_TTL_SECONDS]

The time-to-live for the idempotency key in the storage layer, defining the replay detection window

86400

Must be a positive integer. Should align with the downstream service's idempotency window. Null not allowed; use 0 only if the key is single-use with no expiry.

[CALLER_IDENTITY]

A stable identifier for the caller or session, used to scope the idempotency key and prevent cross-tenant replay

user_42b6f1a3

Must be a non-empty string. Should be derived from an authenticated principal, not user-supplied input. Null allowed only for unauthenticated write operations, which must be explicitly flagged for review.

[REQUEST_PAYLOAD_HASH]

A hash of the canonical request payload, used for payload-consistency checks on replay detection

sha256:abc123def456

Must be a hex-encoded SHA-256 hash. If null, the prompt must instruct the model to generate the key without payload binding, which weakens replay detection. Null requires explicit approval.

[CONFLICT_STRATEGY]

The declared behavior when an idempotency key collision is detected

return_existing

Must be one of: return_existing, reject_with_error, or compare_and_swap. The prompt must instruct the model to include this strategy in the tool call metadata so the execution layer can act on it.

[OUTPUT_SCHEMA]

The expected structure of the model's response, including the tool call, generated key, and replay detection fields

{"tool_call": {}, "idempotency_key": "string", "is_replay": "boolean"}

Must be a valid JSON Schema. The schema must include idempotency_key, is_replay, and the full tool call object. Additional metadata fields are allowed but must be documented.

PROMPT PLAYBOOK

Implementation Harness Notes

How to wire the idempotency key injection prompt into a production write-tool execution pipeline.

This prompt is designed to sit inside a pre-execution hook in your tool-calling pipeline. When the model selects a write-capable tool (POST, PUT, PATCH, DELETE), the harness intercepts the tool call, runs this prompt to generate an idempotency key and execution metadata, and then attaches the result to the outgoing request. The prompt is not the tool call itself—it is a metadata injection layer that wraps the tool call before it reaches the execution runtime. This separation keeps the model's original tool selection logic clean while adding operational safety guarantees.

Implementation flow: (1) Model selects a write tool and generates arguments. (2) Harness classifies the tool as write based on a tool registry flag or OpenAPI spec method. (3) Harness calls this prompt with [TOOL_NAME], [TOOL_ARGUMENTS], [IDEMPOTENCY_KEY_FORMAT] (e.g., {service}-{entity}-{uuid}), and [KEY_TTL_SECONDS]. (4) The prompt returns a structured JSON object containing idempotency_key, key_format_valid, injection_timestamp, and execution_instruction (e.g., NEW_EXECUTION or REPLAY_DETECTED). (5) The harness injects the key into the tool call header or payload according to the target API's idempotency contract (typically an Idempotency-Key header or a top-level field). (6) The execution layer stores the key with a TTL, checks for duplicates before dispatch, and returns the replay status to the harness.

Validation and safety checks: Before the tool call executes, validate that the generated key matches the declared format using a regex validator. Check the key store for collisions—if a duplicate exists and the payload differs, abort and log a collision event for human review. If the duplicate exists with the same payload, return REPLAY_DETECTED and skip execution. Retry logic: If the write tool fails with a network error or timeout, the harness retries with the same idempotency key, not a new one. This is the core value of the pattern. Set a maximum retry budget (e.g., 3 attempts) and a deadline (e.g., 30 seconds). After exhaustion, escalate to a dead-letter queue with the full key and payload for manual reconciliation. Logging: Emit structured logs at each stage—key generation, duplicate check, injection, execution, and replay detection—so that every write operation is traceable end-to-end.

Model choice and latency: This prompt is lightweight and should run on a fast, cheap model (e.g., GPT-4o-mini, Claude Haiku, or a local fine-tuned classifier) because it adds latency to every write path. If your write volume is high, consider caching generated keys for identical tool-argument hashes within the TTL window, or pre-generating keys client-side using a deterministic hash of the request payload. Human review trigger: If the prompt detects a key collision with a different payload, or if the key format validation fails, route to a human review queue before executing. Do not silently regenerate a new key on collision—that defeats the idempotency guarantee. What to avoid: Never use this prompt for read-only tools; it adds unnecessary latency and key-store overhead. Never skip the duplicate check before execution. Never allow the execution layer to mutate state without first persisting the idempotency key.

IMPLEMENTATION TABLE

Expected Output Contract

Fields, types, and validation rules for the idempotency key injection tool call. Use this contract to validate the model's output before forwarding to the execution layer.

Field or ElementType or FormatRequiredValidation Rule

tool_name

string

Must match an allowed write-tool name from [TOOL_LIST]. Reject if tool is read-only or not in list.

idempotency_key

string (UUID v4 or [KEY_PREFIX]-[TIMESTAMP]-[RANDOM])

Must match regex pattern defined in [KEY_FORMAT]. Reject on collision with active key store. Must be unique per intent.

key_source

enum: 'generated' | 'user_provided' | 'derived'

Must be 'generated' unless [USER_KEY] is present and valid. 'derived' allowed only when [IDEMPOTENCY_SCOPE] is 'request'.

intent_fingerprint

string (hex-encoded hash)

Must be SHA-256 of canonicalized [TOOL_NAME] + sorted [ARGUMENTS]. Reject if fingerprint does not match computed hash of provided arguments.

arguments

object

Must conform to the target tool's JSON Schema from [TOOL_SCHEMAS]. All required fields present. No extra fields beyond schema.

replay_detection_mode

enum: 'error' | 'return_prior' | 'ignore'

Must be one of the allowed modes from [REPLAY_POLICY]. Default to 'error' if not specified in policy.

key_ttl_seconds

integer >= 0

Must be within [MIN_TTL] and [MAX_TTL] bounds. 0 means no expiry. Reject if TTL exceeds system maximum.

execution_instruction

string

Must contain explicit directive: 'If key exists and status is COMPLETED, return stored result. If IN_FLIGHT, wait or reject per replay_detection_mode. If not found, execute and store.' Parse check for required clauses.

PRACTICAL GUARDRAILS

Common Failure Modes

Idempotency key injection is a powerful pattern for safe writes, but it introduces its own failure modes. These cards cover what breaks first when you put idempotency logic into a prompt and how to guard against it.

01

Key Collision from Non-Unique Seeds

Risk: The model generates a deterministic key from only a timestamp or a weak hash of the payload, causing unrelated requests to collide and one to be incorrectly rejected as a duplicate. Guardrail: Require the prompt to combine at least three entropy sources (e.g., request_id + operation_type + client_nonce). Validate key uniqueness in the harness before execution.

02

Silent Replay Without Status Differentiation

Risk: The execution layer replays a previous response without indicating it was a replay, leading the user or downstream system to believe a new action was taken (e.g., double-charge perception). Guardrail: The prompt must instruct the model to include an explicit replay_status field (new or replay) in the tool call arguments, and the harness must surface this in the user-facing response.

03

Key Format Drift Across Retries

Risk: On retry, the model generates a slightly different key format (e.g., UUID vs. hash) or re-derives the key from altered context, breaking the idempotency guarantee. Guardrail: Pin the key format in the system prompt with a strict regex or format constraint (e.g., RFC 9562 UUID v7). Add a pre-execution validation check that rejects malformed keys before dispatch.

04

Infinite Retry on Conflict Without Escalation

Risk: The model detects a key conflict and retries with a new key in a loop, exhausting resources or hitting rate limits without resolving the underlying state mismatch. Guardrail: Limit the prompt to a single retry attempt with a new key. If conflict persists, the harness must escalate to a human or a dead-letter queue with the conflicting keys and payloads for manual reconciliation.

05

Key Lifecycle Mismanagement (TTL Ignored)

Risk: The model treats an idempotency key as valid forever, replaying a response for a request that the business logic considers expired, leading to stale or incorrect state mutations. Guardrail: The prompt must accept a key_ttl_seconds parameter and include it in the tool call. The execution harness must reject keys older than the TTL and force a fresh key generation with a key_expired error.

06

Payload Mismatch on Key Reuse

Risk: The model reuses an existing idempotency key but sends a different request payload, expecting the same result. The execution layer either silently returns the old result (data corruption) or rejects the mismatch (unhandled error). Guardrail: The harness must compare the new request payload hash against the stored payload hash for the key. On mismatch, return a 409 Conflict with a clear error, and prompt the model to generate a new key for the new payload.

IMPLEMENTATION TABLE

Evaluation Rubric

Criteria for testing idempotency key injection quality before shipping to production. Each row targets a specific failure mode observed in write-tool dispatch.

CriterionPass StandardFailure SignalTest Method

Key presence in tool call

Every write tool call includes exactly one idempotency key in the arguments

Missing key field or multiple keys in a single call

Schema validation: assert arguments.idempotency_key is present and non-null for all write operations

Key format compliance

Key matches [IDEMPOTENCY_KEY_FORMAT] regex and is between 16 and 128 characters

UUID without prefix, short random string, or key exceeding length limit

Regex match test against 100 generated keys; length assertion in eval harness

Key uniqueness across calls

No duplicate keys across 1000 consecutive tool calls from the same [CALLER_ID]

Collision detected within the test window

Deterministic replay test: run 1000 calls, collect all keys, assert set size equals call count

Replay detection in response

When a duplicate key is submitted, the response includes replay_detected: true and original_result

Duplicate key treated as new execution or returns empty result

Submit same key twice within [REPLAY_WINDOW_SECONDS]; assert replay_detected field and result equality

Key source traceability

Key is derived from [CALLER_ID], [IDEMPOTENCY_SCOPE], and a monotonic or random component

Key is a bare UUID with no caller or scope prefix

Parse key prefix; assert it contains [CALLER_ID] and [IDEMPOTENCY_SCOPE] segments

Conflict resolution on collision

When key collision is detected, the execution layer returns stored result without re-executing the write

Write is re-executed, causing duplicate side effect

Integration test: inject known key collision, verify write endpoint is called exactly once

Key lifecycle documentation

Response includes key_expires_at or key_ttl_seconds field when [KEY_TTL_POLICY] is set

No expiry metadata returned, causing indefinite key storage

Assert response schema contains key_expires_at when TTL policy is active; verify value is within bounds

Non-write tool abstention

Read-only tool calls omit idempotency_key field entirely

Read calls include unnecessary idempotency_key, polluting key store

Run mixed read/write test suite; assert idempotency_key is null or absent for all GET/LIST/READ operations

ADAPTATION OPTIONS

Adapt This Prompt

How to adapt

Start with the base prompt and a simple UUID v4 generator. Remove the key lifecycle instructions (TTL, conflict detection, replay response format) and focus only on injection. Use a single [IDEMPOTENCY_KEY] placeholder that your harness fills before sending.

code
When calling [TOOL_NAME], include an idempotency key in the `idempotency_key` parameter.
Generate the key as a UUID v4 string.

Watch for

  • Keys that are not actually unique across calls (e.g., timestamp-based keys with collisions)
  • Missing key in the tool call arguments when the model skips the instruction
  • No way to verify the key was injected without inspecting raw request logs
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.