mirror of
https://github.com/RooVetGit/Roo-Code.git
synced 2026-08-28 05:27:24 +00:00
fix: improve token usage extraction for OpenAI-compatible proxies
- Add support for alternative field names used by various OpenAI-compatible proxies - Check for usage_metadata field in addition to usage field - Try multiple token field variations (prompt_tokens, input_tokens, promptTokens, etc.) - Add debug logging when tokens are present but not extracted correctly - Fixes issue where Open WebUI proxy responses show 0 tokens despite having valid usage data Fixes #9514
This commit is contained in:
parent
cdc72750e4
commit
42b730a947
1 changed files with 28 additions and 4 deletions
|
|
@ -188,8 +188,13 @@ export abstract class BaseOpenAiCompatibleProvider<ModelName extends string>
|
|||
toolCallAccumulator.clear()
|
||||
}
|
||||
|
||||
// Check for usage data in the chunk
|
||||
// Open WebUI and other proxies might include usage data differently
|
||||
if (chunk.usage) {
|
||||
lastUsage = chunk.usage
|
||||
} else if ((chunk as any).usage_metadata) {
|
||||
// Some proxies use usage_metadata instead of usage
|
||||
lastUsage = (chunk as any).usage_metadata
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -218,15 +223,34 @@ export abstract class BaseOpenAiCompatibleProvider<ModelName extends string>
|
|||
}
|
||||
|
||||
protected processUsageMetrics(usage: any, modelInfo?: any): ApiStreamUsageChunk {
|
||||
const inputTokens = usage?.prompt_tokens || 0
|
||||
const outputTokens = usage?.completion_tokens || 0
|
||||
const cacheWriteTokens = usage?.prompt_tokens_details?.cache_write_tokens || 0
|
||||
const cacheReadTokens = usage?.prompt_tokens_details?.cached_tokens || 0
|
||||
// Try different field names that various OpenAI-compatible proxies might use
|
||||
const inputTokens = usage?.prompt_tokens || usage?.input_tokens || usage?.promptTokens || 0
|
||||
const outputTokens = usage?.completion_tokens || usage?.output_tokens || usage?.completionTokens || 0
|
||||
|
||||
// Cache tokens might be in different locations
|
||||
const cacheWriteTokens =
|
||||
usage?.prompt_tokens_details?.cache_write_tokens ||
|
||||
usage?.prompt_tokens_details?.cache_creation_input_tokens ||
|
||||
usage?.cache_creation_input_tokens ||
|
||||
0
|
||||
const cacheReadTokens =
|
||||
usage?.prompt_tokens_details?.cached_tokens ||
|
||||
usage?.prompt_tokens_details?.cache_read_input_tokens ||
|
||||
usage?.cache_read_input_tokens ||
|
||||
0
|
||||
|
||||
const { totalCost } = modelInfo
|
||||
? calculateApiCostOpenAI(modelInfo, inputTokens, outputTokens, cacheWriteTokens, cacheReadTokens)
|
||||
: { totalCost: 0 }
|
||||
|
||||
// Log for debugging Open WebUI integration
|
||||
if (usage && inputTokens === 0 && outputTokens === 0 && usage.total_tokens > 0) {
|
||||
console.debug(
|
||||
`[BaseOpenAiCompatibleProvider] Usage data present but tokens not extracted correctly:`,
|
||||
usage,
|
||||
)
|
||||
}
|
||||
|
||||
return {
|
||||
type: "usage",
|
||||
inputTokens,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue