The context token calculation has been updated to:
- Include cache tokens (cacheWrites + cacheReads) in the total context size
- Use a cleaner approach to find the last valid API request
- Fix issue where placeholder messages without token info were being counted
- Match upstream's implementation of getTotalTokensFromMessage
This fixes the issue where context size was incorrectly showing as 139 tokens
without any API requests being made.
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.
- 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.
- 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.
- Add contextTokens to ApiMetrics interface
- Calculate context size using last API request's tokens
- Display context token count in TaskHeader below total tokens
- Use exact token counts instead of character estimation
This helps users track the total size of their conversation context,
which is useful for managing context window limits.