From 5311e0c8abfb52fa76fc251d70b8809ebdac5904 Mon Sep 17 00:00:00 2001 From: MFPires Date: Tue, 28 Jan 2025 00:27:17 -0300 Subject: [PATCH] fix: Make context token counter more reliable - Only consider API requests with valid token information - Skip messages with invalid/missing token data - Prevent counter from resetting on action approval messages - Ensure both tokensIn and tokensOut are valid numbers This makes the context token counter more stable and accurate by only updating on valid API responses with complete token data. --- src/shared/getApiMetrics.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/shared/getApiMetrics.ts b/src/shared/getApiMetrics.ts index 0c8a61e..882c4aa 100644 --- a/src/shared/getApiMetrics.ts +++ b/src/shared/getApiMetrics.ts @@ -36,10 +36,18 @@ export function getApiMetrics(messages: ClineMessage[]): ApiMetrics { contextTokens: 0, } - // Find the last api_req_started message to get the context size - const lastApiReq = [...messages] - .reverse() - .find((message) => message.type === "say" && message.say === "api_req_started" && message.text) + // Find the last api_req_started message that has valid token information + const lastApiReq = [...messages].reverse().find((message) => { + if (message.type === "say" && message.say === "api_req_started" && message.text) { + try { + const parsedData = JSON.parse(message.text) + return typeof parsedData.tokensIn === "number" && typeof parsedData.tokensOut === "number" + } catch { + return false + } + } + return false + }) messages.forEach((message) => { if (message.type === "say" && message.say === "api_req_started" && message.text) {