From 111fb5ee1697df56e3f8669829d16d12edc32d38 Mon Sep 17 00:00:00 2001 From: majiayu000 <1835304752@qq.com> Date: Mon, 22 Dec 2025 18:54:29 +0800 Subject: [PATCH] fix: align web token usage display with TUI Fixes #3322 Use tokens from the last assistant message instead of summing all messages. This matches TUI behavior and shows the actual context size. Co-Authored-By: Claude --- packages/web/src/components/Share.tsx | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/web/src/components/Share.tsx b/packages/web/src/components/Share.tsx index 062449712..1e455dc86 100644 --- a/packages/web/src/components/Share.tsx +++ b/packages/web/src/components/Share.tsx @@ -277,9 +277,6 @@ export default function Share(props: { id: string; api: string; info: Session.In if (msg.role === "assistant") { result.cost += msg.cost - result.tokens.input += msg.tokens.input - result.tokens.output += msg.tokens.output - result.tokens.reasoning += msg.tokens.reasoning result.models[`${msg.providerID} ${msg.modelID}`] = [msg.providerID, msg.modelID] @@ -292,6 +289,16 @@ export default function Share(props: { id: string; api: string; info: Session.In } } } + + // Use tokens from the last assistant message (like TUI does) + // This represents the actual context size, not cumulative tokens + const lastAssistant = msgs.findLast((x) => x.role === "assistant" && x.tokens?.output > 0) + if (lastAssistant && lastAssistant.role === "assistant") { + result.tokens.input = lastAssistant.tokens.input + result.tokens.output = lastAssistant.tokens.output + result.tokens.reasoning = lastAssistant.tokens.reasoning + } + return result })