<?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: Ami Heines</title>
    <description>The latest articles on DEV Community by Ami Heines (@amih).</description>
    <link>https://dev.to/amih</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%2F4030564%2Fd1d5cd94-16e2-4bd1-aef7-8069169a13e8.jpg</url>
      <title>DEV Community: Ami Heines</title>
      <link>https://dev.to/amih</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/amih"/>
    <language>en</language>
    <item>
      <title>A LiteLLM circuit breaker that kills runaway agent runs</title>
      <dc:creator>Ami Heines</dc:creator>
      <pubDate>Thu, 16 Jul 2026 05:30:02 +0000</pubDate>
      <link>https://dev.to/amih/a-litellm-circuit-breaker-that-kills-runaway-agent-runs-3fp6</link>
      <guid>https://dev.to/amih/a-litellm-circuit-breaker-that-kills-runaway-agent-runs-3fp6</guid>
      <description>&lt;p&gt;&lt;strong&gt;The bill that ruins a Monday doesn't come from a break-in.&lt;/strong&gt; It comes from one agent run&lt;br&gt;
that got stuck. A tool started failing on Friday night; the agent called it, saw the error,&lt;br&gt;
and — because nothing told it to stop — called it again and again all weekend, full price&lt;br&gt;
every time. Nobody stole anything. The meter just ran.&lt;/p&gt;

&lt;p&gt;Most of that money is spent &lt;em&gt;after&lt;/em&gt; the model already saw the first error. So you can't build&lt;br&gt;
your defense on the agent stopping itself.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A stuck loop isn't a breach. It's a bill — and the thing running it already saw the error and kept paying.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  Why your existing caps don't catch it
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://docs.litellm.ai/" rel="noopener noreferrer"&gt;LiteLLM&lt;/a&gt; gives you monthly budgets and rate limits. They're good.&lt;br&gt;
They're the wrong &lt;em&gt;granularity&lt;/em&gt; for this.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;strong&gt;monthly budget&lt;/strong&gt; of $2,000 doesn't flinch at a run burning $60 an hour on Saturday.&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;rate limit&lt;/strong&gt; caps requests per minute; a patient loop stays under it and still bankrupts the run over hours.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Both watch &lt;em&gt;aggregates across many requests&lt;/em&gt;. The runaway lives &lt;em&gt;inside a single run&lt;/em&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  The idea: a per-run, failure-aware breaker
&lt;/h2&gt;

&lt;p&gt;Three signals separate a runaway from honest work:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Identical call, twice in a row&lt;/strong&gt; — a stuck agent re-emits the exact same tool call.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Same tool errors, twice in a row&lt;/strong&gt; — a broken tool fails on the same call repeatedly.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Session budget&lt;/strong&gt; — a hard ceiling per &lt;em&gt;run&lt;/em&gt;, enforced before the next call.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Everything &lt;strong&gt;fails closed&lt;/strong&gt;. A long legitimate task moves through many different calls and is never cut.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Enforcement belongs on the request path, where you can still act — not on the invoice, where all you can do is read about it.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  As a LiteLLM plugin
&lt;/h2&gt;

&lt;p&gt;LiteLLM's &lt;code&gt;CustomGuardrail&lt;/code&gt; gives you two hooks — one before each call, one after. That's enough:&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="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;GuardrailBreaker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;CustomGuardrail&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;

    &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;async_pre_call_hook&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user_api_key_dict&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;call_type&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;run_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;run_id_from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                 &lt;span class="c1"&gt;# from client metadata.run_id
&lt;/span&gt;        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&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;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;run_id&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;tripped&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;    &lt;span class="c1"&gt;# already cut? refuse the next call
&lt;/span&gt;            &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;_kill&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;run_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...)&lt;/span&gt;                &lt;span class="c1"&gt;# -&amp;gt; HTTP 429, the run stops here
&lt;/span&gt;        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&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;check&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;run_id&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;allowed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="c1"&gt;# over session budget?
&lt;/span&gt;            &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;_kill&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;run_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;

    &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;async_post_call_success_hook&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user_api_key_dict&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;run_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;run_id_from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;cost&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;litellm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;completion_cost&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;completion_response&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt;
        &lt;span class="n"&gt;sig&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;response_action_signature&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;              &lt;span class="c1"&gt;# the model's tool call
&lt;/span&gt;        &lt;span class="n"&gt;ok&lt;/span&gt;   &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="nf"&gt;last_tool_result_errored&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;messages&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;   &lt;span class="c1"&gt;# did the last tool error?
&lt;/span&gt;        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&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;record&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;run_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sig&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cost_usd&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;cost&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# may trip the run
&lt;/span&gt;        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two conventions, because the proxy sees the chat call, not your tool running:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Per-run identity is caller-supplied&lt;/strong&gt; — &lt;code&gt;metadata={"run_id": "agent-run-abc123"}&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A tool error is a marked result&lt;/strong&gt; — the agent sets &lt;code&gt;is_error: true&lt;/code&gt; on the tool message.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Live, against a local model
&lt;/h2&gt;

&lt;p&gt;Wired into LiteLLM 1.92 in front of a local ollama model (priced synthetically at real rates):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  mode = BROKEN AGENT
  turn 1:  model -&amp;gt; search_db({"query": "SELECT COUNT(*) FROM orders WHERE customer_id = 42"})
  turn 2:  model -&amp;gt; search_db({"query": "SELECT COUNT(*) FROM orders WHERE customer_id = 42"})
  turn 3:  ✂  PROXY CUT THE RUN
           reason: identical call repeated 2x in a row — stuck loop, cut run
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Session budget is a separate net — with a $0.01 per-run budget, a distinct-step run was still&lt;br&gt;
cut at call 17. A legitimate task is never touched: in the standalone sim, an unguarded broken&lt;br&gt;
agent runs up &lt;strong&gt;$98.51&lt;/strong&gt;; guarded, it's cut for &lt;strong&gt;$0.24&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  It works &lt;em&gt;with&lt;/em&gt; LiteLLM, not instead of it
&lt;/h2&gt;

&lt;p&gt;Keep the gateway — 100+ providers, monthly budgets, caching. This adds the one layer it&lt;br&gt;
doesn't have, as a plugin: ~60 lines and a &lt;code&gt;run_id&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Code (LiteLLM plugin, a standalone Node version, tests) — &lt;a href="https://github.com/smallestbusiness/guardrail" rel="noopener noreferrer"&gt;https://github.com/smallestbusiness/guardrail&lt;/a&gt;.&lt;br&gt;
Cookbook PR into LiteLLM — &lt;a href="https://github.com/BerriAI/litellm/pull/33386" rel="noopener noreferrer"&gt;https://github.com/BerriAI/litellm/pull/33386&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>litellm</category>
      <category>ai</category>
      <category>llmops</category>
      <category>python</category>
    </item>
  </channel>
</rss>
