<?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: BlackBadHope</title>
    <description>The latest articles on DEV Community by BlackBadHope (@blackbadhope).</description>
    <link>https://dev.to/blackbadhope</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%2F4272715%2F893fc7e9-5768-4630-bcb7-5bddc4b7ede3.jpg</url>
      <title>DEV Community: BlackBadHope</title>
      <link>https://dev.to/blackbadhope</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/blackbadhope"/>
    <language>en</language>
    <item>
      <title>Why a 97.4% Prompt-Cache Hit Rate Still Hid $12.06 of Waste in One 232-Turn Session</title>
      <dc:creator>BlackBadHope</dc:creator>
      <pubDate>Wed, 02 Dec 2026 10:06:59 +0000</pubDate>
      <link>https://dev.to/blackbadhope/why-a-974-prompt-cache-hit-rate-still-hid-1206-of-waste-in-one-232-turn-session-c1l</link>
      <guid>https://dev.to/blackbadhope/why-a-974-prompt-cache-hit-rate-still-hid-1206-of-waste-in-one-232-turn-session-c1l</guid>
      <description>&lt;p&gt;Last week I pulled a real 232-turn local transcript from our Windows/Linux ISP ops repo because weekly coding-agent quota kept draining even though the headline prompt-cache hit rate looked healthy at &lt;code&gt;97.4%&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Parsing the raw JSONL turn-by-turn showed why aggregate cache hit percentages mislead you on long sessions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Turns:&lt;/strong&gt; &lt;code&gt;232&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Total context processed:&lt;/strong&gt; &lt;code&gt;40.08M&lt;/code&gt; tokens&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Peak single-turn context:&lt;/strong&gt; &lt;code&gt;352.7k&lt;/code&gt; tokens (crossed &lt;code&gt;80k&lt;/code&gt; on turn &lt;code&gt;11&lt;/code&gt; and rode &lt;code&gt;221&lt;/code&gt; turns without compacting)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Apparent cache hit rate:&lt;/strong&gt; &lt;code&gt;97.4%&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Effective cache hit rate:&lt;/strong&gt; &lt;code&gt;10.9%&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Estimated spend (list rates):&lt;/strong&gt; &lt;code&gt;$16.01&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Recoverable waste:&lt;/strong&gt; &lt;code&gt;$12.06&lt;/code&gt; (&lt;code&gt;75.3%&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Detected events:&lt;/strong&gt; &lt;code&gt;2&lt;/code&gt; cache-bust cliffs (&lt;code&gt;99% -&amp;gt; 0%&lt;/code&gt; on a &lt;code&gt;210k&lt;/code&gt; window), &lt;code&gt;6&lt;/code&gt; consecutive Bash retry loops, &lt;code&gt;2&lt;/code&gt; oversized tool blobs (including a &lt;code&gt;16k&lt;/code&gt;-token command output on turn &lt;code&gt;13&lt;/code&gt; re-read &lt;code&gt;219&lt;/code&gt; times)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A &lt;code&gt;97.4%&lt;/code&gt; hit rate sounds great until you notice that &lt;code&gt;90%&lt;/code&gt; of those cached reads are re-reading stale history and giant command dumps across 200+ turns.&lt;/p&gt;

&lt;h2&gt;
  
  
  Exact JSONL Fields Across Agent Logs
&lt;/h2&gt;

&lt;p&gt;The offline parser in &lt;code&gt;token-audit-engine.js&lt;/code&gt; (browser) and &lt;code&gt;agent-token-forensics&lt;/code&gt; (Python stdlib CLI) normalizes four provider log shapes into the same 4-tuple &lt;code&gt;(input_tokens, output_tokens, cache_read_tokens, cache_write_tokens)&lt;/code&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Anthropic / Claude Code (&lt;code&gt;usage&lt;/code&gt;):&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;input_tokens&lt;/code&gt;, &lt;code&gt;output_tokens&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;cache_read_input_tokens&lt;/code&gt; (fallback: &lt;code&gt;cache_read_tokens&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;cache_creation_input_tokens&lt;/code&gt; (fallback: &lt;code&gt;cache_creation_tokens&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;OpenAI Responses / Codex CLI (&lt;code&gt;event_msg&lt;/code&gt; &lt;code&gt;token_count&lt;/code&gt; -&amp;gt; &lt;code&gt;last_token_usage&lt;/code&gt; or diff of cumulative &lt;code&gt;total_token_usage&lt;/code&gt;):&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;input_tokens&lt;/code&gt;, &lt;code&gt;output_tokens&lt;/code&gt;, &lt;code&gt;cached_input_tokens&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Note: &lt;code&gt;cached_input_tokens&lt;/code&gt; is a subset of &lt;code&gt;input_tokens&lt;/code&gt;, so uncached input is &lt;code&gt;max(0, input_tokens - cached_input_tokens)&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;OpenAI Chat Completions (&lt;code&gt;usage&lt;/code&gt;):&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;prompt_tokens&lt;/code&gt;, &lt;code&gt;completion_tokens&lt;/code&gt;, &lt;code&gt;prompt_tokens_details.cached_tokens&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Gemini (&lt;code&gt;usageMetadata&lt;/code&gt;):&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;promptTokenCount&lt;/code&gt;, &lt;code&gt;candidatesTokenCount&lt;/code&gt;, &lt;code&gt;cachedContentTokenCount&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Per turn, total context and apparent cache hit are:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="n"&gt;total_context&lt;/span&gt; &lt;span class="o"&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="n"&gt;cache_read_tokens&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;cache_write_tokens&lt;/span&gt;
&lt;span class="n"&gt;cache_hit_ratio&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;total_context&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="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;cache_read_tokens&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;total_context&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How Effective Cache Hit Is Computed
&lt;/h2&gt;

&lt;p&gt;To separate productive cached prefix reads from wasted re-reads, the engine tracks &lt;code&gt;perTurnWastedCr[k]&lt;/code&gt; on every turn &lt;code&gt;k&lt;/code&gt; across three waste buckets:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Uncompacted tail past &lt;code&gt;80,000&lt;/code&gt; tokens&lt;/strong&gt; (default &lt;code&gt;compactThreshold = 80_000&lt;/code&gt;, &lt;code&gt;postCompactTarget = 20_000&lt;/code&gt;):

&lt;ul&gt;
&lt;li&gt;Once context crosses &lt;code&gt;80k&lt;/code&gt;, every subsequent turn carries &lt;code&gt;excess = max(0, total_context - 20_000)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Wasted cached reads on turn &lt;code&gt;k&lt;/code&gt;: &lt;code&gt;round(cache_read_tokens * (excess / total_context))&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Estimated dollar waste on turn &lt;code&gt;k&lt;/code&gt;: &lt;code&gt;turn_cost * (excess / total_context) * 0.85&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Consecutive identical tool-retry loops&lt;/strong&gt; (&lt;code&gt;loopThreshold = 3&lt;/code&gt; turns):

&lt;ul&gt;
&lt;li&gt;When the same tool signature repeats &lt;code&gt;&amp;gt;= 3&lt;/code&gt; consecutive turns, every repeated turn after the first counts its full &lt;code&gt;cache_read_tokens&lt;/code&gt; as wasted.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Oversized tool output blobs&lt;/strong&gt; (&lt;code&gt;bloatCharThreshold = 24_000&lt;/code&gt; chars or &lt;code&gt;bloatDeltaThreshold = 15_000&lt;/code&gt; context jump on a non-cache-bust turn):

&lt;ul&gt;
&lt;li&gt;Excess tokens above &lt;code&gt;2,000&lt;/code&gt; (&lt;code&gt;excessBlob = max(0, blobTokens - 2_000)&lt;/code&gt;) are billed once at ingest (&lt;code&gt;cache_write&lt;/code&gt; or &lt;code&gt;input&lt;/code&gt; rate) and then carried at &lt;code&gt;cache_read&lt;/code&gt; rate on every subsequent turn &lt;code&gt;k &amp;gt; pos&lt;/code&gt;, contributing &lt;code&gt;min(cache_read_tokens, excessBlob)&lt;/code&gt; to &lt;code&gt;perTurnWastedCr[k]&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We take the per-turn maximum across those buckets so overlapping waste is never double-counted, clamp to &lt;code&gt;cache_read_tokens&lt;/code&gt;, sum &lt;code&gt;totalWastedCacheReadTokens&lt;/code&gt;, and compute:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="n"&gt;effective_cache_hit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
  &lt;span class="n"&gt;total_context_sum&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="o"&gt;?&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;max&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;total_cache_read&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;total_wasted_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;total_context_sum&lt;/span&gt;
    &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Separately, the &lt;strong&gt;cache-bust cliff detector&lt;/strong&gt; fires whenever the previous turn was warm (&lt;code&gt;total_context &amp;gt;= 10_000&lt;/code&gt; and &lt;code&gt;cache_hit_ratio &amp;gt;= 0.7&lt;/code&gt;) and the current turn has &lt;code&gt;total_context &amp;gt;= 10_000&lt;/code&gt; with &lt;code&gt;cache_hit_ratio &amp;lt; 0.3&lt;/code&gt;. Extra dollar cost is &lt;code&gt;(input_tokens * (inRate - cacheReadRate) + cache_write_tokens * (cacheWriteRate - cacheReadRate)) / 1_000_000&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Per-turn dollar waste is deduplicated and capped at &lt;code&gt;95%&lt;/code&gt; of the turn's cost:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="n"&gt;combinedTurnWaste&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;turnCost&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;0.95&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;loopWaste&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;bustWaste&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tailWaste&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bloatWaste&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 our 232-turn trace, &lt;code&gt;total_cache_read / total_context_sum&lt;/code&gt; was &lt;code&gt;97.4%&lt;/code&gt;, while &lt;code&gt;(total_cache_read - total_wasted_cache_read) / total_context_sum&lt;/code&gt; dropped to &lt;code&gt;10.9%&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Running It Locally (Zero Upload)
&lt;/h2&gt;

&lt;p&gt;Private repo transcripts should never leave your machine, so both interfaces run offline:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Browser auditor (100% client-side JS, works with Wi-Fi off):&lt;/strong&gt; &lt;a href="https://integrityharness.dev/token-audit" rel="noopener noreferrer"&gt;https://integrityharness.dev/token-audit&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;MIT Python CLI and read-only stdio MCP (&lt;code&gt;v0.1.1&lt;/code&gt;, 0 runtime deps, 39 tests on Win/Linux/macOS CI):&lt;/strong&gt; &lt;a href="https://github.com/BlackBadHope/agent-token-forensics" rel="noopener noreferrer"&gt;https://github.com/BlackBadHope/agent-token-forensics&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pipx install agent-token-forensics

&lt;span class="c"&gt;# Preview on built-in synthetic trace&lt;/span&gt;
agent-token-forensics analyze --demo

&lt;span class="c"&gt;# Auto-scan local Claude Code / Codex / Antigravity JSONL logs&lt;/span&gt;
agent-token-forensics analyze

&lt;span class="c"&gt;# Expose strictly read-only stdio MCP tools (analyze_transcripts, forensics_about)&lt;/span&gt;
agent-token-forensics mcp-serve
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Scope note: this is strictly read-only offline forensics. Dollar figures are deterministic estimates from logged token counts and public list rates, not provider invoices, and the tool does not intercept or auto-compact live sessions.&lt;/p&gt;

&lt;p&gt;What worked best on our own repo after reading these traces: compacting hard around &lt;code&gt;80k-120k&lt;/code&gt;, piping noisy shell commands through &lt;code&gt;head&lt;/code&gt;/&lt;code&gt;tail&lt;/code&gt;/&lt;code&gt;rg&lt;/code&gt; or writing to a scratch file, and stopping identical retry loops after 2 failures.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>python</category>
      <category>devops</category>
      <category>opensource</category>
    </item>
    <item>
      <title>Why "Tests Passed" From Your AI Coding Agent Isn't Proof (SHA-256 Digest-Bound Evidence Gate)</title>
      <dc:creator>BlackBadHope</dc:creator>
      <pubDate>Tue, 01 Dec 2026 13:37:52 +0000</pubDate>
      <link>https://dev.to/blackbadhope/why-tests-passed-from-your-ai-coding-agent-isnt-proof-sha-256-digest-bound-evidence-gate-17b3</link>
      <guid>https://dev.to/blackbadhope/why-tests-passed-from-your-ai-coding-agent-isnt-proof-sha-256-digest-bound-evidence-gate-17b3</guid>
      <description>&lt;p&gt;After 10+ years running Linux ISP production infrastructure, the scariest line from a coding agent is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"All tests passed. Ready to deploy."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In long Claude Code, Codex, or Cursor sessions, two opposite bugs happen constantly:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Stale green claims:&lt;/strong&gt; the agent runs a test, edits another file two turns later, and still treats the earlier pass as valid.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rerun &amp;amp; polling burn:&lt;/strong&gt; the agent burns 30-60% of its context re-running the same green suite or polling unchanged deploy logs in a loop.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;A logged run is not proof unless bound to the exact artifact digest.&lt;/p&gt;

&lt;p&gt;So I extracted our gate into a zero-dependency Python 3.10+ CLI: &lt;strong&gt;&lt;code&gt;integrity-evidence-gate&lt;/code&gt;&lt;/strong&gt; (MIT, 0 telemetry, PyPI).&lt;/p&gt;

&lt;h2&gt;
  
  
  Install
&lt;/h2&gt;

&lt;p&gt;Always install the scoped PyPI name &lt;strong&gt;&lt;code&gt;integrity-evidence-gate&lt;/code&gt;&lt;/strong&gt;, not unscoped &lt;code&gt;evidence-gate&lt;/code&gt; (unrelated third party):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pipx install integrity-evidence-gate
evidence-gate run -- pytest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or zero-install via &lt;code&gt;uvx&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;uvx --from integrity-evidence-gate evidence-gate run -- pytest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  1. Digest-Bound Test Gate (&lt;code&gt;evidence-gate run&lt;/code&gt;)
&lt;/h2&gt;

&lt;p&gt;Hashes watched &lt;code&gt;--artifact&lt;/code&gt; paths (symlink-safe SHA-256, ignoring &lt;code&gt;.git&lt;/code&gt;/&lt;code&gt;__pycache__&lt;/code&gt;/&lt;code&gt;node_modules&lt;/code&gt;), OS/Python env, and exact &lt;code&gt;argv&lt;/code&gt; (&lt;code&gt;shell=False&lt;/code&gt;):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Cold run:&lt;/strong&gt; runs and appends to a local JSONL ledger (&lt;code&gt;~/.evidence-gate&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Unchanged rerun:&lt;/strong&gt; returns &lt;code&gt;SKIP_CACHED_PASS&lt;/code&gt; in milliseconds (&lt;code&gt;command_calls=0&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;1-byte diff:&lt;/strong&gt; invalidates immediately. Failures never cache.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Concurrency:&lt;/strong&gt; atomic &lt;code&gt;O_CREAT | O_EXCL&lt;/code&gt; file lease (&lt;code&gt;exit 75&lt;/code&gt; on duplicate run).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Forced rerun:&lt;/strong&gt; &lt;code&gt;--force&lt;/code&gt; requires &lt;code&gt;--force-reason&lt;/code&gt; (fails closed, &lt;code&gt;exit 2&lt;/code&gt;).
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;evidence-gate run --kind targeted --suite-id auth-unit --reason &lt;span class="s2"&gt;"verify fix"&lt;/span&gt; \
  --artifact src/auth --artifact tests/test_auth.py \
  -- python -m unittest tests/test_auth.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. Loop-Breaking Monitor (&lt;code&gt;evidence-gate observe&lt;/code&gt;)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;evidence-gate observe --monitor-id deploy-rollout --reason &lt;span class="s2"&gt;"watch rollout"&lt;/span&gt; \
  --min-interval-seconds 60 --max-unchanged 3 --json -- python scripts/check_rollout.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Caps polling via &lt;code&gt;--min-interval-seconds&lt;/code&gt;, &lt;code&gt;--deadline-utc&lt;/code&gt;, and &lt;code&gt;--max-unchanged&lt;/code&gt; (&lt;code&gt;STOP_UNCHANGED&lt;/code&gt;, &lt;code&gt;STOP_DEADLINE&lt;/code&gt;, &lt;code&gt;SKIP_TOO_SOON&lt;/code&gt;).&lt;/p&gt;

&lt;h2&gt;
  
  
  Boundary
&lt;/h2&gt;

&lt;p&gt;It only hashes declared paths. Live DB/API tests need &lt;code&gt;--freshness ttl&lt;/code&gt; or &lt;code&gt;--freshness volatile&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Repo (MIT, 0 deps): &lt;a href="https://github.com/BlackBadHope/evidence-gate" rel="noopener noreferrer"&gt;https://github.com/BlackBadHope/evidence-gate&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Full stack: &lt;a href="https://integrityharness.dev" rel="noopener noreferrer"&gt;https://integrityharness.dev&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;How are you stopping stale passes and test-rerun loops?&lt;/p&gt;

</description>
      <category>python</category>
      <category>ai</category>
      <category>devops</category>
      <category>testing</category>
    </item>
    <item>
      <title>Why Prompt Guardrails Fail After 30 Turns — So I Built an OS Process-Tree Killswitch</title>
      <dc:creator>BlackBadHope</dc:creator>
      <pubDate>Mon, 30 Nov 2026 08:05:19 +0000</pubDate>
      <link>https://dev.to/blackbadhope/why-prompt-guardrails-fail-after-30-turns-so-i-built-an-os-process-tree-killswitch-2lll</link>
      <guid>https://dev.to/blackbadhope/why-prompt-guardrails-fail-after-30-turns-so-i-built-an-os-process-tree-killswitch-2lll</guid>
      <description>&lt;p&gt;If you run Claude Code, Codex CLI, Cursor, Gemini CLI, or Antigravity against real Linux and Windows infrastructure, you eventually hit the same failure mode: thirty turns into a debugging session, the context window compacts, your polite system prompt ("never push to main", "ask before touching production") gets diluted, and a background subshell keeps executing commands after you told the agent to stop.&lt;/p&gt;

&lt;p&gt;A text instruction inside an LLM's prompt is not an access control boundary. Even worse, a plain local flag file (&lt;code&gt;~/.killswitch&lt;/code&gt;) fails the moment a confused or prompt-injected agent decides to flip the flag back to &lt;code&gt;false&lt;/code&gt; or spawn a detached shell to bypass your wrapper.&lt;/p&gt;

&lt;p&gt;After 10+ years of running telecom and hybrid Linux/Windows operations, I wanted an emergency stop built out of OS primitives instead of prompt engineering. That became &lt;strong&gt;&lt;code&gt;integrity-killswitch&lt;/code&gt;&lt;/strong&gt; (&lt;code&gt;agent-killswitch&lt;/code&gt; CLI v0.3.0, MIT, zero runtime dependencies, pure Python 3.10+ stdlib, &lt;strong&gt;63 passing tests + 35 subtests&lt;/strong&gt;, now live on PyPI).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;PyPI Package Name Warning:&lt;/strong&gt; The official PyPI distribution is &lt;strong&gt;&lt;code&gt;integrity-killswitch&lt;/code&gt;&lt;/strong&gt; (&lt;code&gt;pipx install integrity-killswitch&lt;/code&gt;, CLI binary: &lt;code&gt;agent-killswitch&lt;/code&gt;). Do &lt;strong&gt;not&lt;/strong&gt; install the unrelated third-party &lt;code&gt;agent-killswitch&lt;/code&gt; package on PyPI — that namespace belongs to another author.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Tighten-Only for Agents, Owner-Only Restore (&lt;code&gt;exit 3&lt;/code&gt;)
&lt;/h3&gt;

&lt;p&gt;The core invariant is asymmetric:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Any agent or script can tighten access:&lt;/strong&gt; &lt;code&gt;panic&lt;/code&gt;, &lt;code&gt;off &amp;lt;vendor&amp;gt;&lt;/code&gt;, &lt;code&gt;scope-off &amp;lt;scope&amp;gt;&lt;/code&gt;, &lt;code&gt;net-cut&lt;/code&gt;, and &lt;code&gt;freeze&lt;/code&gt; are always allowed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Only the human owner can restore access:&lt;/strong&gt; &lt;code&gt;resume&lt;/code&gt;, &lt;code&gt;on&lt;/code&gt;, &lt;code&gt;scope-on&lt;/code&gt;, &lt;code&gt;net-restore&lt;/code&gt;, &lt;code&gt;thaw&lt;/code&gt;, and &lt;code&gt;uninstall-hooks&lt;/code&gt; walk the live OS process tree and &lt;strong&gt;refuse with &lt;code&gt;exit 3&lt;/code&gt; (&lt;code&gt;EXIT_REFUSED&lt;/code&gt;)&lt;/strong&gt; whenever an LLM ancestor or an orphaned/detached shell is detected.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;How the process-tree walk works across all three operating systems without third-party dependencies:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Windows:&lt;/strong&gt; Uses &lt;code&gt;ctypes&lt;/code&gt; to call Win32 &lt;code&gt;CreateToolhelp32Snapshot&lt;/code&gt; (&lt;code&gt;Process32First&lt;/code&gt; / &lt;code&gt;Process32Next&lt;/code&gt;), bridges Git Bash/MSYS &lt;code&gt;fork/exec&lt;/code&gt; chains via &lt;code&gt;ps.exe -e&lt;/code&gt;, and resolves loopback Web UI peers via &lt;code&gt;GetExtendedTcpTable&lt;/code&gt; (&lt;code&gt;iphlpapi&lt;/code&gt;). Orphaned shells (like &lt;code&gt;Start-Process pwsh&lt;/code&gt; whose parent exited) fail closed as &lt;code&gt;unknown_llm&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Linux:&lt;/strong&gt; Walks &lt;code&gt;/proc/&amp;lt;pid&amp;gt;/stat&lt;/code&gt;, &lt;code&gt;exe&lt;/code&gt;, &lt;code&gt;comm&lt;/code&gt;, and &lt;code&gt;cmdline&lt;/code&gt; all the way to PID &lt;code&gt;1&lt;/code&gt;, checks &lt;code&gt;start_time&lt;/code&gt; monotonicity against PID reuse, and re-validates the entire chain on a second pass. Detached &lt;code&gt;tmux&lt;/code&gt;/&lt;code&gt;screen&lt;/code&gt; shells and non-interactive &lt;code&gt;ssh host "cmd"&lt;/code&gt; fail closed as &lt;code&gt;unknown_llm&lt;/code&gt;, while interactive human SSH sessions (&lt;code&gt;sshd&lt;/code&gt;/&lt;code&gt;sshd-session&lt;/code&gt; + &lt;code&gt;SSH_TTY&lt;/code&gt; + live TTY) are recognized as &lt;code&gt;owner&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;macOS:&lt;/strong&gt; Walks &lt;code&gt;/bin/ps -axo pid=,ppid=,comm=&lt;/code&gt; (plus &lt;code&gt;/bin/ps -ww -p &amp;lt;pid&amp;gt; -o args=&lt;/code&gt; for &lt;code&gt;python&lt;/code&gt;/&lt;code&gt;node&lt;/code&gt;/&lt;code&gt;bun&lt;/code&gt;/&lt;code&gt;deno&lt;/code&gt; interpreters) up to &lt;code&gt;launchd&lt;/code&gt; (PID &lt;code&gt;1&lt;/code&gt;) and resolves loopback dashboard peers via &lt;code&gt;/usr/sbin/lsof&lt;/code&gt; without &lt;code&gt;sudo&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Anti-spoofing is enforced in code: &lt;code&gt;check&lt;/code&gt; always runs OS ancestry detection first. Passing &lt;code&gt;--vendor owner&lt;/code&gt; or &lt;code&gt;--vendor codex&lt;/code&gt; from inside a Claude Code subprocess can never downgrade an OS-detected &lt;code&gt;claude&lt;/code&gt; ancestor.&lt;/p&gt;

&lt;h3&gt;
  
  
  Fail-Closed Hooks Across 5 IDEs (&lt;code&gt;exit 2&lt;/code&gt;) + Tighten-Only MCP Server
&lt;/h3&gt;

&lt;p&gt;Hand-editing five different hook schemas is how teams quietly leave one client unguarded. One command wires all five clients idempotently with timestamped &lt;code&gt;.bak&lt;/code&gt; backups and &lt;code&gt;utf-8-sig&lt;/code&gt; (Windows BOM-safe) parsing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Install from PyPI (PEP 668 safe)&lt;/span&gt;
pipx install integrity-killswitch

&lt;span class="c"&gt;# Preview every hook change with zero writes&lt;/span&gt;
agent-killswitch install-hooks --all --dry-run

&lt;span class="c"&gt;# Wire all 5 IDEs safely&lt;/span&gt;
agent-killswitch install-hooks --all
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That patches:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Claude Code&lt;/strong&gt; (&lt;code&gt;~/.claude/settings.json&lt;/code&gt; → &lt;code&gt;hooks.PreToolUse&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Codex CLI&lt;/strong&gt; (&lt;code&gt;~/.codex/hooks.json&lt;/code&gt; → &lt;code&gt;hooks.PreToolUse&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cursor&lt;/strong&gt; (&lt;code&gt;~/.cursor/hooks.json&lt;/code&gt; → &lt;code&gt;preToolUse&lt;/code&gt;, &lt;code&gt;beforeShellExecution&lt;/code&gt;, &lt;code&gt;beforeMCPExecution&lt;/code&gt; with &lt;code&gt;failClosed: true&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Gemini CLI&lt;/strong&gt; (&lt;code&gt;~/.gemini/settings.json&lt;/code&gt; → &lt;code&gt;hooks.BeforeTool&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Antigravity&lt;/strong&gt; (&lt;code&gt;~/.gemini/config/hooks.json&lt;/code&gt; → &lt;code&gt;"agent-killswitch": {"PreToolUse": [...]}&lt;/code&gt;)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;When a hook denies a tool call, &lt;code&gt;check --hook-format &amp;lt;vendor&amp;gt;&lt;/code&gt; exits with &lt;strong&gt;&lt;code&gt;2&lt;/code&gt; (&lt;code&gt;EXIT_HOOK_BLOCK&lt;/code&gt;)&lt;/strong&gt; and prints exact vendor-native blocking JSON on stdout. Need native MCP tool access? &lt;code&gt;agent-killswitch mcp-serve&lt;/code&gt; runs a zero-dependency JSON-RPC 2.0 &lt;code&gt;stdio&lt;/code&gt; MCP server exposing &lt;strong&gt;only&lt;/strong&gt; read/tighten tools (&lt;code&gt;killswitch_status&lt;/code&gt;, &lt;code&gt;killswitch_check&lt;/code&gt;, &lt;code&gt;killswitch_panic&lt;/code&gt;, &lt;code&gt;killswitch_vendor_off&lt;/code&gt;, &lt;code&gt;killswitch_scope_off&lt;/code&gt;) — &lt;code&gt;resume&lt;/code&gt; and &lt;code&gt;on&lt;/code&gt; are deliberately absent from MCP.&lt;/p&gt;

&lt;h3&gt;
  
  
  Escalating Past Cooperative Hooks (&lt;code&gt;harden&lt;/code&gt;, &lt;code&gt;panic --network --freeze&lt;/code&gt;, &lt;code&gt;cloud-sync&lt;/code&gt;)
&lt;/h3&gt;

&lt;p&gt;When you need more than hook interception:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;agent-killswitch harden&lt;/code&gt;:&lt;/strong&gt; Moves enforcement to a system-owned path (&lt;code&gt;/etc/agent-killswitch/state.json&lt;/code&gt; &lt;code&gt;root:root 0755/0644&lt;/code&gt; on Linux, &lt;code&gt;root:wheel&lt;/code&gt; on macOS; &lt;code&gt;C:\\ProgramData\\agent-killswitch\\state.json&lt;/code&gt; on Windows using locale-independent SIDs), eliminating &lt;code&gt;mv ~/.agent-killswitch&lt;/code&gt; swap bypasses while keeping user &lt;code&gt;audit.jsonl&lt;/code&gt; writable.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;sudo agent-killswitch panic --network --freeze&lt;/code&gt;:&lt;/strong&gt; Combines &lt;code&gt;state.json&lt;/code&gt; Master Panic, kernel-level outbound &lt;strong&gt;TCP + UDP 80/443 (HTTP/HTTPS + QUIC)&lt;/strong&gt; firewall blocks to 10 LLM API providers (&lt;code&gt;nftables&lt;/code&gt;/&lt;code&gt;iptables&lt;/code&gt;, &lt;code&gt;netsh&lt;/code&gt; + PowerShell, or macOS &lt;code&gt;pf&lt;/code&gt;, while always preserving loopback, SSH TCP 22, DNS 53, and &lt;code&gt;--allow-host&lt;/code&gt;), and POSIX &lt;code&gt;SIGSTOP&lt;/code&gt; process-tree freezing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;agent-killswitch cloud-sync&lt;/code&gt;:&lt;/strong&gt; Polls &lt;code&gt;GET &amp;lt;cloud_url&amp;gt;/api/v1/killswitch/state&lt;/code&gt; (stdlib &lt;code&gt;urllib.request&lt;/code&gt;, 3s timeout, 15s TTL cache) and latches local &lt;code&gt;master_panic&lt;/code&gt; the instant a remote fleet PANIC button is pressed.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Honest Threat Model
&lt;/h3&gt;

&lt;p&gt;Security tools lose credibility when they claim magic. Here is the exact boundary:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Same OS user, unhardened state:&lt;/strong&gt; If &lt;code&gt;state.json&lt;/code&gt; lives in your user home directory and a rogue same-user agent ignores client hooks, it can overwrite that file or call LLM APIs directly.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mitigation:&lt;/strong&gt; Run &lt;code&gt;agent-killswitch harden&lt;/code&gt; (root/Admin-owned state + explicit &lt;code&gt;--state&lt;/code&gt; in hooks), escalate with &lt;code&gt;panic --network --freeze&lt;/code&gt; (blocks direct API egress at the OS firewall even if hooks are ignored), or run privileged wrappers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Windows &lt;code&gt;freeze&lt;/code&gt; honesty:&lt;/strong&gt; Windows stdlib has no POSIX &lt;code&gt;SIGSTOP&lt;/code&gt;. Live &lt;code&gt;freeze&lt;/code&gt;/&lt;code&gt;thaw&lt;/code&gt; on Windows honestly returns &lt;code&gt;ok: false, supported: false&lt;/code&gt; (&lt;code&gt;exit 1&lt;/code&gt;) and prints a &lt;code&gt;pssuspend&lt;/code&gt; / &lt;code&gt;Stop-Process&lt;/code&gt; plan instead of faking success.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Compromised root/Admin host:&lt;/strong&gt; Nothing local stops an attacker who already owns &lt;code&gt;root&lt;/code&gt;/&lt;code&gt;SYSTEM&lt;/code&gt;. Pair with an external control plane for server-side key revocation and remote notarization.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  60-Second Quickstart (PyPI or Zero-Install from GitHub)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Recommended: live on PyPI (remember: integrity-killswitch, NOT agent-killswitch)&lt;/span&gt;
pipx install integrity-killswitch
agent-killswitch status

&lt;span class="c"&gt;# Or zero-install directly from source&lt;/span&gt;
git clone https://github.com/BlackBadHope/agent-killswitch
&lt;span class="nb"&gt;cd&lt;/span&gt; agent-killswitch
python3 killswitch.py status
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;Source &amp;amp; Production Extensions:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;PyPI (&lt;code&gt;0.3.0&lt;/code&gt; live):&lt;/strong&gt; &lt;a href="https://pypi.org/project/integrity-killswitch/" rel="noopener noreferrer"&gt;pypi.org/project/integrity-killswitch&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Open-Source CLI (MIT, 63 tests):&lt;/strong&gt; &lt;a href="https://github.com/BlackBadHope/agent-killswitch" rel="noopener noreferrer"&gt;github.com/BlackBadHope/agent-killswitch&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Multi-Host Cloud Control Plane (Fleet PANIC Sync + SHA-256 Cloud Witness, $19/$49 LIVE):&lt;/strong&gt; &lt;a href="https://integrityharness.dev?utm_source=devto&amp;amp;utm_medium=article&amp;amp;utm_campaign=killswitch" rel="noopener noreferrer"&gt;integrityharness.dev&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Offline 1-Click MCP Governance Bundles (Agensi Storefront):&lt;/strong&gt; &lt;a href="https://www.agensi.io/r/BLACKHOPE" rel="noopener noreferrer"&gt;agensi.io/r/BLACKHOPE&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>ai</category>
      <category>security</category>
      <category>devops</category>
      <category>python</category>
    </item>
    <item>
      <title>Your AI Coding Agent's 87% Cache Hit Rate Is Lying to You (JSONL Token Forensics)</title>
      <dc:creator>BlackBadHope</dc:creator>
      <pubDate>Sun, 29 Nov 2026 18:06:38 +0000</pubDate>
      <link>https://dev.to/blackbadhope/your-ai-coding-agents-87-cache-hit-rate-is-lying-to-you-jsonl-token-forensics-3kc9</link>
      <guid>https://dev.to/blackbadhope/your-ai-coding-agents-87-cache-hit-rate-is-lying-to-you-jsonl-token-forensics-3kc9</guid>
      <description>&lt;p&gt;If you run autonomous coding agents (Claude Code, Codex CLI, Cursor, Gemini CLI, or Antigravity) on real repositories, you have seen the same pattern: a single debugging session chews through half a daily quota or racks up a dollar-plus on one task, yet the aggregate metrics look fine.&lt;/p&gt;

&lt;p&gt;An aggregate 87% prompt-cache hit ratio sounds healthy. In practice, averages hide the exact turns where your budget actually died.&lt;/p&gt;

&lt;p&gt;After watching long agent runs burn 30–55% of their context budget on avoidable tail weight, I built &lt;a href="https://github.com/BlackBadHope/agent-token-forensics" rel="noopener noreferrer"&gt;&lt;code&gt;agent-token-forensics&lt;/code&gt;&lt;/a&gt; — a strictly read-only, zero-dependency Python 3.10+ CLI that scans local JSONL transcripts offline and points to the exact file, line, and turn responsible for the burn.&lt;/p&gt;

&lt;h3&gt;
  
  
  What a "Normal" 14-Turn Session Actually Hides
&lt;/h3&gt;

&lt;p&gt;To demonstrate without exposing private work logs, here is real stdout from &lt;code&gt;agent-token-forensics v0.1.0&lt;/code&gt; run against a synthetic 14-turn &lt;code&gt;claude-sonnet-4-6&lt;/code&gt; transcript (&lt;code&gt;demo_session.jsonl&lt;/code&gt;) that reproduces three common failure patterns:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;=== Agent Token &amp;amp; Quota Burn Forensics ===
Files: 1 | Turns: 14 | Est. Cost: $0.9374
Context Tokens: 1,128,900 (cache_read=982,600, uncached=91,300, hit_ratio=87.0%)
Output Tokens: 10,830
Estimated Compaction Savings (threshold=80,000): 49.3% (~$0.2801)
Cache Busts Detected: 1 | Retry Loops: 1

Compaction Inflection Points:
  - demo_session.jsonl: compact after turn 8 (ctx=85,700, tail=6 turns, saveable=556,800 tok)

Retry Loops Detected:
  - demo_session.jsonl turn 8: 4x repeated ['Bash']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the exact &lt;code&gt;--markdown&lt;/code&gt; summary table you can paste straight into a team review or PR post-mortem:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Metric&lt;/th&gt;
&lt;th&gt;Value&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Files analyzed&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Model turns&lt;/td&gt;
&lt;td&gt;14&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Total context tokens&lt;/td&gt;
&lt;td&gt;1,128,900&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Prompt cache hit ratio&lt;/td&gt;
&lt;td&gt;87.0%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Output tokens&lt;/td&gt;
&lt;td&gt;10,830&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Estimated cost&lt;/td&gt;
&lt;td&gt;$0.9374&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Est. compaction savings&lt;/td&gt;
&lt;td&gt;49.3% ($0.2801)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Cache bust events&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Retry loops&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  The 3 Mechanical Root Causes
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;agent-token-forensics&lt;/code&gt; does not guess with another LLM. It parses normalized per-turn usage (&lt;code&gt;input_tokens&lt;/code&gt;, &lt;code&gt;output_tokens&lt;/code&gt;, &lt;code&gt;cache_read&lt;/code&gt;, &lt;code&gt;cache_write&lt;/code&gt;, and tool invocations) across Claude Code/Anthropic, OpenAI/Codex/Cursor (&lt;code&gt;prompt_tokens_details.cached_tokens&lt;/code&gt;), Gemini (&lt;code&gt;usageMetadata&lt;/code&gt;), and Antigravity flat or &lt;code&gt;stream-json&lt;/code&gt; logs, then checks three deterministic rules:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Silent prompt-cache busts (&lt;code&gt;&amp;gt;=70%&lt;/code&gt; collapse to &lt;code&gt;&amp;lt;30%&lt;/code&gt; on &lt;code&gt;&amp;gt;=10k&lt;/code&gt; context):&lt;/strong&gt;
In the &lt;code&gt;--json&lt;/code&gt; output for &lt;code&gt;demo_session.jsonl&lt;/code&gt;, turn 6 (line 7) shows &lt;code&gt;context_tokens: 68800&lt;/code&gt; with &lt;code&gt;cache_hit_ratio: 0.078&lt;/code&gt;. One cache TTL expiry, mid-session MCP tool-list change, or dynamic system-prompt edit forced a cold re-prefill of 54,800 uncached input tokens plus 8,600 cache-write tokens — roughly &lt;code&gt;$0.20&lt;/code&gt; burned on a single turn while the session average still read &lt;code&gt;87.0%&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Uncompacted context tails (crossing &lt;code&gt;--compact-threshold&lt;/code&gt;, default &lt;code&gt;80,000&lt;/code&gt;):&lt;/strong&gt;
Every turn past the inflection point pays to carry stale diffs, stack traces, and file dumps forward. Here, turn 8 crossed &lt;code&gt;80,000&lt;/code&gt; at &lt;code&gt;85,700&lt;/code&gt; tokens, dragging a 6-turn tail up to &lt;code&gt;130,700&lt;/code&gt; tokens. Compacting after turn 8 down to &lt;code&gt;--post-compact-target 20000&lt;/code&gt; trims &lt;code&gt;556,800&lt;/code&gt; context tokens (&lt;code&gt;49.3%&lt;/code&gt; of total context, &lt;code&gt;~$0.2801&lt;/code&gt; estimated savings after a conservative &lt;code&gt;0.85&lt;/code&gt; compaction-overhead factor). Lowering &lt;code&gt;--compact-threshold 60000&lt;/code&gt; catches turn 5 right before the cache bust (&lt;code&gt;64.6%&lt;/code&gt; / &lt;code&gt;~$0.4947&lt;/code&gt; saveable).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Identical tool retry loops (&lt;code&gt;&amp;gt;=3&lt;/code&gt; consecutive identical tool signatures):&lt;/strong&gt;
Starting on turn 8, the agent ran &lt;code&gt;Bash&lt;/code&gt; four times in a row. Tool attribution in &lt;code&gt;--json&lt;/code&gt; puts &lt;code&gt;Bash&lt;/code&gt; at &lt;code&gt;583,600&lt;/code&gt; attributed tokens across 6 calls — more than &lt;code&gt;Read&lt;/code&gt; (&lt;code&gt;273,150&lt;/code&gt;) and &lt;code&gt;Edit&lt;/code&gt; (&lt;code&gt;249,960&lt;/code&gt;) combined.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Copy-Paste Quickstart (Zero Dependencies, PEP 668 Safe)
&lt;/h3&gt;

&lt;p&gt;The tool runs straight from the MIT GitHub repo today using Python 3.10+ stdlib only (zero &lt;code&gt;pip&lt;/code&gt; dependencies, zero network calls):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Linux / macOS — Zero-install direct run&lt;/span&gt;
git clone https://github.com/BlackBadHope/agent-token-forensics
&lt;span class="nb"&gt;cd&lt;/span&gt; agent-token-forensics
python3 agent_token_forensics_cli.py analyze ./transcripts --markdown
python3 agent_token_forensics_cli.py analyze ./session.jsonl --compact-threshold 60000 --json

&lt;span class="c"&gt;# Or isolated CLI install (PEP 668 safe on Ubuntu/Debian 24.04)&lt;/span&gt;
uvx --from . agent-token-forensics analyze ./transcripts --markdown
pipx install . &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; agent-token-forensics analyze ./transcripts --markdown
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Windows (PowerShell 7) — Zero-install direct run&lt;/span&gt;
&lt;span class="n"&gt;git&lt;/span&gt; &lt;span class="n"&gt;clone&lt;/span&gt; &lt;span class="n"&gt;https&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;//&lt;/span&gt;&lt;span class="n"&gt;github&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;com&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nc"&gt;BlackBadHope&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;agent&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;forensics&lt;/span&gt;
&lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Location&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="n"&gt;agent&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;forensics&lt;/span&gt;
&lt;span class="n"&gt;python&lt;/span&gt; &lt;span class="n"&gt;agent_token_forensics_cli&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;py&lt;/span&gt; &lt;span class="n"&gt;analyze&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;\&lt;span class="n"&gt;transcripts&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;markdown&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Honest Scope and Limits
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Strictly read-only, zero telemetry:&lt;/strong&gt; Scans files read-only, skips &lt;code&gt;.git&lt;/code&gt;, &lt;code&gt;node_modules&lt;/code&gt;, and &lt;code&gt;.venv&lt;/code&gt;, tolerates malformed/non-UTF-8 lines (&lt;code&gt;errors="replace"&lt;/code&gt;), disambiguates colliding &lt;code&gt;transcript.jsonl&lt;/code&gt; basenames across subdirectories so adjacent sessions never trigger false cache busts, and creates zero hidden state directories.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Indicative pricing, not invoice accounting:&lt;/strong&gt; Built-in per-1M rates (&lt;code&gt;opus&lt;/code&gt;, &lt;code&gt;sonnet&lt;/code&gt;, &lt;code&gt;haiku&lt;/code&gt;, &lt;code&gt;gpt-4o&lt;/code&gt;, &lt;code&gt;codex&lt;/code&gt;, &lt;code&gt;o3&lt;/code&gt;, &lt;code&gt;gemini&lt;/code&gt;, plus &lt;code&gt;default&lt;/code&gt;) exist for relative burn diagnosis, not billing reconciliation. Pass custom pricing via the Python API if you run negotiated enterprise tiers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Diagnostics, not runtime enforcement:&lt;/strong&gt; &lt;code&gt;agent-token-forensics&lt;/code&gt; tells you where quota burned after the fact (27 unit &amp;amp; E2E tests, MIT licensed). It does not kill runaway loops mid-flight.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;Want automated guardrails beyond post-session audits?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;GitHub (MIT, free):&lt;/strong&gt; &lt;a href="https://github.com/BlackBadHope/agent-token-forensics" rel="noopener noreferrer"&gt;BlackBadHope/agent-token-forensics&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;1-Click MCP Governance Skills &amp;amp; Paid Bundles (Agensi, USDC live):&lt;/strong&gt; &lt;a href="https://www.agensi.io/r/BLACKHOPE" rel="noopener noreferrer"&gt;agensi.io/r/BLACKHOPE&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cloud Control Plane (multi-host Killswitch API, SHA-256 Cloud Witness notarization, live &lt;code&gt;$19&lt;/code&gt;/&lt;code&gt;$49&lt;/code&gt; plans):&lt;/strong&gt; &lt;a href="https://integrity.subnet.zp.ua?utm_source=devto&amp;amp;utm_medium=article&amp;amp;utm_campaign=token_forensics" rel="noopener noreferrer"&gt;integrity.subnet.zp.ua&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>ai</category>
      <category>devops</category>
      <category>python</category>
      <category>productivity</category>
    </item>
  </channel>
</rss>
