Skip to content

Commit 86eb383

Browse files
committed
fix: prevent context token counter flickering during API requests
Maintains the last valid context token count between API requests by: - Adding tracking of last valid context tokens - Using last known value as fallback when current request lacks token data - Only updating when new valid token counts are available This prevents the context counter from flickering to "-" during tool calls and API request transitions, improving the UI stability.
1 parent 9056a59 commit 86eb383

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/shared/getApiMetrics.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ export function getApiMetrics(messages: ClineMessage[]): ApiMetrics {
4949
return false
5050
})
5151

52+
// Keep track of the last valid context tokens
53+
let lastValidContextTokens = 0
54+
5255
messages.forEach((message) => {
5356
if (message.type === "say" && message.say === "api_req_started" && message.text) {
5457
try {
@@ -71,12 +74,15 @@ export function getApiMetrics(messages: ClineMessage[]): ApiMetrics {
7174
result.totalCost += cost
7275
}
7376

77+
// Update last valid context tokens whenever we have valid input and output tokens
78+
if (tokensIn > 0 && tokensOut > 0) {
79+
lastValidContextTokens = tokensIn + tokensOut
80+
}
81+
7482
// If this is the last api request, use its tokens for context size
7583
if (message === lastApiReq) {
76-
// Only update context tokens if both input and output tokens are non-zero
77-
if (tokensIn > 0 && tokensOut > 0) {
78-
result.contextTokens = tokensIn + tokensOut
79-
}
84+
// Use the last valid context tokens if the current request doesn't have valid tokens
85+
result.contextTokens = tokensIn > 0 && tokensOut > 0 ? tokensIn + tokensOut : lastValidContextTokens
8086
}
8187
} catch (error) {
8288
console.error("Error parsing JSON:", error)

0 commit comments

Comments
 (0)