From 6ccb061d338cc2f37166b7fbad1b8ac88f51743c Mon Sep 17 00:00:00 2001 From: Vignesh Subbiah Date: Wed, 29 Jan 2025 22:25:01 +0530 Subject: [PATCH] Refactors the cached token code for all models --- src/api/providers/unbound.ts | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/api/providers/unbound.ts b/src/api/providers/unbound.ts index 921f07b..cc748ad 100644 --- a/src/api/providers/unbound.ts +++ b/src/api/providers/unbound.ts @@ -106,21 +106,21 @@ export class UnboundHandler implements ApiHandler, SingleCompletionHandler { } if (usage) { - if (this.getModel().id.startsWith("anthropic/")) { - yield { - type: "usage", - inputTokens: usage?.prompt_tokens || 0, - outputTokens: usage?.completion_tokens || 0, - cacheWriteTokens: (usage as any)?.cache_creation_input_tokens || 0, - cacheReadTokens: (usage as any)?.cache_read_input_tokens || 0, - } - } else { - yield { - type: "usage", - inputTokens: usage?.prompt_tokens || 0, - outputTokens: usage?.completion_tokens || 0, - } + const usageData: any = { + type: "usage", + inputTokens: usage?.prompt_tokens || 0, + outputTokens: usage?.completion_tokens || 0, } + + // Only add cache tokens if they exist + if ((usage as any)?.cache_creation_input_tokens) { + usageData.cacheWriteTokens = (usage as any).cache_creation_input_tokens + } + if ((usage as any)?.cache_read_input_tokens) { + usageData.cacheReadTokens = (usage as any).cache_read_input_tokens + } + + yield usageData } } }