From 97fe13dcb163ae422b05bbdb691a81bb47093379 Mon Sep 17 00:00:00 2001 From: MFPires Date: Mon, 27 Jan 2025 23:12:01 -0300 Subject: [PATCH] fix: Prevent context token counter from resetting on failed API calls - Only update context tokens when both input and output tokens are non-zero - Keep previous context token count when API calls fail - Avoid resetting counter on partial or failed responses This makes the context token counter more resilient against edge cases and provides more accurate context size tracking during API failures. --- src/shared/getApiMetrics.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/shared/getApiMetrics.ts b/src/shared/getApiMetrics.ts index b4caadc..0c8a61e 100644 --- a/src/shared/getApiMetrics.ts +++ b/src/shared/getApiMetrics.ts @@ -65,7 +65,10 @@ export function getApiMetrics(messages: ClineMessage[]): ApiMetrics { // If this is the last api request, use its tokens for context size if (message === lastApiReq) { - result.contextTokens = (tokensIn || 0) + (tokensOut || 0) + // Only update context tokens if both input and output tokens are non-zero + if (tokensIn > 0 && tokensOut > 0) { + result.contextTokens = tokensIn + tokensOut + } } } catch (error) { console.error("Error parsing JSON:", error)