# Circuit Breaker

> Circuit breaker: a safeguard that automatically shuts down an AI agent or a service when it detects a malfunction, to prevent runaway cost loops.

- **Category**: Technical
- **URL**: https://www.romaindelfosse.fr/en/glossary/circuit-breaker/

---

A **circuit breaker** is a safeguard borrowed from software architecture that automatically shuts down an [AI agent](/en/glossary/ai-agent/) or a service when it detects abnormal behavior — an infinite loop, a failing API, excessive token consumption. It's the safety net that keeps an autonomous agent from burning through your budget overnight.

## Technical definition

### The principle: cut it off before it breaks

The concept comes from electrical engineering: a circuit breaker cuts the current when it exceeds a threshold, to protect the circuit. In software, the circuit breaker pattern does the same for calls between services. Applied to AI agents, it watches three indicators: the number of tokens consumed, the elapsed time and the number of failed attempts.

### The three states

A circuit breaker works in three states:

1. **Closed** (normal operation) — Requests go through as usual. The circuit breaker counts errors in the background.
2. **Open** (protection engaged) — The error threshold has been exceeded. Every request is blocked immediately. The agent receives a stop signal or falls back to a plan B.
3. **Half-open** (recovery test) — After a delay, the circuit breaker lets a few test requests through. If they succeed, it goes back to the closed state. Otherwise, it stays open.

{% figureInteractive "circuit-breaker", "Run the agent, take the API down and watch the breaker go from closed to open, then try to recover." %}

## Current challenges

### The autonomous agent trap

An AI agent running in a [ReAct](/en/glossary/react/) loop can run forever if nothing stops it. The classic case: an external API goes down overnight, the agent retries its request every few seconds, each attempt consumes tokens — and in the morning, the bill is waiting. The circuit breaker is the mechanism that turns this worst-case scenario into a contained incident.

### Three layers of protection

In production, a single circuit breaker isn't enough. Good practice combines three layers: a **token cap** per run (micro), a **timeout** per step (meso) and a global **daily budget** (macro). This defense in depth protects against local loops, slow APIs and cumulative overruns across multiple agents.


## Key Figures

- Gartner predicts that 40% of enterprise applications will integrate AI agents by the end of 2026, making safeguards like the circuit breaker essential *(Gartner, cited in A Year of MCP — Pento)*



## Frequently Asked Questions

### What is a circuit breaker in the context of AI?

A circuit breaker is a safeguard that monitors an AI agent's behavior and shuts it down automatically when something goes wrong. For example: if an agent has consumed more than 100,000 tokens without making progress, or if an external API stops responding, the circuit breaker stops the agent instead of letting it loop and rack up costs.

### Why does an AI agent need a circuit breaker?

An AI agent runs in an autonomous loop (the ReAct cycle). If a tool doesn't respond or returns errors, the agent can retry the same action indefinitely — and every attempt burns billable tokens. Without a circuit breaker, an agent stuck retrying an unavailable API can burn through tens or even hundreds of dollars in a few hours, overnight, without anyone noticing.

### How do you implement a circuit breaker for an AI agent?

Three complementary safeguards: a token cap per run (the agent stops automatically above a threshold), a per-step timeout (if a tool call doesn't respond within 30 seconds, the agent switches to a plan B) and a daily budget (above a set amount, every run is suspended). Together, these three layers protect against infinite loops, failing APIs and cumulative overruns.



## Resources

- [Pattern: Circuit Breaker — Microservices.io](https://microservices.io/patterns/reliability/circuit-breaker.html?utm_source=romaindelfosse&amp;utm_medium=glossaire&amp;utm_campaign=circuit-breaker) — Chris Richardson

- [Comment créer un agent IA ? — Les pièges à éviter](https://romaindelfosse.fr/blog/comment-creer-agent-ia/?utm_source=romaindelfosse&amp;utm_medium=glossaire&amp;utm_campaign=circuit-breaker) — Romain Delfosse

