anthropic/retry
Retry logic with exponential backoff for transient failures
This module provides automatic retry functionality for API requests that encounter transient errors like rate limits, server overload, or timeouts.
Features
- Exponential backoff with configurable base delay
- Jitter to prevent thundering herd
- Configurable max retries
- Respects retry-after headers when available
Example
let retry_config = default_retry_config()
|> with_max_retries(5)
|> with_base_delay_ms(500)
let result = retry_with_backoff(retry_config, fn() {
api.create_message(client, request)
})
Types
Configuration for retry behavior
pub type RetryConfig {
RetryConfig(
max_retries: Int,
base_delay_ms: Int,
max_delay_ms: Int,
jitter_factor: Float,
backoff_multiplier: Float,
)
}
Constructors
-
RetryConfig( max_retries: Int, base_delay_ms: Int, max_delay_ms: Int, jitter_factor: Float, backoff_multiplier: Float, )Arguments
- max_retries
-
Maximum number of retry attempts (0 = no retries)
- base_delay_ms
-
Base delay in milliseconds for exponential backoff
- max_delay_ms
-
Maximum delay in milliseconds (cap for exponential growth)
- jitter_factor
-
Jitter factor (0.0 to 1.0) - adds randomness to prevent thundering herd
- backoff_multiplier
-
Multiplier for exponential backoff (typically 2.0)
Result of a retry operation with metadata
pub type RetryResult(a) {
RetryResult(
result: Result(a, error.AnthropicError),
attempts: Int,
total_delay_ms: Int,
)
}
Constructors
-
RetryResult( result: Result(a, error.AnthropicError), attempts: Int, total_delay_ms: Int, )Arguments
- result
-
The final result (success or last error)
- attempts
-
Number of attempts made
- total_delay_ms
-
Total time spent on retries in milliseconds
Values
pub fn aggressive_retry_config() -> RetryConfig
Create an aggressive retry configuration for high availability
pub fn calculate_delay(config: RetryConfig, attempt: Int) -> Int
Calculate delay for a given attempt number (0-indexed)
pub fn calculate_delay_with_retry_after(
config: RetryConfig,
attempt: Int,
retry_after_ms: option.Option(Int),
) -> Int
Calculate delay with optional retry-after header value
pub fn default_retry_config() -> RetryConfig
Create a default retry configuration
Defaults:
- max_retries: 3
- base_delay_ms: 1000 (1 second)
- max_delay_ms: 60000 (60 seconds)
- jitter_factor: 0.25 (25% randomness)
- backoff_multiplier: 2.0
pub fn get_retry_after_from_error(
error: error.AnthropicError,
) -> option.Option(Int)
Get suggested retry delay from an error (if available)
pub fn max_total_delay(config: RetryConfig) -> Int
Get the maximum possible delay for a configuration
pub fn no_retry_config() -> RetryConfig
Create a retry configuration with no retries
pub fn retries_enabled(config: RetryConfig) -> Bool
Check if retry configuration allows retries
pub fn retry(
config: RetryConfig,
operation: fn() -> Result(a, error.AnthropicError),
) -> Result(a, error.AnthropicError)
Execute with retry and return just the result
pub fn retry_with_backoff(
config: RetryConfig,
operation: fn() -> Result(a, error.AnthropicError),
) -> RetryResult(a)
Execute a function with retry logic
Returns the first successful result, or the last error after all retries are exhausted.
pub fn should_retry_error(error: error.AnthropicError) -> Bool
Check if an error should be retried based on custom logic
pub fn with_backoff_multiplier(
config: RetryConfig,
multiplier: Float,
) -> RetryConfig
Set the backoff multiplier
pub fn with_base_delay_ms(
config: RetryConfig,
base_delay_ms: Int,
) -> RetryConfig
Set the base delay in milliseconds
pub fn with_jitter_factor(
config: RetryConfig,
jitter_factor: Float,
) -> RetryConfig
Set the jitter factor (0.0 to 1.0)
pub fn with_max_delay_ms(
config: RetryConfig,
max_delay_ms: Int,
) -> RetryConfig
Set the maximum delay in milliseconds
pub fn with_max_retries(
config: RetryConfig,
max_retries: Int,
) -> RetryConfig
Set the maximum number of retries