<?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: kimbeomgyu</title>
    <description>The latest articles on DEV Community by kimbeomgyu (@kimbeomgyu).</description>
    <link>https://dev.to/kimbeomgyu</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%2F4006378%2Fba933921-f5db-4aa8-8a74-d7f35ae25160.jpg</url>
      <title>DEV Community: kimbeomgyu</title>
      <link>https://dev.to/kimbeomgyu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kimbeomgyu"/>
    <language>en</language>
    <item>
      <title>I built a free circuit breaker for your LLM API bill</title>
      <dc:creator>kimbeomgyu</dc:creator>
      <pubDate>Sun, 28 Jun 2026 10:39:29 +0000</pubDate>
      <link>https://dev.to/kimbeomgyu/i-built-a-free-circuit-breaker-for-your-llm-api-bill-3ifk</link>
      <guid>https://dev.to/kimbeomgyu/i-built-a-free-circuit-breaker-for-your-llm-api-bill-3ifk</guid>
      <description>&lt;p&gt;We've all seen the screenshot: someone leaves an agent running overnight, a retry loop goes sideways, and they wake up to a bill that's 10x what they expected — "$40 from a $5 task." The scary part isn't the money, it's that &lt;strong&gt;nothing stopped it&lt;/strong&gt;. You find out when the invoice lands.&lt;/p&gt;

&lt;p&gt;I wanted the dumbest possible fix: a hard cap that just &lt;em&gt;blocks&lt;/em&gt; the next call when you're over budget. Not a dashboard I have to remember to check, not a gateway I have to deploy. So I built &lt;strong&gt;budget-guard&lt;/strong&gt; — a tiny drop-in wrapper for the OpenAI/Anthropic clients.&lt;/p&gt;

&lt;h2&gt;
  
  
  The whole idea in 3 lines
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;guard&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;budget-guard&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ai&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;guard&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;openai&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;chat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;completions&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;project&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;my-app&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;dailyCapUSD&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;ai&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;gpt-4o&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;messages&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;feature&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;summarize&lt;/span&gt;&lt;span class="dl"&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 for &lt;code&gt;my-app&lt;/code&gt; crosses $50, the next &lt;code&gt;create()&lt;/code&gt; throws &lt;code&gt;BudgetExceededError&lt;/code&gt; &lt;strong&gt;before&lt;/strong&gt; it bills you. A runaway loop dies at the breaker instead of draining your account. It stays out of your critical path — your calls still go straight to the provider; budget-guard just meters the usage and trips when you're over.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bonus: where did the money go?
&lt;/h2&gt;

&lt;p&gt;It tags spend per feature, so you can finally answer "what cost that?":&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;spendReport&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;budget-guard&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nf"&gt;spendReport&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;my-app&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// → { summarize: 2.41, chat: 0.88 }  (today, USD)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It auto-detects OpenAI (&lt;code&gt;prompt_tokens&lt;/code&gt;/&lt;code&gt;completion_tokens&lt;/code&gt;) and Anthropic (&lt;code&gt;input_tokens&lt;/code&gt;/&lt;code&gt;output_tokens&lt;/code&gt;) usage shapes; for anything else you pass a one-line extractor.&lt;/p&gt;

&lt;h2&gt;
  
  
  Being honest about v0.1
&lt;/h2&gt;

&lt;p&gt;I'd rather tell you the limits than have you find them on an invoice:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;In-memory, per-process.&lt;/strong&gt; Great for a single script, agent, or worker. Run multiple instances and the cap is per-instance and resets on restart. A shared (Redis) store is the obvious next step.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Enforced on the next call.&lt;/strong&gt; No pre-call token estimation yet, so one call can overshoot before the breaker trips.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Prices are a hand-maintained table.&lt;/strong&gt; PRs welcome to keep them current.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So today it's best for solo devs, side projects, and single-process agents — exactly the people most likely to get surprised by a bill.&lt;/p&gt;

&lt;h2&gt;
  
  
  Try it / break it
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm i budget-guard
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;MIT, no runtime deps, TypeScript. Repo: &lt;a href="https://github.com/kimbeomgyu/budget-guard" rel="noopener noreferrer"&gt;https://github.com/kimbeomgyu/budget-guard&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I'd love feedback on two things: should it block by default, or warn + callback? And if you run a fleet, would a Redis-backed shared cap actually get used, or do you already handle this at a gateway layer? Issues and PRs very welcome.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>ai</category>
      <category>opensource</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
