Circuit breakers for agents that act at machine speed
A short pattern note on budgets, rate limits and automatic shutdown, with a 30-line implementation.
Placeholder. This is seed content for reviewing the short-article layout. Replace it before launch.
An agent that makes a bad decision once is an incident. An agent that makes the same bad decision two hundred times a minute is an outage. Circuit breakers, borrowed from distributed systems, put a ceiling on how much damage can accumulate before something stops.
Three limits cover most cases:
- Budget: total spend, tokens or tool calls per task and per day.
- Rate: calls per minute to any one tool.
- Error streak: consecutive failures or denials, which often signal a loop.
When any limit trips, the breaker opens: further calls fail fast and a person is notified. It closes again only on an explicit reset.
type Limits = { maxCallsPerMinute: number; maxErrorStreak: number; maxSpend: number };
export class AgentBreaker {
private calls: number[] = [];
private errorStreak = 0;
private spend = 0;
open = false;
constructor(private limits: Limits, private onTrip: (why: string) => void) {}
before(cost = 0) {
if (this.open) throw new Error('circuit open: agent paused');
const now = Date.now();
this.calls = this.calls.filter((t) => now - t < 60_000);
if (this.calls.length >= this.limits.maxCallsPerMinute) return this.trip('rate limit');
if (this.spend + cost > this.limits.maxSpend) return this.trip('budget exhausted');
this.calls.push(now);
this.spend += cost;
}
after(ok: boolean) {
this.errorStreak = ok ? 0 : this.errorStreak + 1;
if (this.errorStreak >= this.limits.maxErrorStreak) this.trip('error streak');
}
private trip(why: string): never {
this.open = true;
this.onTrip(why);
throw new Error(`circuit open: ${why}`);
}
}
Keep the breaker outside the agent’s own process if you can, so a misbehaving agent cannot reset it.