<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: DDHH</title>
    <description>The latest articles on DEV Community by DDHH (@ddhh).</description>
    <link>https://dev.to/ddhh</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4028315%2F7934f7d0-941f-4612-98a6-bc9ed7ae69bb.png</url>
      <title>DEV Community: DDHH</title>
      <link>https://dev.to/ddhh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ddhh"/>
    <language>en</language>
    <item>
      <title>I built a tiny LLM circuit breaker: when the budget runs out, it fails over to a local model instead of failing or overspending</title>
      <dc:creator>DDHH</dc:creator>
      <pubDate>Wed, 15 Jul 2026 07:36:21 +0000</pubDate>
      <link>https://dev.to/ddhh/i-built-a-tiny-llm-circuit-breaker-when-the-budget-runs-out-it-fails-over-to-a-local-model-30ka</link>
      <guid>https://dev.to/ddhh/i-built-a-tiny-llm-circuit-breaker-when-the-budget-runs-out-it-fails-over-to-a-local-model-30ka</guid>
      <description>&lt;p&gt;I run a small multi-agent system at home — a few agents that scout, summarize, and write, on a laptop, mostly for fun and learning. Nothing fancy. But I kept slamming into the same two walls:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Paid API quotas dying mid-run.&lt;/strong&gt; One provider 429s, and the whole run falls over.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The quiet fear of a stuck loop.&lt;/strong&gt; A retry gone wrong, an agent talking to itself — and suddenly you've burned a chunk of your monthly budget in minutes.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I looked at the existing LLM gateways. They're great, but most of them solve &lt;em&gt;observability&lt;/em&gt; (dashboards, traces) or &lt;em&gt;routing&lt;/em&gt; (pick the cheapest/best model). What I actually wanted was weirdly specific and I couldn't find it as a small thing:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;When I'm about to overspend, &lt;strong&gt;don't fail and don't keep paying — fall through to a free local model&lt;/strong&gt; and keep working.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So I wrote the smallest possible version of exactly that. It does one job:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;llm_circuit_breaker&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;LLMCircuitBreaker&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;anthropic_tier&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ollama_tier&lt;/span&gt;

&lt;span class="n"&gt;breaker&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;LLMCircuitBreaker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;tiers&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="nf"&gt;anthropic_tier&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;api_key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;sk-ant-...&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;claude-sonnet-4-5&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="nf"&gt;ollama_tier&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;gemma:2b&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;   &lt;span class="c1"&gt;# local, free, always-on fallback
&lt;/span&gt;    &lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="n"&gt;daily_limit_usd&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;5.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;ledger_path&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;llm_spend.jsonl&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;breaker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;complete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;system&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;You are terse.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;2+2?&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once today's spend crosses $5, &lt;strong&gt;every call for the rest of the day skips the paid tier and goes straight to your local model&lt;/strong&gt; — no code change, no surprise bill. Spend is tracked in a plain JSONL file. No database, no hosted proxy, nothing to stand up.&lt;/p&gt;

&lt;h2&gt;
  
  
  How it decides
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Check today's + all-time spend against your limits (that human-readable JSONL ledger).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Under budget:&lt;/strong&gt; try each tier in order, return the first success.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Over budget:&lt;/strong&gt; skip every paid tier, try only the ones marked &lt;code&gt;is_local=True&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If everything attempted fails, raise a clear exception instead of failing silently.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That's the whole idea. Failing &lt;em&gt;into&lt;/em&gt; free local compute beats failing loudly, and beats quietly running up a bill.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I added once I actually used it (v0.2)
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Streaming&lt;/strong&gt; — &lt;code&gt;complete_stream&lt;/code&gt;, same tiers, same budget fallback, just yielded chunk by chunk.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Async&lt;/strong&gt; — &lt;code&gt;acomplete&lt;/code&gt;, to keep many calls in flight.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A &lt;code&gt;cost&lt;/code&gt; CLI&lt;/strong&gt; — because a budget breaker you can't inspect is half a tool:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;llm-cb cost      &lt;span class="c"&gt;# today / total / per-tier, straight from the ledger&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One more thing that turned out nice: the tiers are just an ordered list, so the fallback doesn't have to be "expensive → local." You can slot a &lt;strong&gt;cheap frontier &lt;em&gt;open&lt;/em&gt; model&lt;/strong&gt; in the middle — e.g. an &lt;code&gt;openrouter_tier&lt;/code&gt; pointing at something like LongCat-2.0 or GLM-5 — so the chain becomes &lt;em&gt;frontier-paid → cheap-open → local&lt;/em&gt;, and the budget cap still governs the whole thing.&lt;/p&gt;

&lt;h2&gt;
  
  
  What this is NOT
&lt;/h2&gt;

&lt;p&gt;It's &lt;strong&gt;not&lt;/strong&gt; a LiteLLM or Portkey replacement. If you need enterprise routing, guardrails, team budgets, or a proxy in front of a fleet, use those — they're excellent.&lt;/p&gt;

&lt;p&gt;This is the opposite trade-off on purpose: ~200 lines you can read in one sitting and vendor straight into your own repo. In-process, one job, for solo builders and small agent projects who just want a hard stop before overspend without running another service. MIT.&lt;/p&gt;

&lt;h2&gt;
  
  
  Two things I learned building it
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;The hard part was saying no to features.&lt;/strong&gt; I almost turned this into a proxy server with a dashboard. The moment I did that, it stopped being the thing I wanted — a tiny file I can read and trust. The whole value &lt;em&gt;is&lt;/em&gt; the smallness. Deleting the roadmap was the feature.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fallback beats retry.&lt;/strong&gt; My first instinct was smarter retries against the paid API. But retries against a dying quota just burn time and money. Falling over to something free that always answers — even if it's a dumber model — kept the agents &lt;em&gt;working&lt;/em&gt;, which is what I actually needed at 2am.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Repo (MIT): &lt;strong&gt;&lt;a href="https://github.com/qkrehgk1-wq/llm-circuit-breaker" rel="noopener noreferrer"&gt;https://github.com/qkrehgk1-wq/llm-circuit-breaker&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I'd genuinely love feedback — especially on the core behavior: does &lt;em&gt;budget-exhausted → local&lt;/em&gt; match how you'd want an agent to fail? And does anyone else route to local as a &lt;strong&gt;fallback&lt;/strong&gt; rather than as the primary? That last question is the one I keep going back and forth on.&lt;/p&gt;

</description>
      <category>llm</category>
      <category>python</category>
      <category>opensource</category>
      <category>ai</category>
    </item>
  </channel>
</rss>
