From 4df486fa5b411ce1bae935fd98ac614c14bca340 Mon Sep 17 00:00:00 2001 From: Saoud Rizwan <7799382+saoudrizwan@users.noreply.github.com> Date: Fri, 5 Dec 2025 13:01:02 -0800 Subject: [PATCH] fix: restore cost calculation for Anthropic API requests (#7943) The taskMetrics.totalCost was incorrectly initialized to 0 instead of undefined in commit 09692d7d3. This broke cost display for providers like Anthropic that don't return totalCost in their usage chunks. When totalCost is 0, the fallback to calculateApiCostAnthropic() in updateApiReqMsg doesn't trigger because the nullish coalescing operator (??) only falls back for null/undefined, not 0. By initializing totalCost to undefined: - Providers that return totalCost (like OpenRouter) use that value - Providers that don't (like Anthropic) fall back to calculating cost from token counts and model pricing info --- src/core/task/index.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/core/task/index.ts b/src/core/task/index.ts index 9ecf4235..1aca65d0 100644 --- a/src/core/task/index.ts +++ b/src/core/task/index.ts @@ -2708,7 +2708,13 @@ export class Task { await this.postStateToWebview() try { - const taskMetrics = { cacheWriteTokens: 0, cacheReadTokens: 0, inputTokens: 0, outputTokens: 0, totalCost: 0 } + const taskMetrics: { + cacheWriteTokens: number + cacheReadTokens: number + inputTokens: number + outputTokens: number + totalCost: number | undefined + } = { cacheWriteTokens: 0, cacheReadTokens: 0, inputTokens: 0, outputTokens: 0, totalCost: undefined } const abortStream = async (cancelReason: ClineApiReqCancelReason, streamingFailedMessage?: string) => { if (this.diffViewProvider.isEditing) {