# `ElixirLLM.Retry`
[🔗](https://github.com/khasinski/elixir_llm/blob/v0.3.0/lib/elixir_llm/retry.ex#L1)

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.

# `retry_opts`
[🔗](https://github.com/khasinski/elixir_llm/blob/v0.3.0/lib/elixir_llm/retry.ex#L38)

```elixir
@type retry_opts() :: [
  max_attempts: pos_integer(),
  base_delay_ms: pos_integer(),
  max_delay_ms: pos_integer(),
  jitter: boolean(),
  on_retry: (integer(), term() -&gt; any())
]
```

# `calculate_delay`
[🔗](https://github.com/khasinski/elixir_llm/blob/v0.3.0/lib/elixir_llm/retry.ex#L126)

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

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

# `should_retry?`
[🔗](https://github.com/khasinski/elixir_llm/blob/v0.3.0/lib/elixir_llm/retry.ex#L103)

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

Returns true if the error should trigger a retry.

# `with_retry`
[🔗](https://github.com/khasinski/elixir_llm/blob/v0.3.0/lib/elixir_llm/retry.ex#L70)

```elixir
@spec with_retry((-&gt; {: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)

---

*Consult [api-reference.md](api-reference.md) for complete listing*
