Skip to content

feat: add per-model token usage and cost statistics - #330

Open
ffftttft wants to merge 1 commit into
agegr:mainfrom
ffftttft:main
Open

feat: add per-model token usage and cost statistics#330
ffftttft wants to merge 1 commit into
agegr:mainfrom
ffftttft:main

Conversation

@ffftttft

Copy link
Copy Markdown

Closes #320

Problem

pi-web shows per-message token counts in the chat, but nothing aggregates them across sessions — there's no way to answer "how much have I spent per model this week/month?"

Changes

  • Usage store (lib/usage-store.ts) — persists usage to ~/.pi/agent/pi-web-usage.json as day × model buckets (input/output/cache-read/cache-write tokens, pi-recorded cost, message count, contributing session ids). Recording starts at first install (installedAt cutoff — no historical backfill, by design). Session files are only re-parsed when their mtime changes, per-entry seen ids make re-scans idempotent even when pi fully rewrites a session file, writes are atomic (tmp + rename), and concurrent requests share a single in-flight scan.
  • API (GET /api/usage?range=today|7d|30d|all) — runs the incremental scan, then aggregates the stored buckets for the requested range: per-model totals, grand totals, and daily cost/token/message series. Invalid range values fall back to 7d.
  • UI (components/UsageStats.tsx) — modal styled after ModelsConfig: range tabs, summary cards (total cost / tokens / messages / sessions), a daily cost bar strip, and a per-model table with cost-share bars. Opened from a bar-chart button in the sidebar header, next to the New button.
  • i18nusage.* strings added to both en.ts and zh-CN.ts.

Notes

  • Costs reuse pi's own per-message usage.cost records (already priced from models.json), so no pricing-catalog lookup is needed and the numbers match what the chat shows per message. Entries without usage data are skipped; the UI labels costs as estimates.
  • Read-only with respect to existing behavior: no agent runtime changes, no session file schema changes, no changes to existing APIs.
  • Verified: tsc --noEmit clean, npm run lint clean, all existing node --test files pass, and an end-to-end smoke test (synthetic session file → scan → aggregate → idempotent re-scan → clean rebuild) passed. The modal was exercised against real session data in headless Chromium, including range switching.

Adds a usage tracking panel to the sidebar header:

- lib/usage-store.ts persists usage to ~/.pi/agent/pi-web-usage.json,
  recording only assistant entries created after first install
  (installedAt cutoff — no historical backfill). Session files are
  re-parsed only on mtime change and per-entry seen ids make re-scans
  idempotent, so full session-file rewrites never double count.
- GET /api/usage?range=today|7d|30d|all scans incrementally and
  aggregates per model (tokens, cache r/w, pi-recorded cost, messages,
  sessions) plus daily totals.
- UsageStats.tsx modal: range tabs, summary cards, daily cost bars and
  a per-model table with cost share, styled after ModelsConfig.
- Sidebar header gains a bar-chart icon button next to the new-session
  button; en/zh-CN i18n strings included.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: per-model token usage and cost statistics

1 participant