joabgonzalez/ai-agents-skills/circuit-breaker-pattern
Claude Code and agent skill by joabgonzalez/ai-agents-skills
- community
Checking that the SKILL.md file is still in the repository.
Prevent cascading failures by short-circuiting failing services. Trigger: When calling external APIs, databases, or any unreliable dependency.
Install
npx vskill@latest install joabgonzalez/ai-agents-skills/circuit-breaker-patternSKILL.md
The same SKILL.md was published earlier as joabgonzalez/ai-agents-skills/circuit-breaker-pattern.
Circuit Breaker Pattern
Protects a system from cascading failures by monitoring calls to an external dependency. When failures exceed a threshold, the circuit "trips" — subsequent calls fail immediately without attempting the operation, giving the dependency time to recover.
Like an electrical circuit breaker: when current spikes (failures accumulate), the breaker opens to prevent damage. It then probes for recovery before restoring normal flow.
When to Use
- Calling external APIs, databases, third-party services, or microservices
- Any dependency that can be slow or unavailable — not just HTTP calls
- Systems where one failing service should not bring down the whole application
- High-traffic services where queuing up failed requests causes cascading load
Don't use for:
- Local in-process function calls (no network boundary, no circuit breaker needed)
- Operations that must succeed or retry indefinitely (use retry + timeout instead)
- User-facing validation errors (4xx responses are not failures — they are expected)
Critical Patterns
✅ REQUIRED: Three States
The circuit breaker is a state machine with three states.
✅ REQUIRED: Failure Threshold Configuration
Configure per-dependency, not globally. Use a time-window count or error rate, not raw cumulative count.
✅ REQUIRED: Fallback Strategy
An open circuit must return something useful — never silently fail.
Fallback options by priority:
- Cached value — return last known good response
- Default/safe value — a sensible degraded response (empty list, zero price with warning)
- Queue for retry — enqueue the request for later processing
- Fail fast with clear error — tell the user the feature is temporarily unavailable
✅ REQUIRED: Observability
Emit events on state transitions and expose metrics. Silent circuit breakers hide production issues.
Metrics to expose: current state, failure count in window, last transition time, request count (success/failure/rejected).
❌ NEVER: Circuit Breaker Without Fallback
An open circuit that just throws an unhandled error provides no protection — it just changes which error the user sees.
❌ NEVER: Single Global Breaker
One breaker for all services means one slow API trips the breaker and blocks all other services.
HALF_OPEN Probe Logic
Allow a small number of test requests through. Reset to CLOSED on enough successes; trip back to OPEN on any failure.
Timeout ≠ Failure
Not all errors should trip the circuit. Distinguish between service failures and client errors.
Decision Tree
Example
Minimal TypeScript circuit breaker implementation.
Edge Cases
Library vs manual implementation: For production use, prefer opossum (Node.js) or cockatiel rather than a home-grown implementation — they handle edge cases (concurrent requests in HALF_OPEN, atomic state transitions, metrics hooks).
Distributed circuit breakers: In multi-instance deployments, each instance has its own breaker state. Use a shared state store (Redis) only if consistency across instances is required. Usually, per-instance breakers are sufficient.
Circuit breaker with bulkhead: Combine circuit breaker (stop calls on failure) with bulkhead (limit concurrent calls) for full resilience: bulkhead → circuit breaker → timeout → retry → actual call.
Fallback strategy when OPEN: Choose based on whether staleness is acceptable:
- Cached data acceptable: Return last-known-good response from an in-memory or Redis cache.
- Partial results acceptable: Return degraded response (e.g., empty recommendations list instead of 500).
- No fallback possible: Fast-fail immediately with a user-facing message; never hang.
Avoid retrying inside the fallback — the circuit is OPEN precisely to prevent cascading load.
Observability: Log every state transition with service name and failure reason:
Expose a gauge metric circuit_breaker_state (0=CLOSED, 1=OPEN, 2=HALF_OPEN). Alert when OPEN for >30s — sustained OPEN indicates the downstream service has not recovered.
Testing HALF_OPEN: Wall-clock resetTimeout makes tests slow and flaky. Use a configurable timeout (inject via constructor or env var) and set it to 0ms in tests. Alternatively, expose a forceHalfOpen() method on the breaker for test-only state injection.
Resources
- result-pattern — composable error handling; use with circuit breaker fallbacks
- architecture-patterns — resilience patterns overview
Security
- Security scanPassed, 100 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/circuit-breaker-pattern/SKILL.md
Popularity
- Repository stars
- 9
- 7-day trend score
- 12
Stars and forks are for the whole GitHub repository, not this skill alone. Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/joabgonzalez/ai-agents-skills/circuit-breaker-pattern)