Resilience (Python)
Resilience Patterns
Section titled “Resilience Patterns”ai-lib-python (v1.1.0) separates built-in client backpressure from opt-in policy primitives:
AiClient: optionalmax_inflightbackpressure viaAiClientBuilderorAI_LIB_MAX_INFLIGHT.ai_lib_python.resilience: retry policies, token-bucket rate limiter, circuit breaker — not wired automatically byAiClient.create(); useproduction_ready()or explicitResilientConfig(seeexamples/resilience.py).
Retry and fallback decisions use V2 standard error codes: retryable and fallbackable metadata on normalized errors.
Quick enable
Section titled “Quick enable”client = await ( AiClient.builder() .model("deepseek/deepseek-chat") .production_ready() # ResilientConfig.production() .build())Circuit breaker
Section titled “Circuit breaker”Prevents cascading failures by stopping requests to failing providers.
States: Closed → Open (after failure threshold) → Half-Open (test request after cooldown).
Configure via ResilientConfig / builder methods — not via undocumented env vars.
Rate limiter
Section titled “Rate limiter”Token-bucket rate limiting lives in ai_lib_python.resilience. Configure on the builder:
from ai_lib_python.resilience import RateLimitConfig
client = await ( AiClient.builder() .model("openai/gpt-4o") .with_rate_limit(RateLimitConfig(requests_per_second=10)) .build())AI_LIB_RPS / AI_LIB_RPM environment variables are not read by the runtime.
Backpressure
Section titled “Backpressure”Limits concurrent in-flight requests:
export AI_LIB_MAX_INFLIGHT=50Or on the builder: .max_inflight(50).
Exponential backoff retry driven by manifest retry_policy and ResilientConfig. Only errors classified as retryable trigger retries.
Combining patterns
Section titled “Combining patterns”A typical request flow when production_ready() is enabled:
- Backpressure — wait for a slot if at max inflight
- Circuit breaker — reject immediately if circuit is open
- Rate limiter — wait for a token if rate limited
- Execute — send the HTTP request via pipeline
- Retry — on retryable errors, backoff and retry
- Update — record success/failure for circuit breaker
Next steps
Section titled “Next steps”- Advanced Features — Telemetry, routing, plugins
- AiClient API — Client usage