From ca4fdcf3c4e6462ac7807a362c933d0883a66f56 Mon Sep 17 00:00:00 2001 From: Dennis Bartlett Date: Fri, 7 Mar 2025 17:27:09 -0800 Subject: [PATCH] Fix Token Count when API incorrectly returns token count per chunk. (#2145) * Fix Token Count when API incorrectly returns token count per chunk. * changeset * Update src/core/Cline.ts Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com> * Update src/core/Cline.ts Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com> --------- Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com> --- .changeset/lovely-zoos-work.md | 5 +++++ src/core/Cline.ts | 30 +++++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 .changeset/lovely-zoos-work.md diff --git a/.changeset/lovely-zoos-work.md b/.changeset/lovely-zoos-work.md new file mode 100644 index 00000000..23570a73 --- /dev/null +++ b/.changeset/lovely-zoos-work.md @@ -0,0 +1,5 @@ +--- +"claude-dev": patch +--- + +Fix Token Count when API incorrectly returns token count per chunk diff --git a/src/core/Cline.ts b/src/core/Cline.ts index 81c997f8..3078c48f 100644 --- a/src/core/Cline.ts +++ b/src/core/Cline.ts @@ -3174,6 +3174,20 @@ export class Cline { let assistantMessage = "" let reasoningMessage = "" this.isStreaming = true + let firstChunkTokenCount: { + inputTokens: number | undefined + outputTokens: number | undefined + cacheWriteTokens: number | undefined + cacheReadTokens: number | undefined + totalCost: number | undefined + } = { + inputTokens: undefined, + outputTokens: undefined, + cacheWriteTokens: undefined, + cacheReadTokens: undefined, + totalCost: undefined, + } + const inputTokenCounts = [] try { for await (const chunk of stream) { if (!chunk) { @@ -3181,11 +3195,15 @@ export class Cline { } switch (chunk.type) { case "usage": + inputTokenCounts.push(chunk.inputTokens) inputTokens += chunk.inputTokens outputTokens += chunk.outputTokens cacheWriteTokens += chunk.cacheWriteTokens ?? 0 cacheReadTokens += chunk.cacheReadTokens ?? 0 - totalCost = chunk.totalCost + totalCost = chunk.totalCost ?? 0 + if (firstChunkTokenCount.inputTokens === undefined) { + firstChunkTokenCount = { inputTokens, outputTokens, cacheWriteTokens, cacheReadTokens, totalCost } + } break case "reasoning": // reasoning will always come before assistant message @@ -3233,6 +3251,16 @@ export class Cline { break } } + + // Naive check to see if all of the chunks have the same token count due to poor implementation of the API + const someTokenCountsAreDifferent = inputTokenCounts.some((tokens) => tokens !== firstChunkTokenCount.inputTokens) + if (!someTokenCountsAreDifferent) { + inputTokens = firstChunkTokenCount.inputTokens ?? 0 + outputTokens = firstChunkTokenCount.outputTokens ?? 0 + cacheWriteTokens = firstChunkTokenCount.cacheWriteTokens ?? 0 + cacheReadTokens = firstChunkTokenCount.cacheReadTokens ?? 0 + totalCost = firstChunkTokenCount.totalCost ?? 0 + } } catch (error) { // abandoned happens when extension is no longer waiting for the cline instance to finish aborting (error is thrown here when any function in the for loop throws due to this.abort) if (!this.abandoned) {