DEV Community

Patrick
Patrick

Posted on

The Circuit Breaker Pattern: Stop Your AI Agent From Amplifying Failures

Every experienced engineer knows the circuit breaker pattern for microservices. But most AI agent builders ignore it entirely.

The Problem: Agents Amplify Failures

A typical agent loop: fetch data, process, write output, sleep 60s, repeat. When step 1 starts returning errors, most agents just keep trying. Every 60 seconds. If it hangs instead of erroring, your agent freezes entirely — blocking every downstream task.

The Fix: max_retries in SOUL.md

Add a circuit breaker rule directly to your agent instruction file:

Circuit breaker rule:
- Track consecutive failures per step
- After 3 failures on the same step, write context to outbox.json and halt
- Do NOT retry indefinitely
- Surface the problem; do not amplify it
Enter fullscreen mode Exit fullscreen mode

That last line is the key insight. An agent that stops and reports is infinitely more valuable than one that keeps failing silently.

What Goes in outbox.json

When the circuit trips, write a structured failure record:

{
  "type": "circuit_break",
  "step": "fetch_market_data",
  "failure_count": 3,
  "last_error": "timeout after 30s",
  "context": "Mid-cycle on portfolio rebalance. Position unchanged.",
  "recommended_action": "Check API credentials and rate limits"
}
Enter fullscreen mode Exit fullscreen mode

Three-Level Circuit Breaker

For production agent stacks, three levels:

Level 1 - Step level: 3 failures on a specific operation → stop that operation.

Level 2 - Agent level: 5 total failures in one cycle → halt the full agent, write to outbox.

Level 3 - Stack level: 2 agents halted simultaneously → alert human operator.

Each level escalates appropriately without triggering false alarms from transient errors.

The Bigger Point

The best agent patterns make failures visible and recoverable rather than silent and compounding. Circuit breakers, escalation rules, accountability logs — all variations on the same principle: build agents that know when to stop.


Full library of agent reliability patterns (including real SOUL.md configs) at askpatrick.co.

Top comments (1)

Collapse
 
pramod_kumar_0820 profile image
Pramod Kumar

Nice