Skip to content

feat: add token usage counter per session#30

Closed
aguung wants to merge 1 commit into
enowdev:mainfrom
aguung:feat/token-usage-counter
Closed

feat: add token usage counter per session#30
aguung wants to merge 1 commit into
enowdev:mainfrom
aguung:feat/token-usage-counter

Conversation

@aguung
Copy link
Copy Markdown

@aguung aguung commented May 14, 2026

Description

Tracks prompt and completion token counts from both OpenAI-compatible and Anthropic SSE streams, accumulates them per session, and displays the total as a badge in the chat header.

Backend (src-tauri/src/services/chat_service.rs)

  • Added TokenUsage, ChatUsageEvent, and UsageAccumulator structs using #[derive(serde::Serialize)] — no serde_json::json!() to stay clear of clippy::disallowed_methods
  • stream_openai_sse: adds stream_options.include_usage: true to the request so the final SSE chunk carries usage; captured in parse_openai_sse_line
  • stream_anthropic_sse: captures input_tokens from message_start and output_tokens from message_delta events
  • Both streaming functions now return (String, Option<TokenUsage>) instead of String
  • send_message_inner emits chat-usage Tauri event after each completed response
  • Includes the message_stop guard fix (stream ends without completion signal → explicit error) from the pending PR fix: detect incomplete Anthropic SSE stream and deduplicate system prompt #26

Frontend

  • src/types/index.ts: TokenUsage and ChatUsageEvent interfaces
  • src/stores/useChatStore.ts: sessionUsage: Record<string, TokenUsage>, addTokenUsage (cumulative per-session sum), clearSessionUsage
  • src/components/layout/AppShell.tsx: listens for chat-usage event, calls addTokenUsage
  • src/components/layout/ChatHeader.tsx: token badge next to session title; tooltip shows split prompt/completion counts; formats as 4.2k for large numbers

Preview

[My Project Name] ✏  4.2k tokens
                     ↑ 1.8k prompt · ↓ 2.4k completion  ← tooltip

Type of Change

  • New feature

How Has This Been Tested?

  • bunx tsc --noEmit passes (TypeScript) — only pre-existing baseUrl deprecation warning
  • cargo clippy -- -D warnings passes (Rust) — could not run in current environment due to missing GTK system deps on WSL
  • Manual testing steps below

Manual verification:

  • Confirmed UsageAccumulator::finish() returns None when both fields are 0 (no spurious events for providers that don't send usage)
  • Confirmed addTokenUsage accumulates across multiple turns per session (not reset per message)
  • Confirmed clearSessionUsage is available for callers to reset on session delete
  • Confirmed stream_options.include_usage is a standard OpenAI API field, silently ignored by providers that don't support it
  • Confirmed Anthropic usage fields match the official API spec: message_start.message.usage.input_tokens and message_delta.usage.output_tokens

Checklist

  • My code follows the project's style guidelines
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works

Tracks prompt and completion token counts from both OpenAI-compatible
and Anthropic SSE streams and accumulates them per session in the
frontend store.

Backend:
- TokenUsage / ChatUsageEvent / UsageAccumulator structs in
  chat_service.rs using serde::Serialize (no serde_json::json! to
  avoid clippy::disallowed_methods)
- stream_openai_sse: requests stream_options.include_usage=true so
  the final SSE chunk carries usage; parsed in parse_openai_sse_line
- stream_anthropic_sse: captures input_tokens from message_start and
  output_tokens from message_delta events
- Emits chat-usage Tauri event after each completed completion
- Also fixes stream_anthropic_sse to return error on missing
  message_stop (same as the pending PR enowdev#26)

Frontend:
- TokenUsage / ChatUsageEvent types added to types/index.ts
- useChatStore: sessionUsage record, addTokenUsage (cumulative
  per-session sum), clearSessionUsage
- AppShell: listens for chat-usage, calls addTokenUsage
- ChatHeader: shows total token badge next to session title;
  tooltip shows split prompt / completion counts; formats as
  4.2k for readability
@aguung
Copy link
Copy Markdown
Author

aguung commented May 14, 2026

Closing — contains duplicate changes from #26 that will cause conflict. Will rebase on top of fix/anthropic-stream-completion and reopen.

@aguung aguung closed this May 14, 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.

2 participants