Skip to content

Conversation

@roomote
Copy link
Contributor

@roomote roomote bot commented Jul 21, 2025

Summary

This PR fixes the incorrect token counting in the context window display that was causing users to see unrealistic values like 0 or under 100 tokens.

Problem

The contextTokens calculation in getApiMetrics was only looking at the last API request's tokens instead of calculating the actual cumulative context window usage. This led to:

  • Incorrect token count display (showing 0 or very low numbers)
  • Context window exceeded alerts despite low displayed token count
  • Chat history condenser not working properly

Solution

Changed the contextTokens calculation to:

  1. Sum all tokens from all API requests in the conversation
  2. Properly handle condense_context messages by resetting to the new context size
  3. Continue accumulating tokens after condensation

Changes

  • Modified src/shared/getApiMetrics.ts to calculate cumulative context tokens
  • Updated tests in src/shared/__tests__/getApiMetrics.spec.ts to verify correct behavior

Testing

  • All existing tests pass
  • Added new tests to verify cumulative token calculation
  • Added tests for handling condense_context messages

Fixes #6004


Important

Fixes contextTokens calculation in getApiMetrics to sum all conversation tokens and handle condense_context messages correctly.

  • Behavior:
    • Fixes contextTokens calculation in getApiMetrics to sum tokens from all API requests.
    • Resets contextTokens after condense_context messages and continues accumulation.
  • Code Changes:
    • Modified getApiMetrics.ts to track cumulative context tokens and handle condense_context messages.
    • Updated tests in getApiMetrics.spec.ts to verify cumulative token calculation and condense_context handling.
  • Testing:
    • Added tests for cumulative token calculation and condense_context message handling.
    • Ensured all existing tests pass.

This description was created by Ellipsis for efcd3e0. You can customize this summary. It will automatically update as commits are pushed.

…kens

- Changed contextTokens calculation to sum all tokens in conversation history
- Properly handles condense_context messages by resetting to new context size
- Fixes issue where token counter showed unrealistic values like 0 or under 100
- Updates tests to verify correct cumulative token calculation

Fixes #6004
@roomote roomote bot requested review from cte, jr and mrubens as code owners July 21, 2025 12:27
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. bug Something isn't working labels Jul 21, 2025
@hannesrudolph hannesrudolph added the Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. label Jul 21, 2025
@daniel-lxs
Copy link
Member

Should be solved by #6019

@daniel-lxs daniel-lxs closed this Jul 21, 2025
@github-project-automation github-project-automation bot moved this from New to Done in Roo Code Roadmap Jul 21, 2025
@github-project-automation github-project-automation bot moved this from Triage to Done in Roo Code Roadmap Jul 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. size:L This PR changes 100-499 lines, ignoring generated files.

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Used context window tokens counter is working incorrectly

4 participants