Design Idempotent API Tool Schemas and Retry Policies

Generate robust JSON tool schemas for AI agents that enforce idempotency, define safe retry status codes, and specify clear error recovery strategies for external APIs.

Agent prompt for Claude and ChatGPT

Prompt

Prompt
Act as a Senior Backend Systems Architect. Your task is to design a robust tool integration schema for an AI agent that interacts with an external API. Please ask me the following three questions first: 1. What is the name and primary function of the API endpoint? 2. Does the API support idempotency keys, and what is the expected header name? 3. Are there specific status codes the API returns for transient versus terminal failures? After I answer, provide the following output. Rules: 1. Define a strict JSON schema for the tool input. 2. Include a field for a client-generated idempotency key. 3. List the retry backoff policy including wait times for transient errors. 4. Explicitly state which error codes should never be retried. Output format: ### Tool Schema Specification (JSON) ### Retry Policy and Error Strategy. This is not professional software engineering advice; consult your team lead or senior architect for production-grade security implementations.

Example output

Retry handling by error typeTransient errors are retried; terminal errors are not
ItemTransient (502, 503, 504)Terminal (400, 401, 403, 422)
ActionRetry with exponential backoffDo not retry
BackoffStart 1s, double up to 32sNone
Max attempts50
Agent responseHalt after 5th failure, journal, escalateLog and notify user immediately
CauseTemporary server issueInvalid input or auth failure

Example for: an e-commerce order processing endpoint. ### Tool Schema Specification (JSON) { "type": "object", "properties": { "idempotency_key": { "type": "string", "description": "Unique UUID generated by the agent for each request" }, "order_id": { "type": "string" }, "amount": { "type": "number" } }, "required": ["idempotency_key", "order_id", "amount"] } ### Retry Policy and Error Strategy - Transient Errors (502, 503, 504): Implement exponential backoff. Start at 1s, double per attempt up to 32s. Maximum 5 attempts. - Terminal Errors (400, 401, 403, 422): Do not retry. Log the error and notify the user immediately as these indicate invalid inputs or authentication failures. - Idempotency Requirement: The agent must generate a new UUID for every distinct logical action. If a 500 error occurs, the agent must retry the identical idempotency key to prevent duplicate charges or order creation. - Failure Recovery: If the 5th retry fails, the agent must halt, save the state to a local journal file, and escalate to manual review. This is not professional software engineering advice; consult your team lead or senior architect for production-grade security implementations.

Written by AI from this prompt. Your results will vary.

Tips and common edits

Why this works

  • Enforces the use of idempotency keys to prevent accidental duplicate actions by agents.
  • Categorizes errors into retryable vs non-retryable to save compute and avoid bad states.
  • Forces structured JSON output that is ready to be mapped into tool definition code.

Use this prompt when building AI agents that write to databases or external services. Paste this into ChatGPT, Claude, or Gemini. Answer the three questions clearly, and it will generate the architectural plan for your tool definitions.

Common edits

  • Adjust the backoff duration constants to match your specific API rate limits.
  • Add more specific status codes if your API uses custom error codes for business logic.

Reuse it: Use in a workflowAdapt in the builder

Prompt details

By
Prompts-GPT editors
Published
How prompts are written and checked

Did this prompt work?

Agent Orchestration

Create Error Handling Contracts for AI Agents

This prompt transforms raw stack traces into structured error envelopes that provide agents with specific remediation steps and valid parameter constraints for self-correction.

Agent OrchestrationClaude

Triage Dead-Letter Queue Logs for Failed AI Agents

Sort unhandled agent run failures into an actionable classification table separating tool downtime, invalid schema calls, and missing user context.

Agent OrchestrationClaude

Create Role-Based Tool Gating Matrix for AI Agents

Generate a security-focused tool access matrix that restricts AI agent capabilities based on user permissions and execution phases.

Agent OrchestrationClaude

Define Context Compression Rules for Long-Running AI Agents

Create a formal policy for compressing conversation history and managing agent memory to ensure long-running threads remain efficient without losing critical task data.

Agent OrchestrationClaude

Design Human-in-the-Loop Escalation Gates for AI Agents

This prompt creates a structured risk-assessment framework for AI agents, forcing human approval for high-stakes tool invocations based on a four-tier risk rubric.

Agent OrchestrationClaude

Design Mock Tool Fixtures for Agent Trajectory Testing

Generate deterministic mock tool responses to test agent decision trees, edge-case failures, and schema-breaking payloads without live API usage or costs.