How I Track My AI Coding Costs Per Project (and Bill Them to Clients)
Описание и применение
Инструмент devspend автоматически агрегирует токен‑затраты из логов Claude Code, применяет корректную ценовую формулу, удаляет дубликаты и выводит расходы по проектам, позволяя выставлять клиентам прозрачные счета.
Задача
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