Просмотр исходного кода

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.
MFPires 11 месяцев назад
Родитель
Сommit
97fe13dcb1
1 измененных файлов с 4 добавлено и 1 удалено
  1. 4 1
      src/shared/getApiMetrics.ts

+ 4 - 1
src/shared/getApiMetrics.ts

@@ -65,7 +65,10 @@ export function getApiMetrics(messages: ClineMessage[]): ApiMetrics {
 
 				// If this is the last api request, use its tokens for context size
 				if (message === lastApiReq) {
-					result.contextTokens = (tokensIn || 0) + (tokensOut || 0)
+					// Only update context tokens if both input and output tokens are non-zero
+					if (tokensIn > 0 && tokensOut > 0) {
+						result.contextTokens = tokensIn + tokensOut
+					}
 				}
 			} catch (error) {
 				console.error("Error parsing JSON:", error)