Resilience (Rust)
Resilience Patterns
Section titled “Resilience Patterns”ai-lib-rust includes production-grade reliability patterns out of the box.
Circuit Breaker
Section titled “Circuit Breaker”Prevents cascading failures by stopping requests to failing providers:
States:
- Closed — Normal operation, requests flow through
- Open — Too many failures, requests immediately rejected
- Half-Open — After cooldown, allows a test request
Configuration:
export AI_LIB_BREAKER_FAILURE_THRESHOLD=5export AI_LIB_BREAKER_COOLDOWN_SECS=30The circuit opens after FAILURE_THRESHOLD consecutive failures and stays open for COOLDOWN_SECS before testing.
Rate Limiter
Section titled “Rate Limiter”Token bucket algorithm prevents exceeding provider rate limits:
export AI_LIB_RPS=10 # Max requests per secondexport AI_LIB_RPM=600 # Max requests per minuteRequests beyond the limit are queued rather than rejected, providing smooth throughput.
Backpressure
Section titled “Backpressure”Limits concurrent in-flight requests with a semaphore:
export AI_LIB_MAX_INFLIGHT=50When the limit is reached, new requests wait until a slot opens.
Exponential backoff retry driven by the protocol manifest’s retry policy:
# In the provider manifestretry_policy: strategy: "exponential_backoff" max_retries: 3 initial_delay_ms: 1000 max_delay_ms: 30000 retryable_errors: - "rate_limited" - "overloaded" - "server_error"Only errors classified as retryable trigger retries. Authentication errors, for example, fail immediately.
Combining Patterns
Section titled “Combining Patterns”All resilience patterns work together. A typical request flow:
- 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 request
- Retry — If retryable error, wait and retry
- Update — Record success/failure for circuit breaker
Observability
Section titled “Observability”Monitor resilience state at runtime:
// Check circuit breaker statelet state = client.circuit_state();println!("Circuit: {:?}", state); // Closed, Open, HalfOpen
// Check current inflight countlet inflight = client.current_inflight();Next Steps
Section titled “Next Steps”- Advanced Features — Embeddings, cache, plugins
- AiClient API — Client usage