How I Track My AI Coding Costs Per Project (and Bill Them to Clients)
Description and use
The devspend CLI aggregates token usage from Claude Code logs, applies a correct pricing formula, deduplicates duplicates, and outputs per‑project costs, enabling transparent client billing.
Problem
Determining which client or project consumed AI spend when using Claude Code, Cursor, or raw API keys, so that costs can be billed to clients.
Outcome
The devspend tool produces per‑project cost summaries, revealing hidden AI spend and enabling transparent billing to clients.
Workflow
- 1. Locate Claude Code session logs at ~/.claude/projects/
<project-slug>/<session-id>.jsonl - 2. Parse each line for assistant messages, extracting cwd, model, and usage fields
- 3. Group events by cwd to associate tokens with projects
- 4. Deduplicate duplicate assistant messages using message.id or a fingerprint of timestamp+sessionId+token counts
- 5. Apply pricing formula: billable = input×1 + cache_write×1.25 + cache_read×0.1 + output×output_price
- 6. Use a configurable price table (~/.devspend/prices.json) to keep up with price drift
- 7. Run the devspend CLI (npx devspend projects) to generate a cost report per project
- 8. Export or use the report for invoicing (rate‑card or cost‑pool allocation)
Evidence
Claude Code writes every session to local JSONL files: ~/.claude/projects/<project-slug>/<session-id>.jsonl
Group by cwd, sum tokens, multiply by prices — done? Almost. Three gotchas first.
billable = input×1 + cache_write×1.25 + cache_read×0.1 (× input price) + output×output_price
I packed the above into a small zero‑dependency CLI, devspend (MIT, open source — I'm the author, to be clear):
- Record date: 2026-10-03T11:41:22.124Z