Skip to content

Commit 97fe13d

Browse files
committed
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.
1 parent e668169 commit 97fe13d

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/shared/getApiMetrics.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ export function getApiMetrics(messages: ClineMessage[]): ApiMetrics {
6565

6666
// If this is the last api request, use its tokens for context size
6767
if (message === lastApiReq) {
68-
result.contextTokens = (tokensIn || 0) + (tokensOut || 0)
68+
// Only update context tokens if both input and output tokens are non-zero
69+
if (tokensIn > 0 && tokensOut > 0) {
70+
result.contextTokens = tokensIn + tokensOut
71+
}
6972
}
7073
} catch (error) {
7174
console.error("Error parsing JSON:", error)

0 commit comments

Comments
 (0)