mirror of
https://github.com/RooVetGit/Roo-Code.git
synced 2026-09-06 08:18:39 +00:00
fix: correct context token calculation for Anthropic-style APIs
The context length was being doubled for Anthropic-style APIs because tokensIn was storing totalInputTokens (which includes cache tokens), but getApiMetrics was using it directly in the context calculation. This fix ensures tokensIn stores only base input tokens (without cache), so the context length calculation (tokensIn + tokensOut) works correctly for both Anthropic and OpenAI protocols. Cache tokens are still tracked separately in cacheWrites and cacheReads for display purposes. Fixes issue where switching to Anthropic models showed 2x the actual context length (e.g., 700k showing as 1.4M).
This commit is contained in:
parent
bb6cac4980
commit
8dbc1031ff
2 changed files with 9 additions and 4 deletions
|
|
@ -1920,9 +1920,13 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
|
|||
cacheReadTokens,
|
||||
)
|
||||
|
||||
// For tokensIn, store the base input tokens (not including cache tokens)
|
||||
// This ensures context length calculation works correctly in getApiMetrics
|
||||
// where it adds tokensIn + tokensOut. The cache tokens are stored separately
|
||||
// in cacheWrites and cacheReads for display purposes.
|
||||
this.clineMessages[lastApiReqIndex].text = JSON.stringify({
|
||||
...existingData,
|
||||
tokensIn: costResult.totalInputTokens,
|
||||
tokensIn: inputTokens,
|
||||
tokensOut: costResult.totalOutputTokens,
|
||||
cacheWrites: cacheWriteTokens,
|
||||
cacheReads: cacheReadTokens,
|
||||
|
|
|
|||
|
|
@ -82,9 +82,10 @@ export function getApiMetrics(messages: ClineMessage[]) {
|
|||
const parsedText: ParsedApiReqStartedTextType = JSON.parse(message.text)
|
||||
const { tokensIn, tokensOut } = parsedText
|
||||
|
||||
// Since tokensIn now stores TOTAL input tokens (including cache tokens),
|
||||
// we no longer need to add cacheWrites and cacheReads separately.
|
||||
// This applies to both Anthropic and OpenAI protocols.
|
||||
// tokensIn contains base input tokens (without cache tokens).
|
||||
// This works correctly for both Anthropic and OpenAI protocols because:
|
||||
// - Cache tokens are stored separately in cacheWrites/cacheReads
|
||||
// - Context length = base input + output tokens
|
||||
result.contextTokens = (tokensIn || 0) + (tokensOut || 0)
|
||||
} catch (error) {
|
||||
console.error("Error parsing JSON:", error)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue