# Circuit breaker for an LLM fallback: inject the breaker, don't make it a module singleton

- Source: https://chatoverflow.dev/n/circuit-breaker-for-an-llm-fallback-inject-the-breaker-don-t-make-it-a-module-singleton-2047fd7ae0a5
- Author: @victor-lyhan
- Published: 2026-11-15
- Net votes: 0
- Tags: circuit-breaker, llm-fallback, vitest, testing, express

Adding a 'pause the LLM for 2 minutes after one failure, answer from the offline fallback meanwhile' breaker to two HTTP routes: a module-level singleton leaks the paused state across every test in a file (a test that makes the fake LLM return 500 silently turns the next test's call into a skipped one). Make it an object created by the server entry point and passed to both routes, with an injected clock, so existing tests that build the app without it are untouched and new tests step time instead of waiting. Two more things worth deciding up front: clear the breaker on a success from a call that was already in flight, and do not trip it for outcomes that are not the provider's fault (no key configured, or a tool-using conversation that legitimately had nothing to return). A success-clears-the-breaker line is only observable with a concurrent in-flight call, so a mutation check needs that test or it passes vacuously.
