Back to cookbook

AI Prompt to Design an API Error-Handling and Retry Strategy

9 views Updated

Make this prompt yours

Share

This AI prompt for error handling helps engineers design a consistent retry and failure strategy for an API client or backend service before writing any code. It's built for backend developers, platform engineers, and anyone integrating with a third-party or internal API who needs to decide how to handle timeouts, rate limits, and transient failures instead of bolting on ad-hoc try/catch blocks later.

Instead of asking a model to "add error handling," which tends to produce generic try/catch wrappers, this prompt asks it to reason through failure modes first: which errors are retryable (503s, timeouts, connection resets) versus which are not (4xx validation errors, auth failures), what backoff strategy fits the call pattern, and where circuit breakers or idempotency keys matter. The output is a structured plan plus implementation code, so the reasoning is visible and reviewable rather than hidden inside generated code.

Because the resulting strategy often ends up as reusable logic spread across multiple endpoints or services, it's worth running the finished prompt output through Prompt Optimizer afterward if you plan to reuse this prompt as a template for other API integrations, since its Coding mode tightens vague instructions into something you can drop into future requests with less rewriting.

Prompt template

Make this prompt yours

prompt-template
272 tokens
Role: You are a backend engineer designing a resilient API error-handling and retry strategy. Context: - API being called: [API_NAME_OR_DESCRIPTION] - Language/framework: [LANGUAGE_OR_FRAMEWORK] - Known behavior: [RATE_LIMITS_TIMEOUTS_ERROR_CODES] - Call pattern: [SINGLE_REQUEST_OR_BATCH_OR_HIGH_VOLUME] - Idempotency: [IS_THE_OPERATION_SAFE_TO_RETRY] Task: 1. Classify likely failure modes for this API call into retryable and non-retryable categories. 2. Recommend a specific retry strategy (backoff type, max attempts, max total wait, jitter) for the retryable category. 3. Recommend how non-retryable errors should be handled (fail fast, surface to caller, alert). 4. Note where a circuit breaker or rate limit guard would help if call volume is high. 5. Provide implementation code in [LANGUAGE_OR_FRAMEWORK] that applies this strategy to the described call. Constraints: - Do not retry non-retryable errors. - Include logging at each retry attempt and final failure. - Keep the retry logic isolated so it can be reused across multiple API calls. Output format: - A short table of failure modes (error type, retryable: yes/no, handling) - The recommended retry parameters - The implementation code in a fenced code block

Want it sharper? Optimize this prompt with Prompt Optimizer, check it with the Prompt Debugger or shorten it with the Token Optimizer.

Example input

example-input
228 tokens
Role: You are a backend engineer designing a resilient API error-handling and retry strategy. Context: - API being called: a third-party payment processor's REST API - Language/framework: Node.js with axios - Known behavior: returns 429 when rate limited, 503 during brief outages, 400 for invalid request bodies, times out after 10 seconds on their end - Call pattern: single request per checkout event, roughly 50 per minute at peak - Idempotency: the API accepts an idempotency key header, so retries are safe Task: 1. Classify likely failure modes into retryable and non-retryable categories. 2. Recommend a specific retry strategy for the retryable category. 3. Recommend how non-retryable errors should be handled. 4. Note where a circuit breaker or rate limit guard would help. 5. Provide implementation code in Node.js with axios that applies this strategy. Constraints: - Do not retry non-retryable errors. - Include logging at each retry attempt and final failure. - Keep the retry logic isolated so it can be reused across multiple API calls.

Example output

example-output
512 tokens

Failure mode classification

| Error | Retryable | Handling | |---|---|---| | 429 (rate limited) | Yes | Backoff and retry, respect Retry-After header if present | | 503 (temporary outage) | Yes | Exponential backoff with jitter | | Network timeout/reset | Yes | Exponential backoff with jitter | | 400 (invalid request) | No | Fail fast, surface validation error to caller | | 401/403 (auth) | No | Fail fast, alert on-call, do not retry |

Recommended retry parameters

  • Strategy: exponential backoff with full jitter
  • Base delay: 500ms, doubling each attempt
  • Max attempts: 4
  • Max total wait: 8 seconds before giving up
  • Always send the same idempotency key across retries of the same checkout event

Circuit breaker note At 50 requests/minute, add a circuit breaker that opens after 5 consecutive failures within 30 seconds, preventing retries from piling up during a sustained outage, and closes after a 60-second cooldown.

async function callPaymentApi(payload, idempotencyKey, maxAttempts = 4) {
const baseDelayMs = 500;
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
try {
const response = await axios.post('https://api.payments.example/charge', payload, {
headers: { 'Idempotency-Key': idempotencyKey },
timeout: 10000,
});
return response.data;
} catch (err) {
const status = err.response?.status;
const retryable = status === 429 || status === 503 || err.code === 'ECONNRESET' || err.code === 'ETIMEDOUT';
if (!retryable || attempt === maxAttempts) {
console.error(`Payment call failed permanently on attempt ${attempt}`, { status, message: err.message });
throw err;
}
const retryAfter = err.response?.headers?.['retry-after'];
const delay = retryAfter ? Number(retryAfter) * 1000 : baseDelayMs * 2 ** (attempt - 1) * (0.5 + Math.random());
console.warn(`Payment call failed on attempt ${attempt}, retrying in ${Math.round(delay)}ms`, { status });
await new Promise((resolve) => setTimeout(resolve, delay));
}
}
}

When to use it

  • Before building a client that calls a flaky or rate-limited third-party API
  • When a service keeps failing silently and you need a defined retry policy instead of guesswork
  • When designing a new microservice-to-microservice call and want error handling decided upfront
  • When a code review flagged inconsistent or missing error handling across endpoints

Best practices

  • State the API's actual behavior (rate limits, timeout thresholds, documented error codes) so the model doesn't invent generic assumptions
  • Specify whether calls need to be idempotent, since that changes whether retries are safe at all
  • Ask for a distinction between retryable and non-retryable errors explicitly, not just a blanket retry loop
  • Run the generated retry logic through Prompt Debugger if you're turning this into a reusable prompt, to catch edge cases like retry storms or missing jitter before you rely on it repeatedly

Common mistakes

  • Retrying every failed request the same way, including errors that will never succeed on retry (like 400 or 401)
  • Using fixed-delay retries instead of exponential backoff with jitter, which can cause thundering-herd spikes
  • Forgetting to cap total retry attempts or total retry duration, leading to requests that hang indefinitely
  • Not asking for logging or observability hooks, so failures are handled but invisible to monitoring

FAQs

How do I know which API errors are safe to retry?

As a general rule, 5xx server errors, timeouts, and connection resets are usually safe to retry because they often indicate a transient problem. 4xx client errors like 400 (bad request) or 401 (unauthorized) are not safe to retry as-is, since the same request will fail the same way every time until the underlying issue (bad payload, expired token) is fixed.

What's the difference between exponential backoff and fixed-delay retries?

Fixed-delay retries wait the same amount of time between every attempt, which can cause many clients to retry at the same moment and overwhelm a recovering service. Exponential backoff increases the delay after each failed attempt (and adding jitter randomizes it slightly), spreading retries out over time and reducing the chance of a retry storm.

Should I always retry a failed API call automatically?

No. Automatic retries only make sense when the operation is idempotent (safe to repeat) and the failure is likely transient. For non-idempotent operations without a safeguard like an idempotency key, an automatic retry can cause duplicate side effects, such as charging a customer twice.

Which Cuelara tool can help me turn this into a reusable prompt for other APIs?

Prompt Optimizer — its Coding mode is built to tighten vague or one-off coding prompts like this into a structured template you can reuse across different API integrations without rewriting it from scratch each time.

Found this prompt useful? Share it.

Share

More in Engineering

Engineering

AI Prompt to Write a Rollback Plan for a Risky Deployment

This AI prompt for a deployment rollback plan helps engineers document exactly how to reverse a risky release before it ships, not after som…

ROLE: You are a senior site reliability engineer writing a rollback plan for a production deployment.

CONTEXT:
- Service or system being deployed: [SERVICE NAME]
- What the deployment changes: [CODE CHANGES, CONFIG CHANGES, DATABASE MIGRATIONS, ETC.]
- Deployment method: [CI/CD PIPELINE, MANUAL DEPLOY, FEATURE FLAG ROLLOUT]
- Dependent services or consumers affected: [LIST DEPENDENT SERVICES]

TASK:
Write a rollback plan for this deployment that includes:
1. The specific monitoring signals or alerts that indicate a rollback is needed
2. Step-by-step rollback instructions in execution order, each with an owner
3. Any step that is irreversible or partially irreversible, flagged separately
4. A verification step to confirm the rollback succeeded
5. An estimated time to complete the rollback

CONSTRAINTS:
- Assume the person executing the rollback may not be the person who wrote the plan
- Do not assume manual database fixes are safe without naming the exact commands or scripts
- Keep each step to one action

OUTPUT FORMAT:
A numbered list of rollback steps, followed by a short "Irreversible Actions" section and a "Verification" section.

Make this prompt yours

Engineering

Secure API Request Handler

A prompt that makes the model write an API route the way a security conscious reviewer would want it: validated input, safe data access, uni…

Write a Node.js Express route handler for a [METHOD] request to '[PATH]'.

Requirements:
- Validate the body with Zod: [FIELDS AND CONSTRAINTS].
- Use parameterized queries / the ORM only. No string-concatenated SQL.
- Wrap the logic in try/catch.
- Return standardized errors: { "error": { "code": string, "message": string } } with 400 for validation failures and 500 for server errors. Never leak stack traces.
- Add a short comment above any security-relevant line.

Return only the code.

Make this prompt yours

Engineering

Senior React Developer Persona

This is a system prompt that turns a general purpose model into a disciplined senior frontend engineer. Instead of asking for "a React compo…

You are a Senior Frontend Engineer specializing in React, Next.js (App Router) and Tailwind CSS.

Rules:
1. Use functional components with TypeScript. Export a typed props interface.
2. Prefer Tailwind utility classes over custom CSS.
3. Use Framer Motion for micro-interactions only; respect prefers-reduced-motion.
4. Make every interactive element keyboard accessible with correct ARIA attributes.
5. Respond with ONLY the code block. No explanations unless I ask.

Task: [DESCRIBE THE COMPONENT]

Make this prompt yours