Track and display per-session token usage in chat and sidebar #2

Merged
kubus merged 1 commit from t3code/add-token-count-chat-sidebar into main 2026-09-28 22:03:02 +00:00
Owner

Summary

  • Persist provider-reported usage onto assistant message metadata in the kernel, and relay it as a stream event.
  • Add addUsage, usageTotal, and sumUsage helpers in @hat/core so totals are only summed when a provider actually reports them.
  • Aggregate usage over each session's active branch via a single recursive query in the SQLite store, exposed through usageBySession().
  • Include per-session usage in the /api/sessions response.
  • Show a live token count in the chat header and a per-session count in the sidebar, with a breakdown tooltip.
  • Add a web test script and tests covering usage math, kernel persistence, store aggregation, and token formatting.

Testing

  • pnpm --filter @hat/core test
  • pnpm --filter @hat/kernel test
  • pnpm --filter @hat/store-sqlite test
  • pnpm --filter @hat/web test
  • pnpm --filter @hat/web typecheck
  • Not run: manual browser verification of the live header readout and sidebar counts.
## Summary - Persist provider-reported `usage` onto assistant message metadata in the kernel, and relay it as a stream event. - Add `addUsage`, `usageTotal`, and `sumUsage` helpers in `@hat/core` so totals are only summed when a provider actually reports them. - Aggregate usage over each session's active branch via a single recursive query in the SQLite store, exposed through `usageBySession()`. - Include per-session usage in the `/api/sessions` response. - Show a live token count in the chat header and a per-session count in the sidebar, with a breakdown tooltip. - Add a web `test` script and tests covering usage math, kernel persistence, store aggregation, and token formatting. ## Testing - `pnpm --filter @hat/core test` - `pnpm --filter @hat/kernel test` - `pnpm --filter @hat/store-sqlite test` - `pnpm --filter @hat/web test` - `pnpm --filter @hat/web typecheck` - Not run: manual browser verification of the live header readout and sidebar counts.
- Persist provider-reported usage on assistant message metadata
- Sum active-branch usage per session via recursive query in the SQLite store
- Show a live token count in the chat and per-session totals in the sidebar
kubus merged commit d941ef8cc9 into main 2026-09-28 22:03:02 +00:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
kubus/hatchat!2
No description provided.