Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: token counter #48

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft

Conversation

fmunteanu
Copy link

@fmunteanu fmunteanu commented Mar 29, 2025

Token Usage Tracking for Claude Desktop

This PR adds token usage tracking and display functionality to Claude Desktop, it follows PR submission guidelines highlighted into CLAUDE.md and closes #46.

Features

  • Tracks token usage across conversations
  • Displays token counter in the UI (using KB format)
  • Automatically continues conversations when Claude hits length limits (optional)
  • Dynamically obtains the user's actual max_tokens from Claude API

Files Added

  • src/wcgw/token_counter.py - Core token tracking functionality
  • src/wcgw/client/mcp_server/token_tracker.py - Integration with the core token counter
  • src/wcgw/client/mcp_server/token_handler.py - Handles token-related operations
  • src/wcgw/client/mcp_server/token_integration.py - Integration utilities for server
  • src/wcgw/ui/js/token_counter.js - UI components for token display
  • src/wcgw/ui/js/ui_integration.js - UI integration helpers
  • tests/test_token_counter.py - Unit tests for token counter

Usage

The token counter displays in the bottom right corner of the input area, showing:

[Current tokens KB] / [Max tokens KB] KB

For example: 1.2 / 200 KB

Benefits

  1. Usage Awareness: Users can see their token consumption in real-time
  2. Automatic Continuation: Optionally continues responses when Claude hits length limits
  3. Dynamic Limits: Adapts to the user's actual token limits from Claude's API
  4. KB Format: Displays token counts in KB format for a cleaner UI
  5. Non-invasive: Uses a decorator/wrapper approach that doesn't require modifying existing code

Testing

Unit tests are provided for the token counter module. Run them with:

python3 -m unittest tests/test_token_counter.py

@fmunteanu fmunteanu marked this pull request as draft March 29, 2025 17:04
@codecov-commenter
Copy link

codecov-commenter commented Mar 31, 2025

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

Attention: Patch coverage is 18.94273% with 184 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/wcgw/client/mcp_server/token_integration.py 0.00% 95 Missing ⚠️
src/wcgw/client/mcp_server/token_handler.py 0.00% 47 Missing ⚠️
src/wcgw/client/mcp_server/token_tracker.py 0.00% 40 Missing ⚠️
src/wcgw/token_counter.py 95.55% 2 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Files with missing lines Coverage Δ
src/wcgw/token_counter.py 95.55% <95.55%> (ø)
src/wcgw/client/mcp_server/token_tracker.py 0.00% <0.00%> (ø)
src/wcgw/client/mcp_server/token_handler.py 0.00% <0.00%> (ø)
src/wcgw/client/mcp_server/token_integration.py 0.00% <0.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@rusiaaman
Copy link
Owner

Thanks for your effort in the PR but it currently doesn't work.

You can verify by running your branch in claude


    "wcgw": {
      "command": "uvx",
      "args": [
        "--python",
        "3.12",
        "--from",
        "git+https://github.com/fmunteanu/wcgw@feat/token-counter",
        "wcgw_mcp"
      ]
    }

As discussed in the corresponding issue, I'm not on board with this feature due to severe limitations associated with MCP servers. This should be handled client side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature: Token reporting usage
3 participants