Skip to content

feat: add /stats command to view conversation token usage#7831

Merged
Soulter merged 6 commits intomasterfrom
feat/conv-stats-command
Apr 27, 2026
Merged

feat: add /stats command to view conversation token usage#7831
Soulter merged 6 commits intomasterfrom
feat/conv-stats-command

Conversation

@Soulter
Copy link
Copy Markdown
Member

@Soulter Soulter commented Apr 27, 2026

改动内容

新增内置指令 /stats,可查看当前对话的 token 使用统计。

实现方式

在已有的 ConversationCommands 类中新增 stats() 方法:

  • 通过 conversation_manager.get_curr_conversation_id() 获取当前对话 ID
  • 查询 ProviderStat 表中该对话的所有记录
  • 聚合统计 token_input_othertoken_input_cachedtoken_output

使用效果

📊 Conversation Stats (ID: abc12345...)
───
Total Calls: 12
Successful: 12
───
Input Tokens (other): 8,520
Input Tokens (cached): 1,230
Output Tokens:        3,456
Total Tokens:         13,206

涉及文件

  • astrbot/builtin_stars/builtin_commands/commands/conversation.py — 核心逻辑
  • astrbot/builtin_stars/builtin_commands/main.py — 注册命令

Summary by Sourcery

New Features:

  • Introduce a conversation stats command that aggregates token usage metrics for the active conversation, including call counts and input/output token totals.

- Add stats() method to ConversationCommands that queries ProviderStat
  records by conversation_id and aggregates token breakdowns
- Register /stats command in main.py
@dosubot dosubot Bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Apr 27, 2026
Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • 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.
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.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread astrbot/builtin_stars/builtin_commands/commands/conversation.py Outdated
Comment thread astrbot/builtin_stars/builtin_commands/commands/conversation.py Outdated
@Soulter Soulter merged commit c5ab4f7 into master Apr 27, 2026
20 checks passed
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant