<?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: uncoil</title>
    <description>The latest articles on DEV Community by uncoil (@uncoil).</description>
    <link>https://dev.to/uncoil</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%2F4195184%2Fce5d8d01-0973-4c80-a15c-f6df9059e5a0.png</url>
      <title>DEV Community: uncoil</title>
      <link>https://dev.to/uncoil</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/uncoil"/>
    <language>en</language>
    <item>
      <title>OpenAI and Anthropic count cached tokens in opposite ways</title>
      <dc:creator>uncoil</dc:creator>
      <pubDate>Mon, 30 Nov 2026 09:02:15 +0000</pubDate>
      <link>https://dev.to/uncoil/openai-and-anthropic-count-cached-tokens-in-opposite-ways-3o77</link>
      <guid>https://dev.to/uncoil/openai-and-anthropic-count-cached-tokens-in-opposite-ways-3o77</guid>
      <description>&lt;p&gt;Send the same 2,048-token prompt to OpenAI and to Anthropic, with most of it served from the prompt cache, and look at the usage objects that come back:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;OpenAI     prompt_tokens = 2048   (prompt_tokens_details.cached_tokens = 1792)
Anthropic  input_tokens  = 256    (cache_read_input_tokens = 1792)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Same prompt, same cache hit, and the headline input numbers differ by 8x. Neither provider is wrong. They just put the cached tokens in different places:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;OpenAI&lt;/strong&gt; counts cached tokens &lt;em&gt;inside&lt;/em&gt; &lt;code&gt;prompt_tokens&lt;/code&gt;. &lt;code&gt;cached_tokens&lt;/code&gt; tells you how many of them were a cache hit.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Anthropic&lt;/strong&gt; counts cached tokens &lt;em&gt;outside&lt;/em&gt; &lt;code&gt;input_tokens&lt;/code&gt;. &lt;code&gt;input_tokens&lt;/code&gt; is only what came after the last cache breakpoint. Cache reads and cache writes are separate fields that you add on.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If your dashboard or cost script reads one field per provider and calls it "input tokens", it will be wrong for at least one of them. With Anthropic you will undercount usage, sometimes badly. With OpenAI you will overcharge yourself if you price every prompt token at the full input rate.&lt;/p&gt;

&lt;p&gt;This post shows how each provider reports cache usage, a small function that turns both into the same shape, and the mistakes I see most often.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Anthropic reports it
&lt;/h2&gt;

&lt;p&gt;Anthropic's usage object has three input fields:&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="n"&gt;usage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;input_tokens&lt;/span&gt;                 &lt;span class="c1"&gt;# tokens after the last cache breakpoint&lt;/span&gt;
&lt;span class="n"&gt;usage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cache_read_input_tokens&lt;/span&gt;      &lt;span class="c1"&gt;# tokens served from the cache&lt;/span&gt;
&lt;span class="n"&gt;usage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cache_creation_input_tokens&lt;/span&gt;  &lt;span class="c1"&gt;# tokens written to the cache on this call&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Anthropic docs give the total as the sum of all three:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;total input = input_tokens + cache_read_input_tokens + cache_creation_input_tokens
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each part has its own price. Per the prompt caching docs, cache writes cost 1.25x the base input price with the default 5-minute lifetime and 2x with the 1-hour lifetime. Cache reads cost 0.1x on most models, and some newer models go lower. So a call that writes a large prompt to the cache costs &lt;em&gt;more&lt;/em&gt; than the same call without caching. The payoff only comes on later reads.&lt;/p&gt;

&lt;p&gt;There is also a &lt;code&gt;cache_creation&lt;/code&gt; object that splits the write count by lifetime (&lt;code&gt;ephemeral_5m_input_tokens&lt;/code&gt; and &lt;code&gt;ephemeral_1h_input_tokens&lt;/code&gt;). If you use both lifetimes, you need that split to price writes correctly.&lt;/p&gt;

&lt;h2&gt;
  
  
  How OpenAI reports it
&lt;/h2&gt;

&lt;p&gt;On Chat Completions, OpenAI's usage looks like this:&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="n"&gt;usage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;prompt_tokens&lt;/span&gt;                          &lt;span class="c1"&gt;# all input tokens, cached or not&lt;/span&gt;
&lt;span class="n"&gt;usage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;prompt_tokens_details&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cached_tokens&lt;/span&gt;    &lt;span class="c1"&gt;# how many of those were a cache hit&lt;/span&gt;
&lt;span class="n"&gt;usage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;completion_tokens&lt;/span&gt;
&lt;span class="n"&gt;usage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;total_tokens&lt;/span&gt;                           &lt;span class="c1"&gt;# prompt_tokens + completion_tokens&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;prompt_tokens&lt;/code&gt; is already the full input count, so the fresh, full-price part is &lt;code&gt;prompt_tokens - cached_tokens&lt;/code&gt;. The Responses API uses different names (&lt;code&gt;input_tokens&lt;/code&gt; and &lt;code&gt;input_tokens_details.cached_tokens&lt;/code&gt;) but the same idea: cached tokens sit inside the input count.&lt;/p&gt;

&lt;p&gt;Recent versions of the Python SDK (I checked &lt;code&gt;openai&lt;/code&gt; 3.60.0) also have a &lt;code&gt;cache_write_tokens&lt;/code&gt; field in &lt;code&gt;prompt_tokens_details&lt;/code&gt;. How it is priced depends on the model, so check OpenAI's pricing page for the models you use before you put it into your formula.&lt;/p&gt;

&lt;h2&gt;
  
  
  One shape for both
&lt;/h2&gt;

&lt;p&gt;The fix is to stop passing raw usage objects around and convert them once, at the edge, into one shape with the same meaning for every provider:&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;from&lt;/span&gt; &lt;span class="n"&gt;dataclasses&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;dataclass&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;openai&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;types&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;CompletionUsage&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;anthropic&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;types&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;Usage&lt;/span&gt;

&lt;span class="nd"&gt;@dataclass&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Tokens&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;fresh_input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;   &lt;span class="c1"&gt;# billed at the normal input price&lt;/span&gt;
    &lt;span class="n"&gt;cache_read&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;    &lt;span class="c1"&gt;# billed at the cache-hit price&lt;/span&gt;
    &lt;span class="n"&gt;cache_write&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;   &lt;span class="c1"&gt;# billed at the cache-write price&lt;/span&gt;
    &lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;

    &lt;span class="nd"&gt;@property&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;total_input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fresh_input&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cache_read&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cache_write&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;from_openai&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CompletionUsage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Tokens&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;prompt_tokens_details&lt;/span&gt;
    &lt;span class="n"&gt;read&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cached_tokens&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="mi"&gt;0&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;d&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Tokens&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;prompt_tokens&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;read&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;read&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;completion_tokens&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;from_anthropic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Usage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Tokens&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Tokens&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;input_tokens&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cache_read_input_tokens&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cache_creation_input_tokens&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;u&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;output_tokens&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run both example usage objects through it, built with the real SDK types:&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="n"&gt;oa&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;CompletionUsage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prompt_tokens&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;2048&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;completion_tokens&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;300&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;total_tokens&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;2348&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                     &lt;span class="n"&gt;prompt_tokens_details&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"cached_tokens"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1792&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;an&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Usage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input_tokens&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;256&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cache_read_input_tokens&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1792&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
           &lt;span class="n"&gt;cache_creation_input_tokens&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;output_tokens&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;300&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="nf"&gt;from_openai&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;oa&lt;/span&gt;&lt;span class="p"&gt;)&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="nf"&gt;from_anthropic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;an&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Tokens(fresh_input=256, cache_read=1792, cache_write=0, output=300)
Tokens(fresh_input=256, cache_read=1792, cache_write=0, output=300)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the two calls look the same, because they were the same. Cost becomes one function over one shape:&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;def&lt;/span&gt; &lt;span class="nf"&gt;cost&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Tokens&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;# price holds dollars per million tokens, taken from your own rate card&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fresh_input&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"input"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cache_read&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"cache_read"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cache_write&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"cache_write"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;output&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"output"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;1_000_000&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Keep the prices out of the code and in config. They change more often than the field names do.&lt;/p&gt;

&lt;h2&gt;
  
  
  Mistakes I keep seeing
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Reading &lt;code&gt;input_tokens&lt;/code&gt; from Anthropic and calling it the input.&lt;/strong&gt; On a heavily cached agent this can hide most of the input. A long system prompt plus tool definitions is often thousands of cached tokens, while &lt;code&gt;input_tokens&lt;/code&gt; shows only the new user turn.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Subtracting cached tokens from Anthropic's &lt;code&gt;input_tokens&lt;/code&gt;.&lt;/strong&gt; This is the reverse mistake, usually made by someone who learned the OpenAI shape first. The cached tokens were never in there, so you get a small or negative number.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pricing all of OpenAI's &lt;code&gt;prompt_tokens&lt;/code&gt; at the full rate.&lt;/strong&gt; Cached tokens are discounted, so you overestimate cost. That sounds harmless until you compare models or decide whether caching is worth it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Ignoring cache writes.&lt;/strong&gt; On Anthropic a write costs more than a normal input token. An agent that keeps changing the start of its prompt (a timestamp in the system prompt is the classic cause) writes the cache on every call and never reads it. Usage looks normal and the bill goes up. Track the ratio of &lt;code&gt;cache_read&lt;/code&gt; to &lt;code&gt;cache_write&lt;/code&gt; over a run. If writes dominate, your cache is not working.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Dropping usage on streams.&lt;/strong&gt; With OpenAI Chat Completions streaming, you only get a usage chunk if you pass &lt;code&gt;stream_options={"include_usage": True}&lt;/code&gt;, and the SDK notes that an interrupted stream may never send it. With Anthropic streaming, usage arrives in &lt;code&gt;message_start&lt;/code&gt; and is updated in &lt;code&gt;message_delta&lt;/code&gt;, and the counts in &lt;code&gt;message_delta&lt;/code&gt; are cumulative, so take the last value, don't add them up.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Summing &lt;code&gt;total_tokens&lt;/code&gt; across providers.&lt;/strong&gt; OpenAI's &lt;code&gt;total_tokens&lt;/code&gt; includes cached input. Anthropic has no &lt;code&gt;total_tokens&lt;/code&gt; field at all, so whatever you computed for it depends on which fields you added. Totals from mixed providers are only comparable after normalization.&lt;/p&gt;

&lt;h2&gt;
  
  
  A checklist
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Store the raw usage object for every model call, exactly as returned. You will want fields you did not think of today.&lt;/li&gt;
&lt;li&gt;Convert to one internal shape (fresh input, cache read, cache write, output) in one place, with a test per provider.&lt;/li&gt;
&lt;li&gt;Price each part separately, with prices in config.&lt;/li&gt;
&lt;li&gt;Watch the cache read to write ratio per agent, not only total tokens.&lt;/li&gt;
&lt;li&gt;Turn on usage for streams, and flag calls whose usage never arrived instead of counting them as zero.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Why I looked into this
&lt;/h2&gt;

&lt;p&gt;I build &lt;a href="https://www.uncoil.dev" rel="noopener noreferrer"&gt;Uncoil&lt;/a&gt;, an open-source tracing tool for AI agents. It records every model call in an agent run with its token usage, and the cache fields were the part that broke cost totals most often when people mixed providers in one agent. If you want to see your own numbers per run and per step, the &lt;a href="https://www.uncoil.dev/product/cost-dashboard" rel="noopener noreferrer"&gt;cost dashboard&lt;/a&gt; shows the setup. Whatever you use, check which way your provider counts cached tokens before you trust a total.&lt;/p&gt;

</description>
      <category>openai</category>
      <category>python</category>
      <category>ai</category>
      <category>llm</category>
    </item>
    <item>
      <title>LangGraph's recursion limit is 25 in JavaScript and 10,007 in Python</title>
      <dc:creator>uncoil</dc:creator>
      <pubDate>Mon, 30 Nov 2026 08:57:18 +0000</pubDate>
      <link>https://dev.to/uncoil/langgraphs-recursion-limit-is-25-in-javascript-and-10007-in-python-15nn</link>
      <guid>https://dev.to/uncoil/langgraphs-recursion-limit-is-25-in-javascript-and-10007-in-python-15nn</guid>
      <description>&lt;p&gt;If you build the same LangGraph agent in Python and in JavaScript, a stuck loop behaves very differently in each.&lt;/p&gt;

&lt;p&gt;I wrote a two-node graph that never reaches &lt;code&gt;END&lt;/code&gt; and ran it with no config on current releases (&lt;code&gt;langgraph&lt;/code&gt; 1.2.24 for Python, &lt;code&gt;@langchain/langgraph&lt;/code&gt; 1.7.5 for JavaScript):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;JavaScript: Recursion limit of 25 reached without hitting a stop condition.
Python:     Recursion limit of 10007 reached without hitting a stop condition.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;JavaScript still stops a runaway graph after 25 steps. Python now lets it run for 10,007. If every pass through the loop calls a model, that is the difference between a quick, cheap failure and thousands of paid model calls before anything raises.&lt;/p&gt;

&lt;p&gt;This post shows the test, explains what a "step" is, and gives three guards you can add today.&lt;/p&gt;

&lt;h2&gt;
  
  
  The test
&lt;/h2&gt;

&lt;p&gt;Here is the Python version. Node &lt;code&gt;b&lt;/code&gt; always routes back to &lt;code&gt;a&lt;/code&gt;, so the graph never ends:&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;from&lt;/span&gt; &lt;span class="n"&gt;typing&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;TypedDict&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langgraph&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;graph&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;StateGraph&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;START&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langgraph&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;errors&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;GraphRecursionError&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;State&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;TypedDict&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;step&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;State&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="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"n"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"n"&lt;/span&gt;&lt;span class="p"&gt;]&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;g&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;StateGraph&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;State&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"a"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;step&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"b"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;step&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_edge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;START&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"a"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_edge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"a"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"b"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_conditional_edges&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"b"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"a"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;compile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"n"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nc"&gt;GraphRecursionError&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&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="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;splitlines&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="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The JavaScript version is the same graph:&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="nc"&gt;StateGraph&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;START&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Annotation&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;GraphRecursionError&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="s2"&gt;"@langchain/langgraph"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nc"&gt;State&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Annotation&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Root&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Annotation&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="o"&gt;&amp;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;reducer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_a&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;default&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&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="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;step&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nc"&gt;State&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;State&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;n&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;n&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="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;StateGraph&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;State&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"a"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;step&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"b"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;step&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEdge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;START&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"a"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEdge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"a"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"b"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addConditionalEdges&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"b"&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"a"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"a"&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="nf"&gt;compile&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;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&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="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&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="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nc"&gt;GraphRecursionError&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nb"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"\n"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the installed packages, the JavaScript default is a constant &lt;code&gt;DEFAULT_RECURSION_LIMIT = 25&lt;/code&gt;. Python reads &lt;code&gt;LANGGRAPH_DEFAULT_RECURSION_LIMIT&lt;/code&gt; from the environment and falls back to &lt;code&gt;10007&lt;/code&gt;. If you pass a limit yourself, both runtimes respect it.&lt;/p&gt;

&lt;h2&gt;
  
  
  What counts as a step
&lt;/h2&gt;

&lt;p&gt;The limit counts supersteps: each round in which LangGraph runs the nodes that are ready. In a classic tool-calling agent, one loop is usually two steps, the model node and then the tool node.&lt;/p&gt;

&lt;p&gt;So with the Python default, a model that keeps asking for the same tool can make about 5,000 model calls before the graph raises. Each call usually carries a growing message history, so later calls cost more than earlier ones. The error only arrives at the end, after all of that spend.&lt;/p&gt;

&lt;p&gt;The low JavaScript limit has the opposite problem. A legitimate long task, such as a research agent visiting many pages, can hit 25 and fail even though nothing is wrong.&lt;/p&gt;

&lt;p&gt;Neither default knows your workload. Pick the limit yourself.&lt;/p&gt;

&lt;h2&gt;
  
  
  Guard 1: always set &lt;code&gt;recursion_limit&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Treat the limit as a budget for the task:&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="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;inputs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"recursion_limit"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;inputs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;recursionLimit&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;40&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A reasonable starting point is the highest step count you see on healthy runs, plus some headroom. If healthy runs need 12 steps, a limit of 25 to 40 catches loops early without breaking real work.&lt;/p&gt;

&lt;h2&gt;
  
  
  Guard 2: end the run cleanly before the limit
&lt;/h2&gt;

&lt;p&gt;When &lt;code&gt;invoke&lt;/code&gt; raises &lt;code&gt;GraphRecursionError&lt;/code&gt;, you get no return value. Often you would rather stop one step early and return what you have, or hand off to a person.&lt;/p&gt;

&lt;p&gt;Python LangGraph has a managed value for this. Add &lt;code&gt;RemainingSteps&lt;/code&gt; to your state and route to &lt;code&gt;END&lt;/code&gt; when it gets low:&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;from&lt;/span&gt; &lt;span class="n"&gt;typing&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;TypedDict&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langgraph&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;graph&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;StateGraph&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;START&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;END&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;langgraph&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;managed&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;RemainingSteps&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;State&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;TypedDict&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
    &lt;span class="n"&gt;remaining_steps&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;RemainingSteps&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;State&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;state&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"remaining_steps"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;END&lt;/span&gt;          &lt;span class="c1"&gt;# stop and return partial work&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"a"&lt;/span&gt;

&lt;span class="n"&gt;g&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;StateGraph&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;State&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"a"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;step&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"b"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;step&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_edge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;START&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"a"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_edge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"a"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"b"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_conditional_edges&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"b"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;route&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"a"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;END&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;compile&lt;/span&gt;&lt;span class="p"&gt;(&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;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"n"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&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="s2"&gt;"recursion_limit"&lt;/span&gt;&lt;span class="p"&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="c1"&gt;# {'n': 10}, no exception&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;LangGraph fills in &lt;code&gt;remaining_steps&lt;/code&gt; for you. You never set it yourself.&lt;/p&gt;

&lt;h2&gt;
  
  
  Guard 3: watch the path, not just the final error
&lt;/h2&gt;

&lt;p&gt;When a run does hit the limit, the question is which step should have ended the loop. Streaming updates shows the path the graph took:&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;for&lt;/span&gt; &lt;span class="n"&gt;chunk&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;inputs&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"recursion_limit"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stream_mode&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"updates"&lt;/span&gt;&lt;span class="p"&gt;)&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="kt"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# ['a'], ['b'], ['a'], ['b'], ...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The same node pair repeating is the loop. Then look at the step just before each repeat:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;A router that never returns &lt;code&gt;END&lt;/code&gt;.&lt;/strong&gt; The condition checks a field that no node ever sets, or a typo means it never matches.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A tool that fails the same way each time.&lt;/strong&gt; The model reads the error, calls the tool again with the same arguments, and gets the same error. Catch the error, return a clear message, and count retries in state.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A model that never says it is done.&lt;/strong&gt; The prompt asks for a final answer in a format the router does not recognize. Log the last model message and compare it with what the router expects.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The router reads a different field than the node writes.&lt;/strong&gt; The node stores its result under one key, the router checks another, so the router keeps seeing the starting value.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  A checklist for production graphs
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Set &lt;code&gt;recursion_limit&lt;/code&gt; (&lt;code&gt;recursionLimit&lt;/code&gt; in JS) on every &lt;code&gt;invoke&lt;/code&gt; and &lt;code&gt;stream&lt;/code&gt; call, sized from healthy runs.&lt;/li&gt;
&lt;li&gt;Add a clean exit before the limit (&lt;code&gt;RemainingSteps&lt;/code&gt; in Python) so stuck runs return partial work instead of an exception.&lt;/li&gt;
&lt;li&gt;Catch &lt;code&gt;GraphRecursionError&lt;/code&gt; at the request boundary so one stuck run does not crash the request.&lt;/li&gt;
&lt;li&gt;Record each step of a run with its node name, model call, tool call and token count, so a failed run shows the repeat at a glance.&lt;/li&gt;
&lt;li&gt;Alert on steps per run, not only on errors. A run that uses 300 steps and then succeeds is still a problem worth seeing.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Why I looked into this
&lt;/h2&gt;

&lt;p&gt;I build &lt;a href="https://www.uncoil.dev" rel="noopener noreferrer"&gt;Uncoil&lt;/a&gt;, an open-source tracing tool for AI agents. A run that loops shows up as one long timeline of the same model call and tool call, with the cost adding up step by step. The &lt;a href="https://www.uncoil.dev/integrations/langgraph" rel="noopener noreferrer"&gt;LangGraph integration page&lt;/a&gt; shows the setup. Whatever you use to watch your agents, check which default your runtime ships with, because the two LangGraph runtimes disagree by 10,000 steps.&lt;/p&gt;

</description>
      <category>langchain</category>
      <category>python</category>
      <category>ai</category>
      <category>javascript</category>
    </item>
    <item>
      <title>How to debug an AI agent in production: a 6-step incident workflow</title>
      <dc:creator>uncoil</dc:creator>
      <pubDate>Mon, 30 Nov 2026 06:41:54 +0000</pubDate>
      <link>https://dev.to/uncoil/how-to-debug-an-ai-agent-in-production-a-6-step-incident-workflow-2nic</link>
      <guid>https://dev.to/uncoil/how-to-debug-an-ai-agent-in-production-a-6-step-incident-workflow-2nic</guid>
      <description>&lt;p&gt;Production debugging starts when the user-visible result is wrong, incomplete, unsafe, or too slow—not when a model request returns an error. The job is to protect users first, reconstruct the path, identify the mechanism, and verify that the fix changes the outcome without creating a new failure.&lt;/p&gt;

&lt;h2&gt;
  
  
  At a glance
&lt;/h2&gt;

&lt;p&gt;This guide is for on-call engineers and incident responders. It helps you &lt;strong&gt;move from symptom to mechanism to proof&lt;/strong&gt; so you can leave with a contained incident, a reproducible hypothesis, and a fix that has a verification plan.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Before you start&lt;/th&gt;
&lt;th&gt;What to define&lt;/th&gt;
&lt;th&gt;What good looks like&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;A user report, alert, or representative failed run&lt;/td&gt;
&lt;td&gt;Impact, trace boundary, comparison case, owner, and stop condition&lt;/td&gt;
&lt;td&gt;Users are protected while the team explains the failure and proves the change&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  The debugging loop
&lt;/h3&gt;

&lt;p&gt;Use &lt;strong&gt;Stabilize → Reconstruct → Compare → Correct → Verify&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Stabilize:&lt;/strong&gt; protect users with pause, fallback, rollback, or a narrower cohort.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reconstruct:&lt;/strong&gt; follow the parent task through evidence, decisions, tools, retries, and state.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Compare:&lt;/strong&gt; contrast the failing path with a healthy run from the same workflow and release.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Correct:&lt;/strong&gt; address the mechanism in code, data, prompt, policy, dependency, or operations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Verify:&lt;/strong&gt; replay the case, test neighbors, and watch the production signal that exposed it.&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Decision rule:&lt;/strong&gt; Do not change the prompt while the failure boundary is still unknown. First preserve the path that proves what happened.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  1. Stabilize users before diagnosis
&lt;/h2&gt;

&lt;p&gt;Choose the smallest reversible action that limits harm. Pause a high-consequence action, roll back a release, narrow the affected cohort, route to human review, or activate a known-safe fallback. Record the start time, impact, owner, affected workflow, and decision so diagnosis does not erase incident control.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Situation&lt;/th&gt;
&lt;th&gt;First containment&lt;/th&gt;
&lt;th&gt;Avoid&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Active harmful action&lt;/td&gt;
&lt;td&gt;Pause the action boundary and require approval&lt;/td&gt;
&lt;td&gt;Repeating the same call to gather more logs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Release regression&lt;/td&gt;
&lt;td&gt;Roll back or narrow exposure&lt;/td&gt;
&lt;td&gt;Editing production prompts without a baseline&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Dependency outage&lt;/td&gt;
&lt;td&gt;Use bounded fallback or degraded state&lt;/td&gt;
&lt;td&gt;Infinite retries that multiply load&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Unclear impact&lt;/td&gt;
&lt;td&gt;Sample and classify representative runs&lt;/td&gt;
&lt;td&gt;Treating one vivid transcript as the distribution&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The &lt;a href="https://www.uncoil.dev/blog/ai-agent-reliability-failures-retries-guardrails" rel="noopener noreferrer"&gt;AI agent reliability guide&lt;/a&gt; covers bounded retries and terminal states. Containment is part of debugging because an uncontained investigation changes the evidence while the incident is still active.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Reconstruct the execution timeline
&lt;/h2&gt;

&lt;p&gt;Open the parent task, not just the last model response. Preserve the user request or input class, expected outcome, release and prompt versions, retrieved evidence, model calls, tool requests and responses, validation, retries, approvals, timing, final state, and user effect. Redact secrets and use protected references for sensitive content.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Timeline question&lt;/th&gt;
&lt;th&gt;Evidence to open&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;What task was the system attempting?&lt;/td&gt;
&lt;td&gt;Parent run, input class, expected outcome&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Which transition changed the path?&lt;/td&gt;
&lt;td&gt;Child spans, retrieval, tool, validator, policy, or retry events&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;What context was available?&lt;/td&gt;
&lt;td&gt;Source IDs, versions, tool results, permissions, prompt/model markers&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;What did the user receive or experience?&lt;/td&gt;
&lt;td&gt;Terminal status, response, fallback, action result, correction&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The &lt;a href="https://www.uncoil.dev/blog/ai-agent-observability-complete-guide" rel="noopener noreferrer"&gt;AI agent observability guide&lt;/a&gt; describes the evidence model. A trace is useful when it shortens the path from user effect to an owned hypothesis; collecting more unstructured logs is not the same as reconstructing a run.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The first useful debugging artifact is a timeline that connects the user effect to the transition that changed the path.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Compare failing and healthy runs
&lt;/h2&gt;

&lt;p&gt;Find a known-good case from the same workflow, task class, and release whenever possible. Compare like with like: input conditions, retrieved sources, model and prompt versions, tool state, retry path, timing, policy result, and final outcome. If the healthy comparison is from an easier cohort or different release, say so and limit the claim.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Difference&lt;/th&gt;
&lt;th&gt;Candidate mechanism&lt;/th&gt;
&lt;th&gt;Next check&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Required source absent&lt;/td&gt;
&lt;td&gt;Retrieval, indexing, filter, or freshness issue&lt;/td&gt;
&lt;td&gt;Inspect corpus version and source selection&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Tool response differs&lt;/td&gt;
&lt;td&gt;Dependency state or contract change&lt;/td&gt;
&lt;td&gt;Replay with recorded request and validate schema&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Same evidence, different decision&lt;/td&gt;
&lt;td&gt;Prompt, model, policy, or context interpretation&lt;/td&gt;
&lt;td&gt;Compare versions and criterion-level outcomes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Same path, slower result&lt;/td&gt;
&lt;td&gt;Retry, provider, queue, or critical-path latency&lt;/td&gt;
&lt;td&gt;Inspect child-span timing and deadlines&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Action changed external state&lt;/td&gt;
&lt;td&gt;Missing authority or idempotency boundary&lt;/td&gt;
&lt;td&gt;Review precondition, approval, and postcondition&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Avoid the attractive explanation that is easiest to edit. The &lt;a href="https://www.uncoil.dev/blog/rag-evaluation-guide" rel="noopener noreferrer"&gt;RAG evaluation guide&lt;/a&gt; shows why missing or irrelevant evidence should be separated from a synthesis failure before changing generation behavior.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Classify the mechanism and choose the correction
&lt;/h2&gt;

&lt;p&gt;Write the hypothesis as a mechanism: “the freshness check was absent,” “the tool contract accepted a partial result,” or “the retry classified a deterministic validation error as transient.” Then identify the smallest correction that changes that mechanism and the evidence that will show recurrence.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;incident: renewal_guidance_wrong_commitment
mechanism: stale_account_state_passed_as_current
correction: require_freshness_check_before_drafting
regression_case: renewal_state_older_than_request
watch: blocked_action_rate_and_user_correction_rate
owner: account-workflow
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is an illustrative incident packet, not executable configuration. Keep observations separate from interpretation, name the owner, and record what would falsify the hypothesis. A correction may belong in code, data, retrieval, prompt, policy, dependency handling, or operations.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Correction rule:&lt;/strong&gt; Prefer the change that makes the failure impossible or visible at its boundary. A prompt edit is insufficient when the system lacks a state or authority control.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  5. Verify the fix before closing
&lt;/h2&gt;

&lt;p&gt;Replay the original case with the corrected version, then test neighboring cases that could regress: empty input, ambiguous intent, stale state, partial tool result, duplicate action, long context, and a dependency timeout. Compare user outcome, failure class, latency, cost, safety, and escalation—not just whether the final response looks better.&lt;/p&gt;

&lt;p&gt;After release, watch the signal that exposed the incident for a defined observation window. Preserve the candidate and baseline versions, cohort, sample rule, and rollback condition. If the result improves one dimension while harming another, record the trade-off and decide whether the exposure remains appropriate.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://www.uncoil.dev/blog/ai-agent-evaluation-practical-guide" rel="noopener noreferrer"&gt;AI agent testing guide&lt;/a&gt; explains how to turn the confirmed mechanism into the lowest useful durable test. The best incident review ends with a new test, contract, alert, runbook step, or explicit risk decision.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Turn the mechanism into a regression case
&lt;/h2&gt;

&lt;p&gt;An incident becomes useful after the immediate correction, when the next change cannot quietly recreate it. Convert the confirmed mechanism into a case that preserves the condition that made the failure possible. Keep the case focused: one input condition, one expected user outcome, and one observable assertion.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Incident condition&lt;/th&gt;
&lt;th&gt;Expected behavior&lt;/th&gt;
&lt;th&gt;Evidence to assert&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Retrieval returns no applicable policy&lt;/td&gt;
&lt;td&gt;Abstain, ask for more information, or route to review&lt;/td&gt;
&lt;td&gt;No unsupported commitment appears in the result&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Account state is older than the workflow allows&lt;/td&gt;
&lt;td&gt;Refresh state before making a decision&lt;/td&gt;
&lt;td&gt;The stale timestamp is rejected or refreshed&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;A write request times out with an unknown result&lt;/td&gt;
&lt;td&gt;Reconcile before retrying&lt;/td&gt;
&lt;td&gt;The action is not duplicated&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;A tool returns a partial record&lt;/td&gt;
&lt;td&gt;Validate required fields and take a safe fallback&lt;/td&gt;
&lt;td&gt;Missing fields cannot pass as a complete result&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The expected behavior should describe what the user or downstream system is allowed to observe. It is rarely useful to require the model to return one exact sentence. For a refusal, assert that the agent does not claim an action succeeded. For a tool call, assert that the arguments satisfy the contract and that authorization was checked. For a cost regression, assert a bounded call count or budget decision rather than a brittle token total.&lt;/p&gt;

&lt;h3&gt;
  
  
  Preserve a fair baseline
&lt;/h3&gt;

&lt;p&gt;Save the input class, relevant source or state version, prompt and model versions, tool contract, and expected outcome. Use stable synthetic identifiers for private data. A case that depends on a moving production record is difficult to reproduce and may expose data that should not be copied into a test set.&lt;/p&gt;

&lt;p&gt;Keep the original failing case unchanged. If you later improve the expected behavior or replace the case, record why; otherwise a test can become green simply because the team weakened the expectation. A passing score should mean that the workflow respected the required boundary, not that a judge liked the wording.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Regression rule:&lt;/strong&gt; Keep one case that reproduces the incident and several neighboring cases that test the same boundary under different conditions.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Neighboring cases matter because a narrow fix can trade one failure for another. A retrieval filter may stop the wrong document but also hide the correct one. A stricter timeout may prevent a long retry loop but increase safe escalations. Include a healthy case, the original failure, and at least one plausible edge case before treating the correction as complete.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;This is the first half of a longer guide. The rest covers keeping diagnostic evidence safe, rolling out a fix without hiding a new failure, and the handoff note another responder can use: &lt;a href="https://www.uncoil.dev/blog/debug-ai-agents-in-production" rel="noopener noreferrer"&gt;How to Debug AI Agents in Production&lt;/a&gt;. I build &lt;a href="https://www.uncoil.dev" rel="noopener noreferrer"&gt;Uncoil&lt;/a&gt;, an open source tracing tool for AI agents.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>debugging</category>
      <category>llm</category>
      <category>observability</category>
    </item>
    <item>
      <title>AI agent observability: what to record and why</title>
      <dc:creator>uncoil</dc:creator>
      <pubDate>Tue, 20 Oct 2026 02:32:29 +0000</pubDate>
      <link>https://dev.to/uncoil/ai-agent-observability-what-to-record-and-why-1g4o</link>
      <guid>https://dev.to/uncoil/ai-agent-observability-what-to-record-and-why-1g4o</guid>
      <description>&lt;p&gt;AI agent observability is the practice of recording what an agent tried, which evidence shaped each step, and what the user experienced, so you can debug failures, control cost and ship changes safely. It should answer more than "did the request finish?" It should help a person explain what the agent tried, which evidence shaped the path, what the user experienced, and what action is safe next.&lt;/p&gt;

&lt;h2&gt;
  
  
  At a glance
&lt;/h2&gt;

&lt;p&gt;This guide is for platform operators, engineers, and leaders responsible for agents in production. It helps you &lt;strong&gt;connect outcomes to inspectable execution evidence&lt;/strong&gt;, so you can leave with a trace that supports debugging, evaluation, ownership, and change.&lt;/p&gt;

&lt;h2&gt;
  
  
  The observability loop
&lt;/h2&gt;

&lt;p&gt;Use &lt;strong&gt;Frame → Follow → Assess → Respond → Learn&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Frame:&lt;/strong&gt; name the user task, expected outcome, and trust boundary.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Follow:&lt;/strong&gt; connect model calls, retrieval, tools, retries, approvals, and state changes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Assess:&lt;/strong&gt; compare outcome quality, reliability, latency, cost, and policy results.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Respond:&lt;/strong&gt; route the evidence to an owner with a safe first action.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Learn:&lt;/strong&gt; turn confirmed mechanisms into tests, controls, alerts, or release decisions.&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Decision rule:&lt;/strong&gt; Instrument the evidence a person needs to change behavior. More events are not automatically more observability.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Disclosure: I build Uncoil, a tracing tool for AI agents.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://www.uncoil.dev/blog/ai-agent-observability-complete-guide" rel="noopener noreferrer"&gt;Read the full guide on uncoil.dev&lt;/a&gt;&lt;/strong&gt; for the six steps, the trace shape, and the operational checklist.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>devops</category>
      <category>testing</category>
      <category>observability</category>
    </item>
  </channel>
</rss>
