Skip to content

Resilience (Python)

ai-lib-python includes a comprehensive resilience system centered around the ResilientExecutor.

Combines all reliability patterns into a single executor:

from ai_lib_python.resilience import (
ResilientConfig, RetryConfig, RateLimiterConfig,
CircuitBreakerConfig, BackpressureConfig
)
config = ResilientConfig(
retry=RetryConfig(
max_retries=3,
initial_delay=1.0,
max_delay=30.0,
backoff_multiplier=2.0,
),
rate_limiter=RateLimiterConfig(
requests_per_second=10,
),
circuit_breaker=CircuitBreakerConfig(
failure_threshold=5,
cooldown_seconds=30,
),
backpressure=BackpressureConfig(
max_inflight=50,
),
)
client = await AiClient.builder() \
.model("openai/gpt-4o") \
.resilience(config) \
.build()
from ai_lib_python.resilience import CircuitBreaker
breaker = CircuitBreaker(
failure_threshold=5,
cooldown_seconds=30,
)
# Check state
print(breaker.state) # "closed", "open", "half_open"

Token bucket algorithm:

from ai_lib_python.resilience import RateLimiter
limiter = RateLimiter(
requests_per_second=10,
burst_size=20,
)

Concurrency limiting:

from ai_lib_python.resilience import Backpressure
bp = Backpressure(max_inflight=50)

Multi-target failover:

from ai_lib_python.resilience import FallbackChain
chain = FallbackChain([
"openai/gpt-4o",
"anthropic/claude-3-5-sonnet",
"deepseek/deepseek-chat",
])

Unified gating before request execution:

from ai_lib_python.resilience import PreflightChecker
checker = PreflightChecker()
# Checks circuit state, rate limits, inflight count
# before allowing a request through

Aggregated runtime state:

signals = client.signals_snapshot()
print(f"Circuit: {signals.circuit_state}")
print(f"Inflight: {signals.current_inflight}")
print(f"Rate remaining: {signals.rate_remaining}")
VariablePurpose
AI_LIB_RPSRate limit (requests per second)
AI_LIB_BREAKER_FAILURE_THRESHOLDCircuit breaker threshold
AI_LIB_BREAKER_COOLDOWN_SECSCooldown period
AI_LIB_MAX_INFLIGHTMax concurrent requests