feat: add /stats command to view conversation token usage#7831
Merged
feat: add /stats command to view conversation token usage#7831
Conversation
- Add stats() method to ConversationCommands that queries ProviderStat records by conversation_id and aggregates token breakdowns - Register /stats command in main.py
Contributor
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- The new
/statsmessages are fully in English while other built-in conversation commands appear to use Chinese text; consider aligning the language and style of user-facing strings for consistency across commands. - When aggregating
token_input_other,token_input_cached, andtoken_output, consider guarding againstNonevalues on older or partialProviderStatrows (e.g., viaor 0orcoalesce) to prevent runtime errors duringsum(). - Right now token counts include all records regardless of
status; if failed/error calls can contain partial or zero tokens, you might want to either excludestatus == 'error'from token aggregation or present a separate breakdown so the numbers are clearly interpretable.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The new `/stats` messages are fully in English while other built-in conversation commands appear to use Chinese text; consider aligning the language and style of user-facing strings for consistency across commands.
- When aggregating `token_input_other`, `token_input_cached`, and `token_output`, consider guarding against `None` values on older or partial `ProviderStat` rows (e.g., via `or 0` or `coalesce`) to prevent runtime errors during `sum()`.
- Right now token counts include all records regardless of `status`; if failed/error calls can contain partial or zero tokens, you might want to either exclude `status == 'error'` from token aggregation or present a separate breakdown so the numbers are clearly interpretable.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Contributor
There was a problem hiding this comment.
Code Review
This pull request introduces a new /stats command to display token usage statistics for the current conversation. The feedback suggests optimizing the statistics calculation by using SQL aggregation functions (such as func.sum and func.count) to perform the computation within the database rather than fetching all records and aggregating them in Python, which improves performance and memory efficiency for long-running conversations.
Co-authored-by: Copilot <copilot@github.com>
LIghtJUNction
pushed a commit
that referenced
this pull request
Apr 28, 2026
* feat: add /stats command to view conversation token usage - Add stats() method to ConversationCommands that queries ProviderStat records by conversation_id and aggregates token breakdowns - Register /stats command in main.py * feat: reorder conversation stats output for better readability Co-authored-by: Copilot <copilot@github.com> * feat: reorder token usage output for improved clarity * feat: enhance stats command to aggregate conversation token usage * feat: add cached input tokens display and update translations for clarity * feat: update stats command to clarify conversation token usage display --------- Co-authored-by: Copilot <copilot@github.com>
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.
改动内容
新增内置指令
/stats,可查看当前对话的 token 使用统计。实现方式
在已有的
ConversationCommands类中新增stats()方法:conversation_manager.get_curr_conversation_id()获取当前对话 IDProviderStat表中该对话的所有记录token_input_other、token_input_cached、token_output使用效果
涉及文件
astrbot/builtin_stars/builtin_commands/commands/conversation.py— 核心逻辑astrbot/builtin_stars/builtin_commands/main.py— 注册命令Summary by Sourcery
New Features: