Repository navigation
feat: add tpm breakdown display - #5647
SwillerDawn wants to merge 3 commits into
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
WalkthroughAdds cache read/write, input, and output token fields to usage log statistics. The backend computes and returns the breakdown, and the frontend shows it in a TPM hover card with localized labels. ChangesToken Breakdown Stats End-to-End
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
web/default/src/features/usage-logs/components/common-logs-stats.tsx (1)
67-95: Memoize TPM derived structures to avoid per-render allocations.
tpmStatsandtpmItemsare recreated on every render; wrapping them inuseMemowill prevent unnecessary downstream re-renders and align with coding guidelines.Proposed refactor
+import { useMemo } from 'react' import { useQuery } from '`@tanstack/react-query`' import { getRouteApi } from '`@tanstack/react-router`' import { useTranslation } from 'react-i18next' ... function TpmBreakdownHoverCard(props: { stats: LogStatistics | undefined }) { const { t } = useTranslation() - const tpmStats = { - cacheRead: statNumber(props.stats?.cache_read_tokens), - cacheWrite: statNumber(props.stats?.cache_write_tokens), - input: statNumber(props.stats?.input_tokens), - output: statNumber(props.stats?.output_tokens), - } + const tpmStats = useMemo( + () => ({ + cacheRead: statNumber(props.stats?.cache_read_tokens), + cacheWrite: statNumber(props.stats?.cache_write_tokens), + input: statNumber(props.stats?.input_tokens), + output: statNumber(props.stats?.output_tokens), + }), + [ + props.stats?.cache_read_tokens, + props.stats?.cache_write_tokens, + props.stats?.input_tokens, + props.stats?.output_tokens, + ] + ) const tpmTotal = statNumber(props.stats?.tpm) - const tpmItems = [ + const tpmItems = useMemo(() => [ { label: t('Cache Read'), value: tpmStats.cacheRead, accent: 'bg-emerald-500/70', }, { label: t('Cache Write'), value: tpmStats.cacheWrite, accent: 'bg-amber-500/75', }, { label: t('Input'), value: tpmStats.input, accent: 'bg-sky-500/70', }, { label: t('Output'), value: tpmStats.output, accent: 'bg-violet-500/70', }, - ] + ], [t, tpmStats])🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@web/default/src/features/usage-logs/components/common-logs-stats.tsx` around lines 67 - 95, The tpmStats object and tpmItems array are being recreated on every render, which causes unnecessary re-renders and memory allocations. Wrap both the tpmStats object definition and the tpmItems array definition in useMemo hooks with appropriate dependencies (props.stats and the translation function t) to ensure they are only recalculated when their dependencies change. This will prevent unnecessary downstream re-renders and improve performance.Source: Coding guidelines
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@model/log.go`:
- Around line 610-614: The cacheRead and cacheWrite variables can retain
negative values from the statOtherInt calls, which corrupts downstream stat
calculations and TPM totals. Clamp both cacheRead and cacheWrite to non-negative
values immediately after they are assigned from statOtherInt, and also clamp the
result of the conditional assignment on line 614. Additionally, apply the same
clamping to the similar cache token variable assignments that appear in the
lines also mentioned in the comment (629-635) to ensure all cache token values
are non-negative before they are added to stat fields.
---
Nitpick comments:
In `@web/default/src/features/usage-logs/components/common-logs-stats.tsx`:
- Around line 67-95: The tpmStats object and tpmItems array are being recreated
on every render, which causes unnecessary re-renders and memory allocations.
Wrap both the tpmStats object definition and the tpmItems array definition in
useMemo hooks with appropriate dependencies (props.stats and the translation
function t) to ensure they are only recalculated when their dependencies change.
This will prevent unnecessary downstream re-renders and improve performance.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: fe44a705-23cd-42fb-b2be-478d2547efec
📒 Files selected for processing (5)
controller/log.gomodel/log.goweb/default/src/features/usage-logs/components/common-logs-stats.tsxweb/default/src/features/usage-logs/constants.tsweb/default/src/features/usage-logs/types.ts
| cacheRead := statOtherInt(other, "cache_tokens") | ||
| cacheWrite := statOtherInt(other, "cache_creation_tokens") | ||
| if cacheWrite == 0 { | ||
| cacheWrite = statOtherInt(other, "cache_creation_tokens_5m") + statOtherInt(other, "cache_creation_tokens_1h") | ||
| } |
There was a problem hiding this comment.
Clamp cache token parts to non-negative before summing TPM.
cacheRead/cacheWrite can remain negative from other, and those values are added directly into stat.* and stat.Tpm. That can undercount or even produce invalid TPM totals.
Proposed fix
cacheRead := statOtherInt(other, "cache_tokens")
+ if cacheRead < 0 {
+ cacheRead = 0
+ }
cacheWrite := statOtherInt(other, "cache_creation_tokens")
- if cacheWrite == 0 {
+ if cacheWrite <= 0 {
cacheWrite = statOtherInt(other, "cache_creation_tokens_5m") + statOtherInt(other, "cache_creation_tokens_1h")
}
+ if cacheWrite < 0 {
+ cacheWrite = 0
+ }Also applies to: 629-635
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@model/log.go` around lines 610 - 614, The cacheRead and cacheWrite variables
can retain negative values from the statOtherInt calls, which corrupts
downstream stat calculations and TPM totals. Clamp both cacheRead and cacheWrite
to non-negative values immediately after they are assigned from statOtherInt,
and also clamp the result of the conditional assignment on line 614.
Additionally, apply the same clamping to the similar cache token variable
assignments that appear in the lines also mentioned in the comment (629-635) to
ensure all cache token values are non-negative before they are added to stat
fields.
|
感觉占的空间有点多,和现在的设计有点不符,能优化一下布局就好多了。 |
|
这个明细展示功能非常实用!可否请求增加一个换存命中率显示?在缓存读取/写入的栏位后面放一个小字写xx.x%? |
51fdfc5 to
2b6f1df
Compare


Important
📝 变更描述 / Description
在默认前端的用量日志统计区域,为 TPM 增加缓存读、缓存写、输入、输出 token 的明细展示,并在后端日志统计接口补充对应字段。
🚀 变更类型 / Type of change
🔗 关联任务 / Related Issue
✅ 提交前检查项 / Checklist
Bug fix,我已提交或关联对应 Issue,且不会将设计取舍、预期不一致或理解偏差直接归类为 bug。📸 运行证明 / Proof of Work
(请在此粘贴截图、关键日志或测试报告,以证明变更生效)

Summary by CodeRabbit
Release Notes