Prompt
How do I make a workflow retry safely when a downstream API fails?
Latest observation
Oct 24, 2026
OpenAI APIWeb search: off
To make a workflow retry safely when a downstream API fails, the key is to ensure retries are idempotent and bounded.
Core principles
-
Make the operation idempotent
- A retry should not create duplicate side effects.
- Use a unique request ID / idempotency key for each logical operation.
- The downstream API should deduplicate based on that key if possible.
-
Retry only transient failures
- Retry on timeouts, connection resets, 429s, and 5xx responses.
- Do not blindly retry on 4xx errors like validation failures or unauthorized access.
-
Use exponential backoff with jitter
- Example: 1s, 2s, 4s, 8s, with random jitter.
- This avoids overwhelming the downstream service and reduces retry storms.
-
Limit the number of attempts
- Set a max retry count or total retry window.
- After that, fail the workflow or send it to a dead-letter queue / manual review.
-
Persist workflow state
- Store the step status before retrying so the workflow can resume safely after a crash or restart.
- Avoid “in-memory only” retry logic.
-
Separate side effects from orchestration
- If possible, write the intent first, then perform the external call, then mark completion.
- Use a saga/compensation pattern if the workflow involves multiple side effects.
-
Handle duplicate callbacks / responses
- If the API is asynchronous, the same callback or event may arrive more than once.
- Deduplicate using the same operation ID.
A simple safe retry pattern
- Generate
operation_id - Save
operation_idand state =pending - Call downstream API with
Idempotency-Key: operation_id - If it fails with a retryable error:
- wait with backoff
- retry
- If it succeeds:
- mark state =
completed
- mark state =
- If retries exhaust:
- mark state =
failed - alert or move to manual handling
- mark state =
Example retry policy
- Retry on: network errors, timeouts, HTTP 429, HTTP 500–599
- Max attempts: 5
- Backoff: exponential, capped at 30 seconds
- Jitter: random 0–50% of delay
Important caution
If the downstream API does not support idempotency keys, you need your own deduplication mechanism, such as:
- storing the request result keyed by operation ID,
- checking whether the action already completed before retrying,
- or using a transactional outbox / inbox pattern.
If you want, I can also show:
- a code example in Python/JavaScript,
- a workflow example for Airflow/Temporal/Step Functions,
- or a design for exactly-once-ish retries with idempotency keys.