How Retries Work
Requests are automatically retried when the following HTTP status codes are returned:- 408 Request Timeout: The server timed out waiting for the request
- 429 Too Many Requests: Rate limit exceeded
- 5XX Server Errors: Internal server errors (500, 502, 503, 504, etc.)
The default retry limit is 2 attempts, meaning each request can be tried up to 3 times total (1 initial attempt + 2 retries).
Retry Strategy
The SDK uses a sophisticated retry strategy:- Exponential Backoff: Delay doubles after each retry (1s, 2s, 4s, 8s, etc.)
- Jitter: Random variation (±10%) prevents thundering herd problems
- Max Delay: Capped at 60 seconds to prevent excessive waiting
- Min Delay: Minimum 1 second between retries
- Header-Based Delays: Respects
Retry-AfterandX-RateLimit-Resetheaders
Configuring Retries Globally
Set the maximum number of retry attempts when creating the client:Disabling Retries
To disable retries entirely, set max attempts to 1:Configuring Retries Per Request
Override retry behavior for individual requests:Retry Delay Calculation
- Exponential Backoff
- Retry-After Header
- Rate Limit Header
When no retry headers are present, the SDK uses exponential backoff:Example delays:
- Retry 1: ~1 second (1s ± 10%)
- Retry 2: ~2 seconds (2s ± 10%)
- Retry 3: ~4 seconds (4s ± 10%)
- Retry 4: ~8 seconds (8s ± 10%)
- Retry 5: ~16 seconds (16s ± 10%)
Implementation Details
The retry logic is implemented in theinternal.Retrier type:
Context and Cancellation
Retries respect context cancellation:Request Body Handling
The SDK automatically resets the request body before each retry:Best Practices
Use default retries for most operations
The default retry configuration (2 attempts) works well for most use cases.
Increase retries for critical operations
For important operations that can tolerate longer waits, increase max attempts to 5 or more.
Disable retries for time-sensitive operations
Use
option.WithMaxAttempts(1) for operations that need immediate feedback.Idempotency Considerations
Always use idempotency keys for operations that modify state:Idempotency keys ensure that if a request is retried due to a network failure, the operation won’t be duplicated on the server.
When Retries Don’t Help
Retries are not attempted for:- 4XX errors (except 408 and 429): These indicate client errors that won’t be fixed by retrying
- Authentication errors (401): Invalid credentials need to be corrected
- Validation errors (400): The request data needs to be fixed
- Not found errors (404): The resource doesn’t exist
