Skip to content

fix(stats): TPM now only counts output tokens#7827

Merged
Soulter merged 2 commits intomasterfrom
fix/tpm-output-only
Apr 27, 2026
Merged

fix(stats): TPM now only counts output tokens#7827
Soulter merged 2 commits intomasterfrom
fix/tpm-output-only

Conversation

@Soulter
Copy link
Copy Markdown
Member

@Soulter Soulter commented Apr 27, 2026

改动内容

问题

TPM(平均每分钟词元数)之前统计的是总 token(input + output),但作为生成性能指标,应该只统计 output tokens。

改动

  • 后端 (stat.py): 新增 range_total_output_tokens,仅累加 record.token_output,TPM 公式改为使用 output tokens
  • 前端国际化: 标签明确为输出词元数
    • zh-CN: 平均每分钟输出(TPM)
    • en-US: Average Output TPM
    • ru-RU: Средний Output TPM

影响

  • ✅ 仅 TPM 指标改为 output-only
  • ✅ 其他统计(趋势图、Provider/UMO排名、今日统计等)保持原样不变

Summary by Sourcery

Update token statistics so that TPM is calculated using only output tokens and align UI labels accordingly.

Bug Fixes:

  • Correct TPM calculation to use only output tokens instead of total input+output tokens.

Enhancements:

  • Add a dedicated accumulator for output tokens in provider token statistics to support accurate TPM computation.

Documentation:

  • Clarify stats UI labels in all supported locales to indicate that TPM reflects average output tokens per minute.

- Add range_total_output_tokens accumulation, separate from total tokens
- Change range_avg_tpm formula to use output tokens only
- Update i18n labels to reflect Output TPM
@dosubot dosubot Bot added size:XS This PR changes 0-9 lines, ignoring generated files. area:core The bug / feature is about astrbot's core, backend area:webui The bug / feature is about webui(dashboard) of astrbot. labels 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:

  • Consider guarding range_total_output_tokens += record.token_output against record.token_output being None or missing to avoid potential runtime errors when older or malformed records are processed.
  • Now that range_avg_tpm is output-only, it might be helpful to rename range_total_tokens to something like range_total_io_tokens (or similar) where it is still used, to avoid confusion between total tokens and output-only tokens in future maintenance.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider guarding `range_total_output_tokens += record.token_output` against `record.token_output` being `None` or missing to avoid potential runtime errors when older or malformed records are processed.
- Now that `range_avg_tpm` is output-only, it might be helpful to rename `range_total_tokens` to something like `range_total_io_tokens` (or similar) where it is still used, to avoid confusion between total tokens and output-only tokens in future maintenance.

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 updates the statistics dashboard to calculate the Average TPM (Tokens Per Minute) based on output tokens instead of total tokens, with corresponding updates to the English, Russian, and Chinese localization files. A review comment points out a potential logic error in the TPM calculation: the total output tokens are accumulated for all records, whereas the total duration used as the denominator only accounts for records with valid start and end times, which could lead to inaccurate averages.

Comment thread astrbot/dashboard/routes/stat.py Outdated
@dosubot
Copy link
Copy Markdown

dosubot Bot commented Apr 27, 2026

Related Documentation

1 document(s) may need updating based on files changed in this PR:

AstrBotTeam's Space

pr4697的改动
View Suggested Changes
@@ -904,7 +904,7 @@
   - `range_total_calls`:选定时间范围内的总 API 调用次数
   - `range_avg_ttft_ms`:首个令牌平均响应时间(毫秒)
   - `range_avg_duration_ms`:请求平均持续时间(毫秒)
-  - `range_avg_tpm`:平均每分钟令牌数(TPM)
+  - `range_avg_tpm`:平均每分钟输出令牌数(Output TPM),仅统计输出令牌,不包含输入令牌
   - `range_success_rate`:API 调用成功率(0-1 之间)
   - `range_by_provider`:按提供商分类的令牌消耗排名
   - `range_by_umo`:按统一消息来源(UMO)分类的令牌消耗排名

[Accept] [Decline]

How did I do? Any feedback?  Join Discord

@Soulter Soulter merged commit 1efe4fd into master Apr 27, 2026
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core The bug / feature is about astrbot's core, backend area:webui The bug / feature is about webui(dashboard) of astrbot. size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant