← All recipes
  • builds
  • cost

We had Claude Code build a cost tracker. It took 2 prompts and $0.30.

Claude Code (Sonnet 5.5) built cc-cost, a small CLI that reads its own session logs and prints API spend per day and per session. It took 2 prompts, 126 seconds and $0.30. Its tests passed the first time, but the real logs turned up an undercount the tests missed. After one fix it matched Claude Code's own cost totals on 76 of 86 real sessions.

This is the first entry in our builds series. We give Claude Code a real, small tool to build, in public, and publish everything: the exact prompts, the cost, the time and what broke. The code is at github.com/codetwigdev/builds.

Build #1 is a tool we wanted for ourselves: how much did my Claude Code sessions cost? Claude Code writes every session to JSONL files under ~/.claude/projects/, with token counts on every API response. Multiply those by Anthropic’s prices and you get spend per session and per day.

What it does

cc-cost is a zero-dependency Node 22 CLI.

  • cc-cost prints a per-day table of requests, tokens and dollars.
  • cc-cost sessions prints the same per session, including subagent spend.
  • --check puts Claude Code’s own running cost for each session (its cost-state records) next to ours, so you can see whether they agree.
  • There are also --since, --until, --tz, --dir and --json.

Prices come from Anthropic’s pricing page, checked Nov 1, 2026, and live in one table.

  • 5-minute and 1-hour cache writes are priced separately.
  • Haiku 5.5 switches to its higher prices when a request’s prompt goes over 100,000 tokens.
  • US-only inference costs 1.1x.
  • Opus 5.5 fast mode uses its own prices.
  • Models that aren’t in the table, such as Kimi running through Claude Code, show up as “unpriced” with their token counts. They are never guessed.

Before writing the prompt, we checked one real log line by hand. Claude Code had recorded $0.0383372 for 8 input, 1,112 output, 71,762 cache-read and 8,010 cache-write tokens on Sonnet 5.5. At the published $2 / $10 / $0.10 / $2.50 per million, that’s exactly $0.0383372. That also showed that thinking tokens are already counted in output_tokens.

The setup

  • Claude Code 2.1.312 on Sonnet 5.5, run headless with claude -p, on our box.
  • Permissions: accept edits, plus Bash, Read, Write, Edit, Glob and Grep.
  • A budget of $25. We used $0.30.
  • Prompt 1 was one long spec. It covered the log format, including the traps we knew about, the price table, the CLI, the tests it had to write on hand-made fixture logs, and a rule not to read our real logs.
  • Prompt 2 was a bug report after we ran it on real logs.
  • Both prompts are in BUILD.md, word for word.

What happened

Prompt 1 (build) Prompt 2 (fix) Total
API cost $0.2570 $0.0383 $0.2953
Claude Code time 109.3 s 16.7 s 126.0 s
Turns 7 2 9
Tool calls 6 (all Bash) 1 (Bash) 7
Output tokens 17,335 2,249 19,584
Tests passing after 11 of 11 12 of 12

Costs are Claude Code’s own total_cost_usd. Prompt 2 resumed the session, so its cost is the cumulative total minus prompt 1’s. Run on the logs of this build session, cc-cost also gives $0.2953.

Prompt 1 came back with the CLI, 11 tests and a README. The finished tool is 377 lines of source. Claude reported that 2 of its own tests failed on the first try because its fixture files reused message ids across sessions. It fixed the fixtures and the tests passed.

What broke

The tests passed, and the tool was still wrong on real data. We ran cc-cost sessions --check on the 86 real Claude Code sessions on our box from our recipe runs. 76 matched Claude Code’s own totals to within $0.0001. Four came out low, the worst at $0.2590 against $0.3035.

The cause: Claude Code writes one API response as several log lines with the same message id. While a response streams, an earlier line can carry partial usage. In one transcript, the same message showed 3 output tokens on one line and 4,020 on a later one. cc-cost counted each message once, as we asked, but it kept the first line. The tests didn’t catch it because the same model wrote the fixtures, and its duplicate-line fixture repeated identical numbers on every line.

Prompt 2 described the bug. Claude switched to the last line’s usage, took the highest output count seen, and added a test. It took 16.7 seconds and cost $0.0383. The $0.2590 session now prices at $0.2992.

What’s still off: those same 4 sessions are low by $0.0043 to $0.0062 each, $0.0212 in all. Their logged output tokens are 433 to 621 short of Claude Code’s own count, so the missing tokens aren’t in the logs at all. All 4 ran an Explore subagent on Claude Code 2.1.302. The 2.1.312 sessions with subagents match exactly.

And one thing broke on our side: our launch script passed the first prompt by a relative path, so the first launch sent Claude Code an empty prompt. It exited before making any API call, so it cost $0.

Checked against our recipe spend

We compared cc-cost’s per-session numbers with the per-run costs our test runner recorded for recipes we’ve published. The runner’s numbers come from Claude Code’s total_cost_usd.

Recipe runs Runner records cc-cost
tests-first $0.5132 $0.5132
paste-the-error (2 sets of 6) $0.5553 $0.5553
think-hard $0.4426 $0.4426
clear-on-a-bigger-repo $0.8883 $0.8883
mixed-models-vs-sonnet (smoke + 9 runs) $5.5414 $5.5414
plan-mode-first $1.3091 $1.2941

The plan-mode-first gap is the Explore-subagent gap above.

Across all 86 sessions:

  • cc-cost totals $13.4880 on Anthropic models.
  • Claude Code’s own totals add up to $15.6118.
  • The $2.1238 difference is $0.0212 from the gap above, plus $2.1026 that Claude Code put on the 6 Kimi sessions from claude-vs-kimi-k3. Kimi is billed by Moonshot, not Anthropic, so cc-cost leaves it unpriced.

The limits

  • It’s an estimate from local logs, not your bill. It only sees sessions whose logs are still on disk. It doesn’t count web search fees ($10 per 1,000 searches), and none of our sessions used web search.
  • The price table is hard-coded. It was checked on Nov 1, 2026. If prices change, src/prices.js needs an update.
  • The comparison is against Claude Code’s own estimate, not an Anthropic invoice.
  • It’s one build, one model, one run. We didn’t try other models or repeat it, so the cost and time are one data point.

The recipe

  1. Spell out the data format and its traps in the prompt. We told Claude about duplicate lines, subagent files and cache-write types up front. Our 2 prompts cost $0.30 in total.
  2. Don’t trust tests the same model wrote. All 11 passed, and the tool still undercounted 4 real sessions. The fixtures encoded the same assumption as the code.
  3. Check it against a second source. Claude Code’s cost-state records gave us a number to compare every session with, and that’s how the bug surfaced.
  4. Feed back the real failure, not a vague complaint. Prompt 2 named the session, both numbers and the line pattern. The fix took one Bash call and $0.0383.
  5. Use it: git clone https://github.com/codetwigdev/builds && node builds/claude-code-cost-tracker/bin/cc-cost.js sessions --check.