Skip to content

Commit 0c4ca55

Browse files
committed
fix(roo-provider): normalize Anthropic input token accounting to avoid double-counting cached tokens; revert core-level heuristic to keep Task provider-agnostic
1 parent bb6cac4 commit 0c4ca55

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/api/providers/roo.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Anthropic } from "@anthropic-ai/sdk"
22
import OpenAI from "openai"
33

4-
import { rooDefaultModelId } from "@roo-code/types"
4+
import { rooDefaultModelId, getApiProtocol } from "@roo-code/types"
55
import { CloudService } from "@roo-code/cloud"
66

77
import type { ApiHandlerOptions, ModelRecord } from "../../shared/api"
@@ -163,12 +163,25 @@ export class RooHandler extends BaseOpenAiCompatibleProvider<string> {
163163
const model = this.getModel()
164164
const isFreeModel = model.info.isFree ?? false
165165

166+
// Normalize input tokens based on protocol expectations:
167+
// - OpenAI protocol expects TOTAL input tokens (cached + non-cached)
168+
// - Anthropic protocol expects NON-CACHED input tokens (caches passed separately)
169+
const modelId = model.id
170+
const apiProtocol = getApiProtocol("roo", modelId)
171+
172+
const promptTokens = lastUsage.prompt_tokens || 0
173+
const cacheWrite = lastUsage.cache_creation_input_tokens || 0
174+
const cacheRead = lastUsage.prompt_tokens_details?.cached_tokens || 0
175+
const nonCached = Math.max(0, promptTokens - cacheWrite - cacheRead)
176+
177+
const inputTokensForDownstream = apiProtocol === "anthropic" ? nonCached : promptTokens
178+
166179
yield {
167180
type: "usage",
168-
inputTokens: lastUsage.prompt_tokens || 0,
181+
inputTokens: inputTokensForDownstream,
169182
outputTokens: lastUsage.completion_tokens || 0,
170-
cacheWriteTokens: lastUsage.cache_creation_input_tokens,
171-
cacheReadTokens: lastUsage.prompt_tokens_details?.cached_tokens,
183+
cacheWriteTokens: cacheWrite,
184+
cacheReadTokens: cacheRead,
172185
totalCost: isFreeModel ? 0 : (lastUsage.cost ?? 0),
173186
}
174187
}

0 commit comments

Comments
 (0)