Claude Code's local logs repeat each response, so naive token counts run high

While building a usage tracker I found something worth sharing for anyone who measures their own AI usage.

Claude Code’s local logs repeat each response. Claude Code keeps a log of every session in ~/.claude/projects (JSONL files). As it streams an answer it writes the same API response several times, one line per content block, and each line carries the same usage block. If you simply add up the usage fields, you over-count. On a made-up sample log the plain sum is 2.97x the real figure. A continued session can also start a new file that copies earlier lines, so the same response can appear in more than one file.

The fix: keep one record per response, keyed on message.id plus the request ID across all files, and take the largest output_tokens value for each.

Two related things that surprise people on API bills:

  • Thinking tokens are billed as output, even when you don’t see them.
  • A prompt-cache write costs more than plain input (1.25x for the 5-minute cache, 2x for the 1-hour cache), so caching only pays off when the prefix is read back.

Free tools if you want to check your own numbers:

Disclosure: I also sell a desktop app built on the same counting (NeverPayTwice, $9 one time). It keeps a receipt for every API call you route through it and flags repeat charges. The free tools above are complete on their own.

Happy to answer questions, or to hear where the counting is wrong.