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

Circuit breaker pattern for LLM API calls.

Prevents cascading failures by temporarily blocking requests to failing providers.

## States

  * `:closed` - Normal operation, requests pass through
  * `:open` - Failures exceeded threshold, requests are blocked
  * `:half_open` - Testing if service has recovered

## Configuration

    config :elixir_llm, :circuit_breaker,
      enabled: true,
      failure_threshold: 5,
      recovery_timeout_ms: 30_000,
      half_open_max_calls: 3

## Example

    # Execute with circuit breaker protection
    case ElixirLLM.CircuitBreaker.call(:openai, fn -> provider.chat(chat) end) do
      {:ok, response} -> handle_response(response)
      {:error, :circuit_open} -> handle_circuit_open()
      {:error, reason} -> handle_error(reason)
    end

    # Check circuit state
    state = ElixirLLM.CircuitBreaker.state(:openai)

# `provider`
[🔗](https://github.com/khasinski/elixir_llm/blob/v0.3.0/lib/elixir_llm/circuit_breaker.ex#L39)

```elixir
@type provider() :: atom()
```

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

```elixir
@type state() :: :closed | :open | :half_open
```

# `call`
[🔗](https://github.com/khasinski/elixir_llm/blob/v0.3.0/lib/elixir_llm/circuit_breaker.ex#L63)

```elixir
@spec call(provider(), (-&gt; {:ok, term()} | {:error, term()})) ::
  {:ok, term()} | {:error, term()}
```

Executes a function with circuit breaker protection.

Returns `{:error, :circuit_open}` if the circuit is open.

# `child_spec`
[🔗](https://github.com/khasinski/elixir_llm/blob/v0.3.0/lib/elixir_llm/circuit_breaker.ex#L34)

Returns a specification to start this module under a supervisor.

See `Supervisor`.

# `record_failure`
[🔗](https://github.com/khasinski/elixir_llm/blob/v0.3.0/lib/elixir_llm/circuit_breaker.ex#L133)

```elixir
@spec record_failure(provider()) :: :ok
```

Records a failed call (for external use when not using `call/2`).

# `record_success`
[🔗](https://github.com/khasinski/elixir_llm/blob/v0.3.0/lib/elixir_llm/circuit_breaker.ex#L124)

```elixir
@spec record_success(provider()) :: :ok
```

Records a successful call (for external use when not using `call/2`).

# `reset`
[🔗](https://github.com/khasinski/elixir_llm/blob/v0.3.0/lib/elixir_llm/circuit_breaker.ex#L104)

```elixir
@spec reset(provider()) :: :ok
```

Resets the circuit breaker for a provider to closed state.

# `reset_all`
[🔗](https://github.com/khasinski/elixir_llm/blob/v0.3.0/lib/elixir_llm/circuit_breaker.ex#L114)

```elixir
@spec reset_all() :: :ok
```

Resets all circuit breakers.

# `start_link`
[🔗](https://github.com/khasinski/elixir_llm/blob/v0.3.0/lib/elixir_llm/circuit_breaker.ex#L52)

Starts the circuit breaker.

# `state`
[🔗](https://github.com/khasinski/elixir_llm/blob/v0.3.0/lib/elixir_llm/circuit_breaker.ex#L95)

```elixir
@spec state(provider()) :: state()
```

Returns the current state of the circuit for a provider.

---

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