# cost Token and spend telemetry for Pi. ## Install / load Loaded through the root `pi-ext` package. See [root README](../../README.md). ## Commands / tools / settings Commands: none. Tools: none. Settings: none. Hooks: - `session_start`: reads current-month session files, resets counters, publishes first snapshot when UI exists. - `agent_start`, `agent_end`, `tool_execution_end`: refresh telemetry. `agent_end` also schedules a background monthly refresh. - `model_select`, `session_compact`: clears snapshot signature, then refreshes telemetry. - `session_shutdown`: clears published status/footer segments. Events emitted: - `footer:segment` when `@bugabinga/pi-ext-footer` is loaded: `tokens`, `cost` in the `llm` zone. `tokens` uses `muted`, order `2`. `cost` uses `success`, order `3`. UI status: - `cost` status fallback when shared footer is not loaded. ## Behavior Shows cumulative session token usage, turn cost, session cost, and current-month cost. Token totals include assistant messages, nested tool usage, compactions, and branch summaries across the session. The token label uses input, output, cache-read, and cache-write counts. Cache reads use `◐`, cache writes use `W`, and `` shows the latest assistant prompt's cache-hit percentage. No token usage shows `↑0`. Uses provider-reported `usage.cost.total` when present and greater than zero. If reported cost is zero or missing, estimates cost only for built-in `zai` and `minimax` pricing rows. Unknown provider/model prices produce `$0` estimated cost. Session cost is summed from assistant messages in the active branch. Turn cost is the increase since the previous telemetry sync. Monthly cost is summed from `.jsonl` session files whose names start with the current `YYYY-MM`, scanning `dirname(getSessionDir())` and its direct child directories. This includes persisted child sessions in those directories. Startup and `agent_end` refreshes cache file subtotals by modification time and size, rereading only new or changed files. Deleted files and previous-month entries leave the cache on refresh. Overlapping refresh requests coalesce; shutdown and session replacement invalidate stale results. Cost label shows turn cost, `Σ` session cost, and `` monthly cost. Monthly cost is shown only when it is greater than session cost. Cost label is hidden when all costs are zero. Telemetry publishes only when UI exists. Duplicate snapshots are skipped by signature. Read/parse/update errors are swallowed so the agent loop keeps running.