diff --git a/.changeset/fix-context-flickering.md b/.changeset/fix-context-flickering.md new file mode 100644 index 00000000000..8b6798cdd70 --- /dev/null +++ b/.changeset/fix-context-flickering.md @@ -0,0 +1,7 @@ +--- +"kilo-code": patch +--- + +fix: prevent context token indicator flickering + +Fixed a bug where the context token indicator would flicker to 0% when a new API request started. The issue occurred because the loop would break on placeholder messages without valid token data instead of continuing to search for the last complete message with token data. diff --git a/packages/core/src/message-utils/consolidateTokenUsage.ts b/packages/core/src/message-utils/consolidateTokenUsage.ts index 7f1a324b376..6f8ac9f2789 100644 --- a/packages/core/src/message-utils/consolidateTokenUsage.ts +++ b/packages/core/src/message-utils/consolidateTokenUsage.ts @@ -99,7 +99,6 @@ export function consolidateTokenUsage(messages: ClineMessage[]): TokenUsage { // with only apiProtocol (no token data). We need to skip these placeholders and // find the last message with actual token data to avoid showing 0% context. result.contextTokens = 0 - let foundValidTokenData = false for (let i = messages.length - 1; i >= 0; i--) { const message = messages[i] @@ -112,21 +111,14 @@ export function consolidateTokenUsage(messages: ClineMessage[]): TokenUsage { const hasTokenData = typeof tokensIn === "number" || typeof tokensOut === "number" if (hasTokenData) { - // Since tokensIn now stores TOTAL input tokens (including cache tokens), - // we no longer need to add cacheWrites and cacheReads separately. - // This applies to both Anthropic and OpenAI protocols. result.contextTokens = (tokensIn || 0) + (tokensOut || 0) - foundValidTokenData = true + break } } catch { - // Ignore JSON parse errors continue } } else if (message.type === "say" && message.say === "condense_context") { result.contextTokens = message.contextCondense?.newContextTokens ?? 0 - foundValidTokenData = true - } - if (foundValidTokenData) { break } }