How I Track My AI Coding Costs Per Project (and Bill Them to Clients)
说明与用途
devspend CLI 自动聚合 Claude Code 日志中的 token 使用量,应用正确的定价公式,去重后输出按项目的费用,帮助向客户开具透明账单。
问题
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.
结果
The devspend tool produces per‑project cost summaries, revealing hidden AI spend and enabling transparent billing to clients.
工作流
- 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)
证据
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):
- 记录日期: 2026-10-03T11:41:22.124Z