mirror of
https://github.com/RooVetGit/Roo-Code.git
synced 2026-10-10 03:28:03 +00:00
Fix: Roo Anthropic input token normalization (avoid double-count) (#9224)
This commit is contained in:
parent
bb6cac4980
commit
4e6cdad052
1 changed files with 17 additions and 4 deletions
|
|
@ -1,7 +1,7 @@
|
||||||
import { Anthropic } from "@anthropic-ai/sdk"
|
import { Anthropic } from "@anthropic-ai/sdk"
|
||||||
import OpenAI from "openai"
|
import OpenAI from "openai"
|
||||||
|
|
||||||
import { rooDefaultModelId } from "@roo-code/types"
|
import { rooDefaultModelId, getApiProtocol } from "@roo-code/types"
|
||||||
import { CloudService } from "@roo-code/cloud"
|
import { CloudService } from "@roo-code/cloud"
|
||||||
|
|
||||||
import type { ApiHandlerOptions, ModelRecord } from "../../shared/api"
|
import type { ApiHandlerOptions, ModelRecord } from "../../shared/api"
|
||||||
|
|
@ -163,12 +163,25 @@ export class RooHandler extends BaseOpenAiCompatibleProvider<string> {
|
||||||
const model = this.getModel()
|
const model = this.getModel()
|
||||||
const isFreeModel = model.info.isFree ?? false
|
const isFreeModel = model.info.isFree ?? false
|
||||||
|
|
||||||
|
// Normalize input tokens based on protocol expectations:
|
||||||
|
// - OpenAI protocol expects TOTAL input tokens (cached + non-cached)
|
||||||
|
// - Anthropic protocol expects NON-CACHED input tokens (caches passed separately)
|
||||||
|
const modelId = model.id
|
||||||
|
const apiProtocol = getApiProtocol("roo", modelId)
|
||||||
|
|
||||||
|
const promptTokens = lastUsage.prompt_tokens || 0
|
||||||
|
const cacheWrite = lastUsage.cache_creation_input_tokens || 0
|
||||||
|
const cacheRead = lastUsage.prompt_tokens_details?.cached_tokens || 0
|
||||||
|
const nonCached = Math.max(0, promptTokens - cacheWrite - cacheRead)
|
||||||
|
|
||||||
|
const inputTokensForDownstream = apiProtocol === "anthropic" ? nonCached : promptTokens
|
||||||
|
|
||||||
yield {
|
yield {
|
||||||
type: "usage",
|
type: "usage",
|
||||||
inputTokens: lastUsage.prompt_tokens || 0,
|
inputTokens: inputTokensForDownstream,
|
||||||
outputTokens: lastUsage.completion_tokens || 0,
|
outputTokens: lastUsage.completion_tokens || 0,
|
||||||
cacheWriteTokens: lastUsage.cache_creation_input_tokens,
|
cacheWriteTokens: cacheWrite,
|
||||||
cacheReadTokens: lastUsage.prompt_tokens_details?.cached_tokens,
|
cacheReadTokens: cacheRead,
|
||||||
totalCost: isFreeModel ? 0 : (lastUsage.cost ?? 0),
|
totalCost: isFreeModel ? 0 : (lastUsage.cost ?? 0),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue