How I correlate two async data streams to compute per-tool-call token costs in Claude Code (FOSS)

Posted by shade175@reddit | Python | View on Reddit | 2 comments

Built an open-source CLI tool that solves a specific problem: Claude Code doesn't tell you

which individual tool call consumed your tokens. CAT fills that gap.

Tech stack:

- FastAPI + Uvicorn async collector (receives hook events from Claude Code)

- SQLite + aiosqlite with schema migrations (WAL mode for concurrent reads)

- Delta engine that correlates hook events with statusline snapshots via session ID + timestamps

- Welford's online algorithm for O(1) rolling baseline statistics per task type

- Z-score anomaly detection over a 20-sample window

- Optional Haiku LLM classifier for root-cause analysis

- Rich TUI dashboard + Typer CLI

Why not just parse the Claude Code logs?

Claude Code hooks don't include token counts — only the statusline hook provides them.

The delta engine matches the two data streams by timestamp to compute per-call costs.

Currently at v0.3.1, CI passing on Ubuntu/macOS/Windows across Python 3.11–3.13.

Active development, good-first-issues available.

GitHub: https://github.com/roeimichael/ContextAnalyzerTerminal