mirror of
https://github.com/Jeuners/ECC.git
synced 2026-09-09 15:02:30 +02:00
Claude Code writes one transcript JSONL line per content block, so a single API response (one message.id) spans multiple assistant lines that each repeat the same message.usage. sumUsageFromTranscript summed every line, inflating token totals and estimated_cost_usd roughly 2.5-3x. Verified on a real session: 704 assistant lines but only 286 unique message.ids (2.46 lines/response on average); line-summing reported $866.52 while the deduped total is $332.62. Usage payloads are identical across lines of the same id (0/286 varied), so counting once per id is equivalent to taking the last line per id. Fix: collect usage into a Map keyed by message.id (last line wins) and sum unique entries. Lines without a message.id (older transcript shapes) keep the previous per-line behavior via a synthetic key, so existing tests and old transcripts are unaffected. Adds a regression test: a response split into 3 content-block lines with the same message.id is counted exactly once. Note: rows already written to ~/.claude/metrics/costs.jsonl by the old code carry inflated token counts and estimates (except rows whose cost came from the harness-cost cache, where cost is authoritative but token counts are still inflated). Downstream consumers may want to annotate history; this change intentionally does not rewrite the raw log. Co-authored-by: Claude Fable 5 <noreply@anthropic.com> |
||
|---|---|---|
| .. | ||
| ci | ||
| commands | ||
| docs | ||
| hooks | ||
| integration | ||
| lib | ||
| scripts | ||
| __init__.py | ||
| codex-config.test.js | ||
| conftest.py | ||
| opencode-config.test.js | ||
| opencode-plugin-hooks.test.js | ||
| opencode-tools.test.js | ||
| plugin-manifest.test.js | ||
| run-all.js | ||
| test_astraflow_provider.py | ||
| test_atlas_provider.py | ||
| test_builder.py | ||
| test_claude_provider.py | ||
| test_executor.py | ||
| test_invariant_runner.py | ||
| test_provider_tools.py | ||
| test_resolver.py | ||
| test_selector.py | ||
| test_templates.py | ||
| test_types.py | ||