<?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: Patric</title>
    <description>The latest articles on DEV Community by Patric (@mypatric69).</description>
    <link>https://dev.to/mypatric69</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3894207%2Ff45c18bf-9632-49c9-abde-27f237fbdacc.jpeg</url>
      <title>DEV Community: Patric</title>
      <link>https://dev.to/mypatric69</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mypatric69"/>
    <language>en</language>
    <item>
      <title>I built a token cost tracker for Claude Code - and it changed the way I think about AI development</title>
      <dc:creator>Patric</dc:creator>
      <pubDate>Thu, 23 Apr 2026 16:20:47 +0000</pubDate>
      <link>https://dev.to/mypatric69/i-built-a-token-cost-tracker-for-claude-code-and-it-changed-the-way-i-think-about-ai-development-111i</link>
      <guid>https://dev.to/mypatric69/i-built-a-token-cost-tracker-for-claude-code-and-it-changed-the-way-i-think-about-ai-development-111i</guid>
      <description>&lt;h2&gt;
  
  
  It started with a number I couldn’t explain
&lt;/h2&gt;

&lt;p&gt;A few months ago, I looked at my Anthropic billing dashboard and saw a number that didn’t add up. I knew roughly how much I’d worked with Claude Code. The bill said otherwise.&lt;br&gt;
I couldn’t tell which sessions were expensive. I couldn’t tell why. I only had a total figure at the end of the month—and no way to link it to specific work.&lt;br&gt;
That bothered me. Not because of the money—but because of the lack of visibility. And as someone who’s worked in IT for 30 years, I know: What you can’t measure, you can’t improve.&lt;br&gt;
So I built TRACE.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I thought I was building—and what I actually built
&lt;/h2&gt;

&lt;p&gt;The original idea was simple: log token consumption per session, calculate costs, store in SQLite. An afternoon project.&lt;br&gt;
What I actually built—over weeks of iteration with Claude Code itself as a development partner—is a local MCP server that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Tracks token costs per project and session in real time&lt;/li&gt;
&lt;li&gt;Detects session health (green / yellow / red) based on configurable thresholds&lt;/li&gt;
&lt;li&gt;Sends native macOS notifications when expensive limits are exceeded – no dashboard required&lt;/li&gt;
&lt;li&gt;Automatically keeps &lt;code&gt;AI_CONTEXT.md&lt;/code&gt; up to date via Git hooks&lt;/li&gt;
&lt;li&gt;Generates enriched handoff prompts when starting a new thread&lt;/li&gt;
&lt;li&gt;Displays everything in a web dashboard with a 7-day history, provider badges, and live multi-session tracking&lt;/li&gt;
&lt;li&gt;Runs directly in the VS Code Simple Browser Panel – no external browser required&lt;/li&gt;
&lt;li&gt;Optionally starts automatically at Mac login via LaunchAgent
MIT license – free to use, forkable, no restrictions. With solid test coverage ensuring it works in real-world scenarios.
The gap between an “afternoon project” and a production-ready open-source tool says something about how AI-powered development really works when you seriously commit to it.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What Nobody Talks About: Context Rot
&lt;/h2&gt;

&lt;p&gt;Here’s what I’ve learned—and what I’ve never heard in any podcast, video, or presentation:&lt;br&gt;
Token costs do not scale linearly with the work. They scale with the session length.&lt;br&gt;
Every turn in a Claude Code session appends to the conversation history. By turn 50, every new message carries the weight of the previous 49 turns as input tokens. By turn 100, you’re burning tens of thousands of tokens per message—just to maintain context, even if the actual task is small.&lt;br&gt;
Anthropic calls this “context rot” in its documentation: As the number of tokens grows, accuracy and recall degrade. The model doesn’t lose tokens—it loses attention. It has to spread its focus across an ever-growing history.&lt;br&gt;
&lt;strong&gt;The practical consequence:&lt;/strong&gt; A 300-turn session doesn’t just cost more than 30 sessions of 10 turns each. It costs significantly more—and the quality of the later turns is measurably worse.&lt;/p&gt;

&lt;h2&gt;
  
  
  Push Instead of Pull: Why Built-in Commands Aren’t Enough
&lt;/h2&gt;

&lt;p&gt;Claude Code has built-in visibility tools. &lt;code&gt;/cost&lt;/code&gt; shows the current session usage. &lt;code&gt;/context&lt;/code&gt; visualizes context window utilization. &lt;code&gt;/stats&lt;/code&gt; provides usage statistics.&lt;br&gt;
These are pull mechanisms. You have to remember to use them. You have to be curious enough to check. You have to already suspect that something is wrong.&lt;br&gt;
TRACE is a push mechanism. It comes to you.&lt;br&gt;
When a session exceeds 80,000 tokens, a notification—a subtle &lt;code&gt;Tink&lt;/code&gt; sound and a macOS alert—appears before it gets really expensive. At 150,000 tokens, a more distinct &lt;code&gt;Funk&lt;/code&gt; signals that it’s time for a new thread. You don’t have to remember to check. TRACE checks for you.&lt;br&gt;
The dashboard also runs directly in the VS Code Simple Browser—for those who want to keep everything in one interface.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;&lt;code&gt;/cost&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;/context&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;TRACE&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Current session usage&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Visual context window&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Cache tokens separately&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Historical sessions&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Cost per project&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Monthly budget &amp;amp; alerts&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Session health indicator&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Push notifications&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Handoff prompt&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;AI_CONTEXT.md auto-update&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;VS Code Simple Browser&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Autostart on Login&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  There is no universal token limit
&lt;/h3&gt;

&lt;p&gt;A question that quickly arises during use: at what point is a session “too long”? The answer is: it depends.&lt;br&gt;
80,000 tokens as a warning threshold is a good starting point—but a developer working on a small script will reach that after just a few turns, while someone refactoring a complex backend is still far from it.&lt;br&gt;
TRACE therefore makes the thresholds configurable—directly in the dashboard or in the configuration file. Three recommendations for guidance:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Usage Type&lt;/th&gt;
&lt;th&gt;Warning&lt;/th&gt;
&lt;th&gt;Critical&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Economical – cost-conscious&lt;/td&gt;
&lt;td&gt;50,000&lt;/td&gt;
&lt;td&gt;100,000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Standard – recommended&lt;/td&gt;
&lt;td&gt;80,000&lt;/td&gt;
&lt;td&gt;150,000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Intensive – large projects&lt;/td&gt;
&lt;td&gt;120,000&lt;/td&gt;
&lt;td&gt;200,000&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;These numbers are not set in stone. They are a starting point that should be adjusted once you understand your own usage patterns.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: TRACE tracks Claude Code sessions—i.e., work done in the terminal. For claude.ai web/desktop chats, a separate Anthropic Usage API is required, which is only available for Team and Enterprise accounts.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  What a real day of usage looks like
&lt;/h2&gt;

&lt;p&gt;I recently installed TRACE for a colleague. After one day of use, the logs told a clear story.&lt;br&gt;
A single session showed over 37 million cache read tokens and nearly $20 in costs. The session had accumulated hundreds of turns—including many invisible tool-use turns that Claude Code generates internally for file reads, Bash commands, and code analysis. Each of these counts as a turn in the transcript, even if the user typed only 30 prompts.&lt;br&gt;
The insight isn’t that something went wrong. The insight is: Without visibility, you don’t even ask the question in the first place.&lt;br&gt;
TRACE makes the invisible visible. That’s all it does—but it turns out to be a lot.&lt;/p&gt;

&lt;h2&gt;
  
  
  The /resume Trap
&lt;/h2&gt;

&lt;p&gt;One more thing worth knowing: Claude Code’s &lt;code&gt;/resume&lt;/code&gt; command is more expensive than it looks.&lt;br&gt;
When resuming a session, Claude Code sends the entire conversation history as input tokens—including invisible “Thinking Block Signatures” from Extended Thinking turns. These are base64-encoded, unreadable, and cannot be truncated. But they are sent to the API with every resume and billed accordingly.&lt;br&gt;
&lt;a href="https://github.com/anthropics/claude-code/issues/42260" rel="noopener noreferrer"&gt;Anthropic’s own GitHub issues document cases where resuming a 24-hour session cost ~156,000 input tokens—before the user had even typed a single character.&lt;/a&gt;&lt;br&gt;
Anthropic’s own documentation is clear: Do not rely on session resumption. Save results as state and pass them to a fresh session.&lt;br&gt;
TRACE’s &lt;code&gt;new_session()&lt;/code&gt; tool does exactly that—it generates a compressed handoff prompt from &lt;code&gt;AI_CONTEXT.md&lt;/code&gt;, &lt;code&gt;CLAUDE.md&lt;/code&gt;, current Git changes, and the open task in the backlog. A new thread gets everything it needs in a few hundred tokens instead of hundreds of thousands.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Bugs That Taught Me the Most
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;The $0.0000 Sessions.&lt;/strong&gt; TRACE logged sessions but showed zero cost. The model string in the transcripts was &lt;code&gt;claude-sonnet-4-5-20250929&lt;/code&gt;. The config key was &lt;code&gt;claude-sonnet-4-5&lt;/code&gt;. Exact matching failed. A line of prefix matching fixed it—but finding it really required digging into the logs.&lt;br&gt;
&lt;strong&gt;The health indicator that disappeared after a refresh.&lt;/strong&gt; Session Health turned red at 150,000 tokens—and then disappeared when the browser was refreshed because the state was stored in a JavaScript variable. The fix was to move the health state to a &lt;code&gt;last_health.json&lt;/code&gt; file on the server side. Obvious in hindsight. Not obvious until a real user encountered it.&lt;br&gt;
&lt;strong&gt;The &lt;code&gt;AI_CONTEXT.md&lt;/code&gt; file that was out of date.&lt;/strong&gt; The doc synthesizer only updated on &lt;code&gt;feat:&lt;/code&gt; and &lt;code&gt;fix:&lt;/code&gt; commits. A day with &lt;code&gt;chore:&lt;/code&gt; and &lt;code&gt;docs:&lt;/code&gt; commits left the context file four days out of date. Removing the commit type filter entirely fixed it.&lt;br&gt;
Every bug taught us something about the gap between “works in theory” and “works when a real person uses it all day.”&lt;/p&gt;

&lt;h2&gt;
  
  
  Where TRACE stands today
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Solid test coverage, all tests passing&lt;/li&gt;
&lt;li&gt;Tracks input, output, cache creation, and cache read tokens separately with correct pricing&lt;/li&gt;
&lt;li&gt;Session Health: green under 80,000 tokens, yellow up to 150,000, red above that – all configurable&lt;/li&gt;
&lt;li&gt;Native notifications on macOS, Windows, and Linux&lt;/li&gt;
&lt;li&gt;Web dashboard with 7-day history, dark/light/auto theme, provider badges, live multi-session tracking&lt;/li&gt;
&lt;li&gt;Runs in the VS Code Simple Browser – no external browser required&lt;/li&gt;
&lt;li&gt;Optional autostart via macOS LaunchAgent&lt;/li&gt;
&lt;li&gt;Enhanced Handoff prompts with current phase, open tasks, files to read&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What I would tell someone just starting out
&lt;/h2&gt;

&lt;p&gt;The most useful thing I built wasn’t the dashboard. It was the discipline of treating &lt;code&gt;AI_CONTEXT.md&lt;/code&gt; as a first-class artifact. Every project gets one. Every commit potentially updates it. Every new session starts by reading it.&lt;br&gt;
An AI assistant with full context is a different tool than one that starts from scratch. TRACE exists to make the former the standard—not the exception.&lt;br&gt;
The second most useful thing: Measure before you optimize. I had strong intuitions about which sessions were expensive. The data contradicted most of them. The expensive sessions weren’t the ones with the difficult problems. They were the ones that ran for a long time.&lt;br&gt;
And the third: Don’t wait for permission to build something useful.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;TRACE is open source under the MIT license: &lt;a href="https://github.com/MyPatric69/trace" rel="noopener noreferrer"&gt;github.com/MyPatric69/trace&lt;/a&gt;&lt;/em&gt;&lt;br&gt;
&lt;em&gt;If you find it useful: a star helps. If you find a bug: an issue helps more. If you build something with it: I’d love to hear about it.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>claude</category>
      <category>monitoring</category>
      <category>showdev</category>
    </item>
  </channel>
</rss>
