@@ -74,7 +74,7 @@ import { RooTerminalProcess } from "../../integrations/terminal/types"
7474import { TerminalRegistry } from "../../integrations/terminal/TerminalRegistry"
7575
7676// utils
77- import { calculateApiCostAnthropic } from "../../shared/cost"
77+ import { calculateApiCostAnthropic , calculateApiCostOpenAI } from "../../shared/cost"
7878import { getWorkspacePath } from "../../utils/path"
7979
8080// prompts
@@ -1886,21 +1886,35 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
18861886 }
18871887
18881888 const existingData = JSON . parse ( this . clineMessages [ lastApiReqIndex ] . text || "{}" )
1889+
1890+ // Calculate total tokens and cost using provider-aware function
1891+ const modelId = getModelId ( this . apiConfiguration )
1892+ const apiProtocol = getApiProtocol ( this . apiConfiguration . apiProvider , modelId )
1893+
1894+ const costResult =
1895+ apiProtocol === "anthropic"
1896+ ? calculateApiCostAnthropic (
1897+ this . api . getModel ( ) . info ,
1898+ inputTokens ,
1899+ outputTokens ,
1900+ cacheWriteTokens ,
1901+ cacheReadTokens ,
1902+ )
1903+ : calculateApiCostOpenAI (
1904+ this . api . getModel ( ) . info ,
1905+ inputTokens ,
1906+ outputTokens ,
1907+ cacheWriteTokens ,
1908+ cacheReadTokens ,
1909+ )
1910+
18891911 this . clineMessages [ lastApiReqIndex ] . text = JSON . stringify ( {
18901912 ...existingData ,
1891- tokensIn : inputTokens ,
1892- tokensOut : outputTokens ,
1913+ tokensIn : costResult . totalInputTokens ,
1914+ tokensOut : costResult . totalOutputTokens ,
18931915 cacheWrites : cacheWriteTokens ,
18941916 cacheReads : cacheReadTokens ,
1895- cost :
1896- totalCost ??
1897- calculateApiCostAnthropic (
1898- this . api . getModel ( ) . info ,
1899- inputTokens ,
1900- outputTokens ,
1901- cacheWriteTokens ,
1902- cacheReadTokens ,
1903- ) ,
1917+ cost : totalCost ?? costResult . totalCost ,
19041918 cancelReason,
19051919 streamingFailedMessage,
19061920 } satisfies ClineApiReqInfo )
@@ -2104,21 +2118,34 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
21042118 await this . updateClineMessage ( apiReqMessage )
21052119 }
21062120
2107- // Capture telemetry
2121+ // Capture telemetry with provider-aware cost calculation
2122+ const modelId = getModelId ( this . apiConfiguration )
2123+ const apiProtocol = getApiProtocol ( this . apiConfiguration . apiProvider , modelId )
2124+
2125+ // Use the appropriate cost function based on the API protocol
2126+ const costResult =
2127+ apiProtocol === "anthropic"
2128+ ? calculateApiCostAnthropic (
2129+ this . api . getModel ( ) . info ,
2130+ tokens . input ,
2131+ tokens . output ,
2132+ tokens . cacheWrite ,
2133+ tokens . cacheRead ,
2134+ )
2135+ : calculateApiCostOpenAI (
2136+ this . api . getModel ( ) . info ,
2137+ tokens . input ,
2138+ tokens . output ,
2139+ tokens . cacheWrite ,
2140+ tokens . cacheRead ,
2141+ )
2142+
21082143 TelemetryService . instance . captureLlmCompletion ( this . taskId , {
2109- inputTokens : tokens . input ,
2110- outputTokens : tokens . output ,
2144+ inputTokens : costResult . totalInputTokens ,
2145+ outputTokens : costResult . totalOutputTokens ,
21112146 cacheWriteTokens : tokens . cacheWrite ,
21122147 cacheReadTokens : tokens . cacheRead ,
2113- cost :
2114- tokens . total ??
2115- calculateApiCostAnthropic (
2116- this . api . getModel ( ) . info ,
2117- tokens . input ,
2118- tokens . output ,
2119- tokens . cacheWrite ,
2120- tokens . cacheRead ,
2121- ) ,
2148+ cost : tokens . total ?? costResult . totalCost ,
21222149 } )
21232150 }
21242151 }
0 commit comments