Skip to content

Add local token usage dashboard with SQLite storage#236

Open
tdu-naifen wants to merge 1 commit intoericc-ch:masterfrom
tdu-naifen:feat/token-usage-dashboard
Open

Add local token usage dashboard with SQLite storage#236
tdu-naifen wants to merge 1 commit intoericc-ch:masterfrom
tdu-naifen:feat/token-usage-dashboard

Conversation

@tdu-naifen
Copy link
Copy Markdown

Track per-request input/output token usage in a local SQLite database and expose a live dashboard at /dashboard on the running server.

Features:

  • SQLite store (bun:sqlite) at ~/.local/share/copilot-api/token-usage.db
    • 30-day auto-prune, 1-minute bucket resolution
    • Atomic upsert per (timestamp_min, model) composite key
  • Token capture for both /chat/completions and /v1/messages endpoints
    • Non-streaming: reads response.usage from Copilot API response
    • Streaming: injects stream_options.include_usage=true so Copilot returns usage in the final SSE chunk; falls back to local tokenizer estimate
  • GET /token-usage?range=1h|6h|24h|7d|30d JSON API
  • GET /dashboard — shadcn-inspired dark UI with:
    • Stat cards (total requests, input tokens, output tokens)
    • Area chart (Chart.js via CDN, input vs output over time)
    • Per-model breakdown table
    • Copilot Budget section: fetches /usage and renders progress bars for premium_interactions, chat, completions quota with color thresholds (green <80%, yellow 80-95%, red >=95%, gray for unlimited)
    • 30-second auto-refresh
  • Unit tests for budget color/percent helpers (9 tests)
Screenshot 2026-04-11 at 12 05 23 PM

Track per-request input/output token usage in a local SQLite database
and expose a live dashboard at /dashboard on the running server.

Features:
- SQLite store (bun:sqlite) at ~/.local/share/copilot-api/token-usage.db
  - 30-day auto-prune, 1-minute bucket resolution
  - Atomic upsert per (timestamp_min, model) composite key
- Token capture for both /chat/completions and /v1/messages endpoints
  - Non-streaming: reads response.usage from Copilot API response
  - Streaming: injects stream_options.include_usage=true so Copilot
    returns usage in the final SSE chunk; falls back to local tokenizer estimate
- GET /token-usage?range=1h|6h|24h|7d|30d  JSON API
- GET /dashboard  — shadcn-inspired dark UI with:
  - Stat cards (total requests, input tokens, output tokens)
  - Area chart (Chart.js via CDN, input vs output over time)
  - Per-model breakdown table
  - Copilot Budget section: fetches /usage and renders progress bars for
    premium_interactions, chat, completions quota with color thresholds
    (green <80%, yellow 80-95%, red >=95%, gray for unlimited)
  - 30-second auto-refresh
- Unit tests for budget color/percent helpers (9 tests)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@tdu-naifen tdu-naifen changed the title feat: add local token usage dashboard with SQLite storage Add local token usage dashboard with SQLite storage Apr 11, 2026
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.

1 participant