LemonAi.Providers.RetryHelper (lemon_ai v0.1.0)

View Source

Shared retry utilities for AI provider implementations.

Provides exponential backoff with jitter, retryable HTTP status detection, transport error classification, and error text pattern matching.

Summary

Functions

Compute exponential backoff delay with jitter.

Extract a provider retry delay from headers or error text.

Check whether an error text matches known retryable patterns via regex.

Check whether an HTTP status code is retryable.

Check whether a transport error is retryable.

Check whether a transport reason term is retryable.

Functions

exponential_backoff_with_jitter(base_ms, attempt)

@spec exponential_backoff_with_jitter(pos_integer(), non_neg_integer()) ::
  pos_integer()

Compute exponential backoff delay with jitter.

Returns a random delay between base * 2^attempt / 2 and base * 2^attempt.

extract_retry_delay_ms(error_text, headers \\ %{})

@spec extract_retry_delay_ms(String.t(), map() | list()) :: non_neg_integer() | nil

Extract a provider retry delay from headers or error text.

Returns milliseconds with a 1s buffer, matching the existing provider retry behavior for retry-after, x-ratelimit-reset-after, retryDelay, and common text retry hints.

retryable_error_text?(error_text)

@spec retryable_error_text?(String.t()) :: boolean()

Check whether an error text matches known retryable patterns via regex.

Matches: rate limit, overloaded, service unavailable, upstream connect, connection refused, resource exhausted, other side closed.

retryable_http_status?(status)

@spec retryable_http_status?(integer()) :: boolean()

Check whether an HTTP status code is retryable.

Uses the comprehensive default list when called with one argument. Pass an explicit list of statuses as second argument to override.

retryable_http_status?(status, statuses)

@spec retryable_http_status?(integer(), [integer()]) :: boolean()

retryable_transport_error?(reason)

@spec retryable_transport_error?(term()) :: boolean()

Check whether a transport error is retryable.

Accepts a Req.TransportError struct or a raw reason term.

retryable_transport_reason?(reason)

@spec retryable_transport_reason?(term()) :: boolean()

Check whether a transport reason term is retryable.