Skip to content

fix(client): show token usage on replies when Show Cost is enabled - #2240

Open
Phoenix0531-sudo wants to merge 4 commits into
EKKOLearnAI:mainfrom
Phoenix0531-sudo:fix/show-cost-token-usage-in-replies-2140
Open

fix(client): show token usage on replies when Show Cost is enabled#2240
Phoenix0531-sudo wants to merge 4 commits into
EKKOLearnAI:mainfrom
Phoenix0531-sudo:fix/show-cost-token-usage-in-replies-2140

Conversation

@Phoenix0531-sudo

Copy link
Copy Markdown

Summary

Display settings already had a Show Cost toggle (show_cost) with the hint “Show token usage in replies”, but chat bubbles never read that setting. With the toggle on, replies still looked empty of any token information.

This wires the existing session inputTokens / outputTokens into MessageItem and shows them on the latest completed assistant reply when show_cost is enabled. Management of the setting itself is unchanged; usage is still session-cumulative (same numbers the context meter already tracks).

Validation

  • npm run test -- tests/client/message-item-token-usage.test.ts
  • npm run harness:check
  • npm run build

Fixes #2140

@Phoenix0531-sudo
Phoenix0531-sudo marked this pull request as ready for review July 27, 2026 11:17
@EKKOLearnAI

Copy link
Copy Markdown
Owner

我本地实际测试了这版,目前仍有两个问题,暂时不能合并:

  1. 显示后会消失usage.updated 到达时能显示,但 refreshSessionListOnly() 会定时刷新会话列表,并用列表接口返回的 inputTokens/outputTokens 覆盖当前值。当前 sessions 表里的这两个字段仍是 0/0,所以刷新或最多约 12 秒后,token 又被覆盖成 0,组件随即隐藏。

  2. 统计口径不对MessageItem 直接读取 chatStore.activeSession.inputTokens/outputTokens,这是会话级字段,却挂在最新一条回复下面,并不是该回复对应 run 的用量。而且这两个字段目前被混用:Hermes 运行结束时 calcAndUpdateUsage() 写入的是消息历史估算值;重连时 loadSessionStateFromDb() 写入的是 session_usage 全会话累计;会话列表刷新后又可能变成 0/0

以我测试的会话 ms337ldsrronvz 为例:全会话记录为 input 91,281、cache read 122,368、output 1,177;最新一次天气回复对应的 run 实际包含 4 次模型调用,合计 input 17,247、cache read 122,112、output 1,014。当前实现既不能稳定显示,也不能表示这条回复的真实用量。

建议后端按 run_id 聚合并返回 { input, output, cacheRead, reasoning },将它绑定到对应的最终 assistant message;会话累计、当前 context 和单次回复 usage 使用独立字段。同时避免 session list 的旧 0/0 覆盖运行时 usage。

Display settings already had show_cost, but chat replies never rendered
it. Surface cumulative session input/output tokens on the latest
completed assistant message when the toggle is on.
Addressed review on EKKOLearnAI#2240:

1. Do not let session-list 0/0 overwrite runtime session usage from
   usage.updated / run.completed during refreshSessionListOnly.
2. Aggregate session_usage rows by run_id (exact + prefix) and emit
   runUsage on run.completed for hermes / ekko / coding-agent.
3. Bind that runUsage to the completed assistant message and render
   it under Show Cost, instead of cumulative session counters.

Tests cover MessageItem run usage rendering, session-list preservation,
and getRecordedUsageByRun JSON aggregation.
@Phoenix0531-sudo
Phoenix0531-sudo force-pushed the fix/show-cost-token-usage-in-replies-2140 branch from e99fe23 to 315703e Compare July 27, 2026 12:19
@Phoenix0531-sudo

Copy link
Copy Markdown
Author

Addressed both review points:

  1. Session list no longer wipes runtime usage
    refreshSessionListOnly() only updates inputTokens/outputTokens when the list payload has non-zero values, so 0/0 from sessions no longer hides a live reply usage after ~12s.

  2. Reply usage is now run-scoped, not session-cumulative

    • Added getRecordedUsageByRun(sessionId, source, runId) over session_usage (exact run_id + ${runId}:... model-call rows).
    • Hermes / Ekko / coding-agent run.completed now emit runUsage: { input, output, cacheRead, cacheWrite, reasoning, apiCalls }.
    • Client binds that to the completed assistant message (message.usage / message.runId).
    • MessageItem renders that per-reply usage under Show Cost (with cache when present). Session totals are no longer shown as reply cost.

Validation:

  • npm test -- tests/client/message-item-token-usage.test.ts tests/client/chat-store-run-usage.test.ts tests/server/usage-store.test.ts → 19 passed

Not yet done: persisting per-message usage into DB history for reload-after-restart. Live run + reconnect-in-session path is covered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: 回复中看不到token使用量

2 participants