feat: add per-model token usage and cost statistics - #330
Open
ffftttft wants to merge 1 commit into
Open
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
lib/usage-store.ts) — persists usage to~/.pi/agent/pi-web-usage.jsonas day × model buckets (input/output/cache-read/cache-write tokens, pi-recorded cost, message count, contributing session ids). Recording starts at first install (installedAtcutoff — 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.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 to7d.components/UsageStats.tsx) — modal styled afterModelsConfig: 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.usage.*strings added to bothen.tsandzh-CN.ts.Notes
usage.costrecords (already priced frommodels.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.tsc --noEmitclean,npm run lintclean, all existingnode --testfiles 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.