<?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: Pradyumn verma</title>
    <description>The latest articles on DEV Community by Pradyumn verma (@vermadyumn).</description>
    <link>https://dev.to/vermadyumn</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%2F2501256%2Fa81ba21f-5363-4a01-b820-cfdb6ffd3c2f.jpg</url>
      <title>DEV Community: Pradyumn verma</title>
      <link>https://dev.to/vermadyumn</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vermadyumn"/>
    <language>en</language>
    <item>
      <title>Your AI spend cap probably has a race condition</title>
      <dc:creator>Pradyumn verma</dc:creator>
      <pubDate>Fri, 17 Jul 2026 13:00:59 +0000</pubDate>
      <link>https://dev.to/vermadyumn/your-ai-spend-cap-probably-has-a-race-condition-2ei7</link>
      <guid>https://dev.to/vermadyumn/your-ai-spend-cap-probably-has-a-race-condition-2ei7</guid>
      <description>&lt;p&gt;There's a whole genre of posts about waking up to a surprise OpenAI bill.&lt;br&gt;
They all end with the same advice: set a budget cap. Sensible. What nobody&lt;br&gt;
talks about is how those caps actually work under the hood, so I got&lt;br&gt;
curious and started reading implementations. Tutorials, production&lt;br&gt;
snippets, open source tools, whatever I could find.&lt;/p&gt;

&lt;p&gt;Almost all of them share the same bug. Not a typo kind of bug, a&lt;br&gt;
shape-of-the-problem kind of bug. This post is about what it is, how bad it&lt;br&gt;
gets (I measured), and the small open source tool I ended up building&lt;br&gt;
because I couldn't leave it alone.&lt;/p&gt;
&lt;h2&gt;
  
  
  The bug
&lt;/h2&gt;

&lt;p&gt;Give your AI agent a $50/day budget and ask yourself: can it spend $63?&lt;/p&gt;

&lt;p&gt;With most setups, yes. Here's the pattern I keep seeing, in tutorials, in&lt;br&gt;
production code, in tools that should know better:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;read the spend counter&lt;/li&gt;
&lt;li&gt;if there's room under the cap, make the LLM call&lt;/li&gt;
&lt;li&gt;when the response arrives, add its cost to the counter&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The problem lives between step 1 and step 3. An LLM call's cost doesn't&lt;br&gt;
exist until the response comes back. So while one call is in flight, ten&lt;br&gt;
other requests are reading a counter that knows nothing about it. All ten&lt;br&gt;
pass the check. Your cap was $50 and you're at $63.&lt;/p&gt;

&lt;p&gt;Normal rate limits don't have this problem. A request costs "one request"&lt;br&gt;
and you know that up front. Spend is different: the thing you're limiting is&lt;br&gt;
only measurable after the fact, so the counter is always behind reality, and&lt;br&gt;
it falls further behind exactly when traffic spikes. Which is when you&lt;br&gt;
wanted the cap most.&lt;/p&gt;

&lt;p&gt;To be fair, the popular proxies have done real work here. LiteLLM moved its&lt;br&gt;
rate limiting onto atomic Redis Lua scripts back in v1.72.2, which killed&lt;br&gt;
the read-check-write race inside a single check. But spend tracking is a&lt;br&gt;
different subsystem: cost gets computed after the response and written&lt;br&gt;
asynchronously (batched writers, cached counters), and nothing holds the&lt;br&gt;
estimated cost of in-flight calls against the budget. I'm not picking on&lt;br&gt;
anyone. It's a genuinely awkward shape of problem.&lt;/p&gt;
&lt;h2&gt;
  
  
  Measuring it instead of arguing about it
&lt;/h2&gt;

&lt;p&gt;I wrote a demo that runs two limiters under the same load: 1,000 concurrent&lt;br&gt;
requests against a cap of 500. One is naive check-then-act against a Redis&lt;br&gt;
counter. The other does the check and the deduction inside one Lua script.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cap = 500, concurrent requests = 1000

naive check-then-act:  granted 613  (over-spend: +113)
atomic enforcement:    granted 500  (over-spend: +0)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I ran it four times. The naive limiter over-spent by +113, +124, +148 and&lt;br&gt;
+161. A different number every run, because it's a race, and races don't&lt;br&gt;
owe you consistency. The atomic one granted exactly 500 every time.&lt;/p&gt;

&lt;p&gt;That's a 20-30% overshoot on a "hard" cap, from nothing but concurrency.&lt;/p&gt;
&lt;h2&gt;
  
  
  The fix has two parts
&lt;/h2&gt;

&lt;p&gt;The first part is obvious once you see it: the check and the deduction have&lt;br&gt;
to be one operation. Redis executes a Lua script atomically, nothing&lt;br&gt;
interleaves, so the race window just doesn't exist:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight lua"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- abridged: all-or-nothing across every configured period&lt;/span&gt;
&lt;span class="kd"&gt;local&lt;/span&gt; &lt;span class="n"&gt;allowed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;ipairs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;budgets&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;remaining&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt; &lt;span class="n"&gt;allowed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;allowed&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;ipairs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;budgets&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;redis&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'DECRBY'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;remaining_key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The second part took me longer. Atomic or not, you can't deduct a number&lt;br&gt;
you don't know yet, and at decision time the LLM call hasn't happened.&lt;br&gt;
Payment systems solved this decades ago with two phases, so I stole that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;reserve&lt;/strong&gt; happens before the call: atomically hold an &lt;em&gt;estimate&lt;/em&gt;
against the budget. If the daily cap or the monthly cap would be blown
(they're checked together, all-or-nothing), the request is rejected and
nothing is held.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;settle&lt;/strong&gt; happens after the response: report what the call actually
cost. The difference gets refunded. If the actual ran over your estimate,
the difference gets charged, and yes, that means a budget can end up
slightly negative.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I went back and forth on that last part. But a cap on unknown costs can be&lt;br&gt;
strict at admission time or it can be clairvoyant, and it can't be both.&lt;br&gt;
I'd rather the system admit that than pretend.&lt;/p&gt;

&lt;p&gt;Crashes were the other design headache. If a client reserves and then dies,&lt;br&gt;
that hold can't sit there eating budget forever. So every reservation&lt;br&gt;
carries a TTL and gets auto-released. There's no background worker for&lt;br&gt;
this; expired holds get reclaimed lazily on the next call for that subject,&lt;br&gt;
in bounded batches, which keeps enforcement latency flat.&lt;/p&gt;
&lt;h2&gt;
  
  
  Does it hold up
&lt;/h2&gt;

&lt;p&gt;The test I care most about fires 1,000 concurrent 1-unit requests at a&lt;br&gt;
500-unit budget and asserts that exactly 500 succeed. Not "at most 500".&lt;br&gt;
Exactly. There's an equivalent for reserve/settle that checks the books&lt;br&gt;
reconcile to the unit after 200 concurrent reservations with a mix of&lt;br&gt;
settles and cancellations.&lt;/p&gt;

&lt;p&gt;On my laptop it does about 9,000 enforcement decisions per second with p99&lt;br&gt;
around 5ms, and a multiprocess drain test shows zero over-serving.&lt;/p&gt;
&lt;h2&gt;
  
  
  What it doesn't do
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Rate limiting. The proxies already do that well, atomically.&lt;/li&gt;
&lt;li&gt;Redis Cluster. Single instance for now.&lt;/li&gt;
&lt;li&gt;Estimate your costs for you. Estimates are your job; reserve/settle just
makes a wrong estimate safe instead of free.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Try "Hardcap"
&lt;/h2&gt;

&lt;p&gt;The whole thing is a small FastAPI + Redis sidecar called &lt;strong&gt;hardcap&lt;/strong&gt;,&lt;br&gt;
Apache-2.0: &lt;a href="https://github.com/pradyumnvermaa/hardcap" rel="noopener noreferrer"&gt;https://github.com/pradyumnvermaa/hardcap&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone https://github.com/pradyumnvermaa/hardcap &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;cd &lt;/span&gt;hardcap
docker compose up &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; python demo/overspend_demo.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The demo prints the naive limiter's over-spend on your machine. It will be&lt;br&gt;
a different number than mine. That's kind of the whole point.&lt;/p&gt;

&lt;p&gt;And if you look at the reserve/settle semantics and think you can make it&lt;br&gt;
over-spend, I genuinely want to hear about it.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>python</category>
      <category>redis</category>
      <category>lua</category>
    </item>
  </channel>
</rss>
