AI Wisdom

Circuit Breaker for LLMs

Prevent cascading failures by temporarily stopping requests to a failing LLM provider and automatically recovering when it stabilises.

In this article

Problem: When an LLM provider degrades, your application hammers the failing endpoint, exhausting connection pools, inflating costs, and degrading all users โ€” not just those on the affected model.

Solution: Apply the Circuit Breaker pattern: track provider health; when failure rate exceeds a threshold, open the circuit (stop sending requests) for a cooldown period; then probe to test recovery.

States:

  • Closed (normal): requests flow through; track failure rate
  • Open (tripped): immediately return fallback response; no provider calls
  • Half-Open (probing): allow a small % of requests through to test recovery

Implementation:

  1. Track rolling 60-second window: request count + failure count per provider
  2. Open circuit when error rate > 50% over at least 10 requests
  3. Open circuit for 30 seconds (configurable)
  4. In half-open state, allow 10% of requests; close if success rate normalises

Trade-Offs:

  • โœ” Pro: Prevents cascading failures and connection pool exhaustion
  • โœ” Pro: Fast-fail for users instead of hanging requests
  • โœ– Con: False trips during brief transient errors
  • โœ– Con: Requires tracking state in Redis or a shared cache (not per-process)

When To Use: Any system making more than ~100 LLM requests/day that needs high availability. When to avoid: Low-traffic internal tools where simple retry is sufficient.

Discussion

Sign in to share your feedback and join the discussion.