feat: add token usage counter per session#31
Open
aguung wants to merge 2 commits into
Open
Conversation
…ompt stream_anthropic_sse previously returned partial output silently when the connection dropped before message_stop arrived. Now tracks message_stop_received and returns an explicit error so the user knows to retry rather than seeing a silently truncated response. Also propagates JSON parse errors in parse_anthropic_sse_line via ? instead of silently returning Ok(false), consistent with the OpenAI SSE parser. Extracts the duplicated CHAT_SYSTEM_INSTRUCTIONS into a single module- level constant — previously maintained as two identical inline concat! blocks in send_openai_compatible and send_anthropic.
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
enowdev
requested changes
May 15, 2026
Owner
enowdev
left a comment
There was a problem hiding this comment.
Nice feature idea, but I’m blocking this version because send_openai_compatible() now unconditionally adds stream_options: { include_usage: true } for every OpenAI-compatible provider (src-tauri/src/services/chat_service.rs:227-235). In this repo that path is also used for Ollama/custom gateways/other OpenAI-style backends, and many of them reject unknown request fields instead of ignoring them. That means this can break normal chat completions for providers that worked before. Please gate usage collection behind provider capability detection (or a fallback/retry path) before merging.
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.
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)TokenUsage,ChatUsageEvent, andUsageAccumulatorstructs using#[derive(serde::Serialize)]— noserde_json::json!()to stay clear ofclippy::disallowed_methodsstream_openai_sse: addsstream_options.include_usage: trueto the request so the final SSE chunk carries usage; captured inparse_openai_sse_linestream_anthropic_sse: capturesinput_tokensfrommessage_startandoutput_tokensfrommessage_deltaevents(String, Option<TokenUsage>)instead ofStringsend_message_inneremitschat-usageTauri event after each completed responseFrontend
src/types/index.ts:TokenUsageandChatUsageEventinterfacessrc/stores/useChatStore.ts:sessionUsage: Record<string, TokenUsage>,addTokenUsage(cumulative per-session sum),clearSessionUsagesrc/components/layout/AppShell.tsx: listens forchat-usageevent, callsaddTokenUsagesrc/components/layout/ChatHeader.tsx: token badge next to session title; tooltip shows split prompt/completion; formats as4.2kfor large numbersPreview
Type of Change
How Has This Been Tested?
bunx tsc --noEmitpasses (TypeScript) — only pre-existingbaseUrldeprecation warningcargo clippy -- -D warningspasses (Rust) — could not run in current environment due to missing GTK system deps on WSLManual verification:
UsageAccumulator::finish()returnsNonewhen both fields are 0 — no spurious events for providers that omit usageaddTokenUsageaccumulates across multiple turns per session, not reset per messagestream_options.include_usageis a standard OpenAI API field, silently ignored by providers that don't support itmessage_start.message.usage.input_tokensandmessage_delta.usage.output_tokensChecklist