ElixirLLM.CircuitBreaker (ElixirLLM v0.3.0)

Copy Markdown View Source

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)

Summary

Functions

Executes a function with circuit breaker protection.

Returns a specification to start this module under a supervisor.

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

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

Resets the circuit breaker for a provider to closed state.

Resets all circuit breakers.

Starts the circuit breaker.

Returns the current state of the circuit for a provider.

Types

provider()

@type provider() :: atom()

state()

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

Functions

call(provider, fun)

@spec call(provider(), (-> {: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(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

record_failure(provider)

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

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

record_success(provider)

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

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

reset(provider)

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

Resets the circuit breaker for a provider to closed state.

reset_all()

@spec reset_all() :: :ok

Resets all circuit breakers.

start_link(opts \\ [])

Starts the circuit breaker.

state(provider)

@spec state(provider()) :: state()

Returns the current state of the circuit for a provider.