How to Get an Accurate Claude Code Token Count
Краткое содержание
Pixel shares why Claude Code usage logs can overstate token counts: responses may be duplicated across content blocks and resumed sessions, while early streaming entries can be incomplete. The announcement explains how to count each response once and points to a free browser-based counter and script; a sample log shows the difference between 5.3M reported tokens and 1.8M actual tokens.
DEV Community
Why your Claude Code token count is wrong (and how to get the real number)
Originally published at neverpaytwice.pages.dev. Short answer: Claude Code writes each AI response to its log several times, and every copy carries the full token count. If you add up every line, you count most responses two or more times. On the made-up sample log in our free scripts repo (30 responses, each logged 2 to 4 times, as Claude Code does), a plain sum says 5.3M tokens. The real figure is 1.8M. To get the right number, count each response once. Our free counter does that in your browser, or use the short script below. Where Claude Code keeps its usage logs Claude Code keeps a local log of every session. They live in a hidden folder in your home directory: System Folder Mac and Linux ~/.claude/projects Windows %USERPROFILE%.claude\projects Inside there is one folder per project, holding .jsonl files (mostly one per session). Each line is one JSON event. Lines for AI responses carry a usage block with four token counts: input_tokens: new input sent to the model output_tokens: what the model wrote back cache_creation_input_tokens: input written to the prompt cache cache_read_input_tokens: input read back from the cache (much cheaper than new input) Why adding up the logs gives the wrong answer It looks simple: add the usage numbers on every line. But three things in how the logs are written make that total too high. One response, several lines. A single response is written once per content block: thinking, text, and each tool call. Every one of those lines repeats the usage of the whole response. A response with a thinking block, some text and two tool calls can appear four times. Resumed sessions copy their history. When you resume a session, earlier lines can be copied into a new log file, so the same response shows up in more than one file. Early lines are incomplete. While a response streams, the first lines can carry a partial output count. Only the last one is final, so even picking "any one line" can under-count output. None of this means you were billed twice. Each response is one request to the API. The over-count only appears when you total the log lines yourself, for example to estimate spending or build a dashboard. How to count correctly Every response line carries a message.id and a requestId. Together they identify one response. So: Keep one record per (message.id, requestId) pair, across all files. For its output tokens, use the largest value seen for that pair (the final streaming count). Skip lines that are not real responses: "model": "" and API error messages. A short script to do it yourself This Python 3 script follows the rules above. It only reads your logs and prints three numbers. (Download it, or get it with the other scripts on GitHub.) import json, pathlib, sys
root = pathlib.Path(sys.argv[1]) if len(sys.argv) > 1 else pathlib.Path.home() / ".claude" / "projects"
best, plain = {}, 0
for f in sorted(root.rglob("*.jsonl")):
n = 0
for line in f.open(encoding="utf-8", errors="replace"):
try:
o = json.loads(line)
except ValueError:
continue
if not isinstance(o, dict):
continue
n += 1
m = o.get("message")
if o.get("type") != "assistant" or not isinstance(m, dict) or not m.get("usage"):
continue
if m.get("model") == "" or o.get("isApiErrorMessage"):
continue
u = m["usage"]
t = [u.get("input_tokens") or 0, u.get("output_tokens") or 0,
u.get("cache_creation_input_tokens") or 0, u.get("cache_read_input_tokens") or 0]
plain += sum(t)
if m.get("id") or o.get("requestId"):
key = (m.get("id") or "", o.get("requestId") or "")
else:
key = (str(f), n)
if key not in best:
best[key] = t
else:
best[key][1] = max(best[key][1], t[1]) # keep the final (largest) output count
real = sum(sum(t) for t in best.values())
print(f"{len(best):,} responses...

Powered by Readybot.io