Agentic.CircuitBreaker (agentic v0.2.2)

Copy Markdown

Per-tool circuit breaker for agent tool execution.

Tracks consecutive failures per tool name. After a configurable threshold (default 3), the tool is marked as "open" and calls are rejected instantly for a cooldown period (default 5 minutes).

State transitions: :closed (normal) -> 3 consecutive failures -> :open (rejecting) :open -> cooldown expires -> :half_open (testing) :half_open -> success -> :closed :half_open -> failure -> :open

Uses ETS for lock-free reads on the hot path. No GenServer needed.

Summary

Functions

Check if a tool is available (circuit closed or half-open).

Get the current state of a tool's circuit breaker.

Initialize the ETS table. Call once at app startup.

Record a failed tool execution. May trip the circuit.

Record a successful tool execution. Resets failure count.

Reset a specific tool's circuit breaker.

Reset all circuit breakers.

Types

state()

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

Functions

check(tool_name)

@spec check(String.t()) :: :ok | {:error, :circuit_open}

Check if a tool is available (circuit closed or half-open).

get_state(tool_name)

@spec get_state(String.t()) :: state()

Get the current state of a tool's circuit breaker.

init()

Initialize the ETS table. Call once at app startup.

record_failure(tool_name)

@spec record_failure(String.t()) :: :ok

Record a failed tool execution. May trip the circuit.

record_success(tool_name)

@spec record_success(String.t()) :: :ok

Record a successful tool execution. Resets failure count.

reset(tool_name)

@spec reset(String.t()) :: :ok

Reset a specific tool's circuit breaker.

reset_all()

@spec reset_all() :: :ok

Reset all circuit breakers.