Coding with AI

I counted 30 days of my Claude Code tokens and found that 98% of them were cache reads.

Local Claude Code logs show 4.9B tokens over 30 days, 98% cache reads; dedupe by reply IDs, watch 30-day cleanup, and avoid double-counting usage.

Key Takeaways

  • Claude Code stores usage in JSONL session files under `~/.claude/projects/`, with token counters for new input, cache writes, cache reads, and output.
  • In 30 days, 98.1% of 4.9 billion tokens were cache reads because each request resends the full conversation; output was only 0.37% of tokens but 19% of API value.
  • Counting raw usage lines double-counts replies across multiple lines or files; deduplicate by message/request ID and keep the maximum values.
  • Claude Code deletes old session logs after `cleanupPeriodDays`, which defaults to 30 days, so important history must be saved manually.
  • Pro/Max rate-limit percentages are server-side and available via status-line JSON or `.claude.json`, while `/clear` between unrelated tasks reduces costly context reuse.

A developer who spent months on a flat-fee Claude subscription built a small menu bar app to count tokens on his own machine and found 4.9 billion of them in 30 days. The figure came not from a dashboard but from plain JSON files that Claude Code writes to the home folder after every reply. The bigger surprise was the mix: 98.1% of those tokens were cache reads, while the text Claude actually wrote back accounted for 0.37%.

The finding matters to anyone on a Claude Pro or Max plan who wants to understand what their usage really looks like, how to measure it without double counting, and where the 5-hour and weekly limits are actually stored. It also comes with a deadline: Claude Code deletes session files older than `cleanupPeriodDays`, which defaults to 30 days, so history you want to keep has to be preserved before it disappears.

Where the numbers live on disk

Every Claude Code session is a JSONL file, one JSON object per line, appended as the session runs. The files sit under `~/.claude/projects/`, in a folder per project named after the project's path, with subagent sessions in a `subagents/` directory. Anyone running two accounts through separate `CLAUDE_CONFIG_DIR` folders has two sets of `projects/` directories and needs to count both.

Each line is one event: a prompt, a tool result, a summary, or a model reply. Reply lines carry a usage object from the API with four counters: `input_tokens` for new input the cache did not cover, `cache_creation_input_tokens` for input written to the cache on that request, `cache_read_input_tokens` for input served from the cache, and `output_tokens` for what the model wrote, thinking included.

The reason the cache dominates is structural. Each request resends the whole conversation, so a one-line question in a session that has been open all day still draws usage for the entire conversation, as Claude Code's own cost documentation states. Caching is what makes that resend cheap, and the usage object records exactly how cheap.

The double-counting trap

The obvious script reads every line with a usage field and adds the numbers up. On the developer's logs, that overcounts output tokens 2.47 times and cache reads 2.13 times, making the total look more than twice as alarming as it is.

One reply is often written as several lines. A reply containing thinking, text and a tool call can land as one line per content block, and each line carries the same usage. The 30-day dataset had 51,429 lines with usage but only 24,434 distinct replies. A reply can also appear in two files when a session is resumed or forked.

The fix is to key each reply by its message ID and request ID and keep the largest value per counter across copies. A short Python script walks the JSONL files, skips any line that does not contain the string `"usage"` before parsing, filters for assistant messages with usage, and merges duplicates by taking the maximum. The string check matters on large folders: most lines are prompts and tool output, and skipping them unparsed keeps a run over a 1.6 GB folder to about 9 seconds. Taking the largest rather than the last value is the safe choice because copies of the same reply should agree, and when they do not, the largest is the complete count.

What a month of logs looks like

The count covered both accounts from 2026–09–26 to 2026–10–26, with each reply priced at Anthropic's API list prices for its model. Because the developer is on a subscription, the dollar figure is not what he paid; it is what the same tokens would cost on the API, which makes months and models comparable.

The totals: 4.90 billion tokens, of which cache reads were 4.80 billion (98.1%), cache writes 73 million (1.5%), output 18 million (0.37%), and uncached input 49 thousand. The API value came to $1,782, split as $954 for cache reads (54%), $490 for cache writes (27%) and $338 for output (19%). By model, Opus 5.5 accounted for $1,622, Sonnet 5.5 for $135 and Sonnet 5 for $25.

Three patterns stand out. Caching is the whole story: cache reads are billed at a tenth of the input price or less, and priced as plain input the 4.8 billion cache reads would have cost $18,083 on their own, ten times the whole month. The expensive event is not a long answer but anything that breaks the cache, such as changing the system prompt mid-session or returning after the cache has expired. Output is tiny in tokens and large in dollars, at 0.37% of tokens but 19% of value, because output costs five times input per token. Uncached input is effectively zero at 49 thousand tokens in a month.

The practical takeaway points at context size rather than shorter answers. Every turn re-reads everything before it, which makes `/clear` between unrelated tasks worth more than any clever prompt.

Where the plan limits actually live

Token counts do not tell you how close you are to the 5-hour or weekly limit on a Pro or Max plan. That figure belongs to the server, and Claude Code exposes it in two places.

The first is the status line input. Claude Code runs the status line command with a JSON object on stdin that includes `rate_limits.five_hour` and `rate_limits.seven_day`, each with `used_percentage` and `resets_at` in Unix seconds, refreshed with every reply. A small script can save those figures to a file for later reading. The second is a cache in `.claude.json`, where Claude Code saves a reading of each window when it checks the account. It is convenient because it can be read at any time, but it updates only now and then; on the developer's Mac it was 8 and 21 hours old while Claude Code was running.

Lessons from building a reader

The app, OpusBar, is a macOS menu bar utility that reads these files and displays them, with a pixel cat that changes pose for each Claude Code or Codex session. A Usage tab shows tokens and API value over 24 hours, 7, 30 and 90 days, plus each account's 5-hour and weekly limits, with warnings at 80% and 95%. It is free and open source under Apache-2.0, and the developer says he is the only author and is not affiliated with Anthropic or OpenAI.

It pulls data from three sources: the JSONL logs, read in place with a per-file cursor that reads only newly appended bytes and macOS file events to decide when to look; the status line input, saved to a file by wrapping the existing status line command so the line still prints exactly what it printed before; and Claude Code hooks, where a tiny binary runs on 12 hook events and sends one line to a local socket. One rule sits behind all three: no network calls and no sign-in tokens. Some tools read the OAuth token from the Keychain and call the usage endpoint, which works but means a menu bar app holding credentials.

Several engineering lessons emerged. Counting replies rather than lines had to be right before a single number reached the screen, and the same logic had to hold for Codex, which logs a running total per session where two copies of the same total must count once. A cached limit figure can be a day old: the first version read the Claude desktop app's own usage file, which did not exist on the developer's Mac, and the second read the `.claude.json` cache, whose cards looked right until the timestamp showed 21 hours. A warning at 80% is useless if it describes yesterday, and moving to the status line input fixed it at the cost of freshness tied to the last reply on that machine; usage from a phone shows up after the next reply on the desktop.

A price table goes stale. OpusBar prices tokens from a table shipped inside the app with the date it was last checked, and a model the table does not know is counted as "unpriced," never guessed. While running the numbers for the article, 420 replies came back unpriced: Haiku 5.5, released after the table's date. Hooks arrive out of order: synchronous hooks keep order but add 10 to 20 ms to every tool call, so OpusBar registers async hooks that never slow Claude down but can arrive out of order, and each event carries the time its hook started so the app can drop anything older than the last event it applied for that session, with session end always removing the session even if it arrives late.

A hook must never hurt the agent. If the app is closed, the hook still runs on every tool call, so it connects with a 200 ms deadline, never prints, and always exits 0, making quitting OpusBar invisible to Claude Code. And a missing event is a state too: no hook fires when a user answers a permission prompt, so "needs you" clears on the next tool call, prompt or stop event, and after a denied prompt a session can show "needs you" until Claude does something else. Keeping the session logic as a pure function from state, event and time to state turned each odd case into a fast unit test.

What readers can do next

No special app is needed to see your own numbers. The counting script runs on any Mac or Linux machine with Claude Code. Set `cleanupPeriodDays` in `~/.claude/settings.json` first if you want more than a month of history, then run the count and look at the mix. The developer says he would genuinely like to know if anyone's cache-read share is below 90%.

The menu bar version requires macOS 14 or later and installs via a shell script from the project's GitHub repository at kushalBanda/OpusBar, where the source is also published; reading the short install script first is advised. The larger point for subscribers is that the token bill, whether paid in dollars or plan limits, is driven by context and cache behavior far more than by the length of any single answer.

Comments (0)

  1. No comments yet. Be the first to share what worked for you.

Leave a comment

Comments are reviewed before they appear. Your email address is not published.