ElixirLLM.Retry (ElixirLLM v0.3.0)

Copy Markdown View Source

Retry mechanism with exponential backoff for LLM API calls.

Configuration

config :elixir_llm, :retry,
  max_attempts: 3,
  base_delay_ms: 1000,
  max_delay_ms: 30_000,
  jitter: true

Example

# With default retry settings
ElixirLLM.new()
|> ElixirLLM.with_retry()
|> ElixirLLM.ask("Hello!")

# With custom retry settings
ElixirLLM.new()
|> ElixirLLM.with_retry(max_attempts: 5, base_delay_ms: 500)
|> ElixirLLM.ask("Hello!")

Retry Behavior

By default, the following errors are retried:

  • Rate limit errors (429)
  • Server errors (5xx)
  • Network errors
  • Timeout errors

Non-retryable errors (authentication, validation) are returned immediately.

Summary

Functions

Calculates the delay for a given retry attempt using exponential backoff.

Returns true if the error should trigger a retry.

Executes a function with retry logic.

Types

retry_opts()

@type retry_opts() :: [
  max_attempts: pos_integer(),
  base_delay_ms: pos_integer(),
  max_delay_ms: pos_integer(),
  jitter: boolean(),
  on_retry: (integer(), term() -> any())
]

Functions

calculate_delay(attempt, config)

@spec calculate_delay(pos_integer(), map()) :: non_neg_integer()

Calculates the delay for a given retry attempt using exponential backoff.

should_retry?(arg1)

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

Returns true if the error should trigger a retry.

with_retry(fun, opts \\ [])

@spec with_retry((-> {:ok, term()} | {:error, term()}), retry_opts()) ::
  {:ok, term()} | {:error, term()}

Executes a function with retry logic.

Options

  • :max_attempts - Maximum number of attempts (default: 3)
  • :base_delay_ms - Base delay between retries in milliseconds (default: 1000)
  • :max_delay_ms - Maximum delay between retries (default: 30000)
  • :jitter - Add random jitter to delays (default: true)
  • :on_retry - Callback called before each retry: (attempt, error) -> any()

Example

ElixirLLM.Retry.with_retry(fn ->
  provider.chat(chat)
end, max_attempts: 5)