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: 3Example
# 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
@type provider() :: atom()
@type state() :: :closed | :open | :half_open
Functions
Executes a function with circuit breaker protection.
Returns {:error, :circuit_open} if the circuit is open.
Returns a specification to start this module under a supervisor.
See Supervisor.
@spec record_failure(provider()) :: :ok
Records a failed call (for external use when not using call/2).
@spec record_success(provider()) :: :ok
Records a successful call (for external use when not using call/2).
@spec reset(provider()) :: :ok
Resets the circuit breaker for a provider to closed state.
@spec reset_all() :: :ok
Resets all circuit breakers.
Starts the circuit breaker.
Returns the current state of the circuit for a provider.