Browse Source

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.
MFPires 11 months ago
parent
commit
5311e0c8ab
1 changed files with 12 additions and 4 deletions
  1. 12 4
      src/shared/getApiMetrics.ts

+ 12 - 4
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) {