Study FAQ
How do I retry Claude API calls without duplicating work or exhausting the caller's deadline?
Also asked as 2
- What retry, backoff, and idempotency controls should I put around Claude Messages calls?
- How do I prevent SDK retries and my own retry loop from creating duplicate Claude attempts?
Answer
Retry only an eligible transient failure while the caller has budget and the workflow can reconcile another attempt. A repeat Messages request can generate a different result and incur additional usage. Use an application operation ID, separate attempt IDs, and idempotent downstream processing so repeated generations cannot produce repeated business actions.
- Bound the full operation. Choose one policy owner. Check your client version's retry defaults before adding an outer retry loop. Anthropic documents two retries by default for supported SDKs, with exceptions such as some Go create methods. Set the SDK retry setting explicitly when your application owns the retry loop. The SDK does not automatically retry an error inside an already-started stream.
- Use service signals. Honor
x-should-retrywhen present and wait at least the validretry-afterinterval. Otherwise use capped exponential backoff with jitter for eligible failures. Stop on caller cancellation, a deadline, or an exhausted attempt budget. - Pace shared traffic. Feed documented limit, remaining-capacity, and reset headers into monitoring. Limit concurrent calls, requests, and estimated input and output tokens at the organization, workspace, or model scope that actually shares the quota. Coordinate workers so they do not retry in waves.
- Reconcile uncertain work. Record how far the previous request progressed. Persist operation state before consequential effects, accept at most the current validated result, and deduplicate downstream writes with a stable operation key. Keep the API request ID for correlation when present. The Messages contract does not document the request ID or your operation ID as a server deduplication key.
If you cannot tell whether an earlier attempt completed or whether it already reached a consumer, resolve that ambiguity before allowing a repeated side effect. Use a compatible evaluated fallback or return a controlled failure when the budget runs out.
Official references
Full explanation
Reliable Claude API Streaming, Asynchronous Work, and Error Handling
Study guide · CCDV-F
Read the full explanation