<?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: Gabi Ulianitski</title>
    <description>The latest articles on DEV Community by Gabi Ulianitski (@gabi_ulianitski_531d04d75).</description>
    <link>https://dev.to/gabi_ulianitski_531d04d75</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%2F4240719%2Fe945c026-e445-4c6d-9eed-2c6cae13c6c1.jpg</url>
      <title>DEV Community: Gabi Ulianitski</title>
      <link>https://dev.to/gabi_ulianitski_531d04d75</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gabi_ulianitski_531d04d75"/>
    <language>en</language>
    <item>
      <title>How to Stop Infinite AI Agent Loops and Reduce LLM API Costs in Python</title>
      <dc:creator>Gabi Ulianitski</dc:creator>
      <pubDate>Thu, 12 Nov 2026 09:42:27 +0000</pubDate>
      <link>https://dev.to/gabi_ulianitski_531d04d75/how-to-stop-infinite-ai-agent-loops-and-reduce-llm-api-costs-in-python-16c1</link>
      <guid>https://dev.to/gabi_ulianitski_531d04d75/how-to-stop-infinite-ai-agent-loops-and-reduce-llm-api-costs-in-python-16c1</guid>
      <description>&lt;p&gt;If you have deployed autonomous AI agents using tool calling, you have probably experienced—or feared—the midnight API bill spike. &lt;/p&gt;

&lt;p&gt;An agent calls a tool, hits a minor validation error, apologizes to its own context window, and retries the exact same failing call in a tight loop until your token budget explodes.&lt;/p&gt;

&lt;p&gt;Most frameworks (like LangChain, LangGraph, or Vercel AI SDK) patch this with static hard caps like &lt;code&gt;max_iterations=15&lt;/code&gt;. That approach has three major flaws in production:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;It kills legitimate long tasks:&lt;/strong&gt; If a complex enterprise workflow genuinely requires 20 steps, a rigid cap breaks it prematurely.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It is blind to early loops:&lt;/strong&gt; If an agent gets stuck repeating the exact same 2-step cycle at turn 2, it still burns 13 unnecessary LLM calls before hitting the limit.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It crashes the user experience:&lt;/strong&gt; Hard caps terminate the runtime abruptly, leaving the end-user stranded with an error instead of a completed answer.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To solve this without rewriting your agent logic, I built &lt;strong&gt;&lt;a href="https://github.com/gabiUlianitski/loopshield.git" rel="noopener noreferrer"&gt;LoopShield&lt;/a&gt;&lt;/strong&gt;—an open-source Python SDK and OpenAI/Anthropic-compatible gateway proxy that detects loops in flight and guides agents to self-heal.&lt;/p&gt;




&lt;h2&gt;
  
  
  How LoopShield Works (Twin-Engine Detection)
&lt;/h2&gt;

&lt;p&gt;LoopShield sits directly in front of your LLM provider (&lt;code&gt;POST /v1/chat/completions&lt;/code&gt;), screening every turn before forwarding traffic upstream:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Agent / App ──▶ LoopShield Proxy ──▶ Graph &amp;amp; Semantic Check
                                          │
                  ┌───────────────────────┴───────────────────────┐
                  ▼ (Loop Detected)                               ▼ (Clean Traffic)
     Lock Tool + Inject System Nudge                Symmetrical Context Pruning
     (Returns valid completion to Agent)             (Forwards to OpenAI / Anthropic)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead of relying on static iteration counters, LoopShield runs two detectors on every step:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Structural Graph Analysis (&lt;code&gt;networkx&lt;/code&gt;):&lt;/strong&gt; Maps sequential tool calls (including normalized arguments) as a directed graph. If the agent walks the same multi-step cycle again, it flags a graph loop immediately.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Semantic Thought Proximity (&lt;code&gt;numpy&lt;/code&gt;):&lt;/strong&gt; Evaluates cosine similarity on internal thought metadata locally. If an agent gets stuck rephrasing the same failing plan without calling tools, it catches the loop once similarity exceeds your threshold (default &lt;code&gt;&amp;gt; 0.92&lt;/code&gt;).&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Self-Healing Over Termination
&lt;/h2&gt;

&lt;p&gt;Instead of crashing your application when a loop is caught, LoopShield intercepts the request before it hits your upstream API bill, locks the repeating tool, and returns a valid assistant message containing a &lt;strong&gt;System Nudge&lt;/strong&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;"You are repeating a tool cycle. Tool X is locked. Switch strategies or finalize your response."&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The agent adapts, picks a different tool or path, and finishes the job. When long conversations approach your token limit (&lt;code&gt;SHIELD_MAX_TOKENS&lt;/code&gt;), LoopShield also applies &lt;strong&gt;Symmetrical Context Pruning&lt;/strong&gt;, stripping historical failed tool calls alongside their matching error outputs so upstream schemas stay valid.&lt;/p&gt;




&lt;h2&gt;
  
  
  Quick Start (1-Line Proxy or In-Process Python SDK)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Option 1: Run as a Containerized Gateway Proxy
&lt;/h3&gt;

&lt;p&gt;Start the Docker container and point your OpenAI or Anthropic SDK &lt;code&gt;base_url&lt;/code&gt; to port &lt;code&gt;8000&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker run --rm -p 8000:8000 \
  -e &lt;span class="nv"&gt;UPSTREAM_API_BASE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;https://openai.com \
  -e &lt;span class="nv"&gt;SHIELD_SIMILARITY_THRESHOLD&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;0.92 \
  loopshield:1.0.0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;openai&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;OpenAI&lt;/span&gt;

&lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;OpenAI&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;base_url&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"http://127.0.0"&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="s2"&gt;"sk-..."&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Option 2: Use the Python SDK Directly
&lt;/h3&gt;

&lt;p&gt;Install via pip and wrap your agent steps in-process:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip install loopshield
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;loopshield&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;AgentStep&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;LoopShield&lt;/span&gt;

&lt;span class="n"&gt;shield&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;LoopShield&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;similarity_threshold&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;0.92&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;max_cycles&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;verdict&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;shield&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;step&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nc"&gt;AgentStep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;thought&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"Checking config file again."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;tool_name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"read_file"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;tool_args&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"path"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"config.txt"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;tokens_used&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;verdict&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;is_loop&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;verdict&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;intervention_prompt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Native middleware wrappers are also included for &lt;strong&gt;LangChain&lt;/strong&gt;, &lt;strong&gt;LangGraph&lt;/strong&gt;, and &lt;strong&gt;CrewAI&lt;/strong&gt; (&lt;code&gt;pip install "loopshield[langchain]"&lt;/code&gt;).&lt;/p&gt;




&lt;h2&gt;
  
  
  Check Out the Code
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;GitHub Repository:&lt;/strong&gt; &lt;a href="https://github.com/gabiUlianitski/loopshield.git" rel="noopener noreferrer"&gt;://https://github.com/gabiUlianitski/loopshield.git&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;PyPI Package:&lt;/strong&gt; &lt;code&gt;pip install loopshield&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;How are you currently handling runaway agent loops and token budgets in production? I would love to hear your thoughts in the comments!&lt;/p&gt;

</description>
      <category>ai</category>
      <category>python</category>
      <category>agents</category>
      <category>showdev</category>
    </item>
  </channel>
</rss>
