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
This commit is contained in:
Saoud Rizwan
2025-12-05 13:01:02 -08:00
committed by GitHub
parent 5fc6d4e9e3
commit 4df486fa5b
+7 -1
View File
@@ -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) {