-
-
Notifications
You must be signed in to change notification settings - Fork 296
Fix: Token calculation logic and update token limits #67
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
Fix: Token calculation logic and update token limits #67
Conversation
WalkthroughA new terminal-based monitoring script Changes
Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (2)
✅ Files skipped from review due to trivial changes (1)
🚧 Files skipped from review as they are similar to previous changes (1)
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (1)
claude_monitor.py (1)
262-274: Improve code readability with max() function.The custom_max logic is correct and well-commented. However, the pylint suggestion is valid - using
max()would be more concise and readable.Apply this diff to improve readability:
- max_tokens = 0 - for block in blocks: - if not block.get("isGap", False) and not block.get("isActive", False): - tokens = block.get("totalTokens", 0) - if tokens > max_tokens: - max_tokens = tokens + # Find the highest token usage from completed sessions + max_tokens = max( + (block.get("totalTokens", 0) + for block in blocks + if not block.get("isGap", False) and not block.get("isActive", False)), + default=0 + )
📜 Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
claude_monitor.py(1 hunks)usage_analyzer/output/json_formatter.py(2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
usage_analyzer/output/json_formatter.py (1)
usage_analyzer/models/data_structures.py (1)
total_tokens(41-47)
🪛 Pylint (3.3.7)
claude_monitor.py
[refactor] 268-269: Consider using 'max_tokens = max(max_tokens, tokens)' instead of unnecessary if block
(R1731)
🔇 Additional comments (3)
usage_analyzer/output/json_formatter.py (2)
71-91: LGTM! Token calculation logic correctly simplified.The removal of the model-specific multipliers (especially the 5x multiplier for Opus tokens) aligns perfectly with the PR objectives to fix incorrect token calculation logic. The new implementation accurately sums only input and output tokens, which should provide more accurate token monitoring.
183-183: Minor formatting improvement.Good practice to ensure files end with a newline.
claude_monitor.py (1)
256-260: LGTM! Token limits updated to align with corrected calculation logic.The updated token limits are consistent with the simplified token calculation logic in
json_formatter.py. The removal of the artificial 5x multiplier for Opus tokens necessitated these adjusted limits, and the inline comments clearly explain the token counts per session.
|
@AlexLoar Thanks for your help! Please check 3.0.3 release |
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #67 +/- ##
=======================================
Coverage 71.01% 71.01%
=======================================
Files 39 39
Lines 3171 3171
Branches 462 462
=======================================
Hits 2252 2252
Misses 807 807
Partials 112 112 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
0044e86 to
376cd12
Compare
…ude-Code-Usage-Monitor into fix/token-calculation-logic
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 7
🧹 Nitpick comments (1)
usage_analyzer/output/json_formatter.py (1)
119-121: Remove or address the TODO commentThe TODO comment suggests this is for debugging purposes. Either remove it if no longer needed or properly implement the debug functionality.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
claude_monitor.py(1 hunks)usage_analyzer/output/json_formatter.py(1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
usage_analyzer/output/json_formatter.py (3)
src/claude_monitor/core/models.py (4)
SessionBlock(72-109)total_tokens(44-51)total_tokens(93-95)total_cost(98-100)src/claude_monitor/core/calculations.py (3)
BurnRateCalculator(31-91)calculate_burn_rate(34-57)project_block_usage(59-91)src/claude_monitor/terminal/themes.py (2)
print_themed(679-687)get_themed_console(665-676)
🪛 GitHub Check: Lint with Ruff (3.10)
claude_monitor.py
[failure] 109-109: Ruff (W293)
claude_monitor.py:109:1: W293 Blank line contains whitespace
[failure] 104-104: Ruff (W293)
claude_monitor.py:104:1: W293 Blank line contains whitespace
[failure] 103-103: Ruff (F841)
claude_monitor.py:103:5: F841 Local variable console is assigned to but never used
[failure] 96-96: Ruff (W293)
claude_monitor.py:96:1: W293 Blank line contains whitespace
[failure] 77-77: Ruff (W293)
claude_monitor.py:77:1: W293 Blank line contains whitespace
[failure] 32-32: Ruff (W293)
claude_monitor.py:32:1: W293 Blank line contains whitespace
[failure] 25-25: Ruff (W291)
claude_monitor.py:25:65: W291 Trailing whitespace
[failure] 5-5: Ruff (F401)
claude_monitor.py:5:8: F401 subprocess imported but unused
[failure] 4-4: Ruff (F401)
claude_monitor.py:4:8: F401 json imported but unused
[failure] 3-14: Ruff (I001)
claude_monitor.py:3:1: I001 Import block is un-sorted or un-formatted
🪛 GitHub Check: Lint with Ruff (3.11)
claude_monitor.py
[failure] 109-109: Ruff (W293)
claude_monitor.py:109:1: W293 Blank line contains whitespace
[failure] 104-104: Ruff (W293)
claude_monitor.py:104:1: W293 Blank line contains whitespace
[failure] 103-103: Ruff (F841)
claude_monitor.py:103:5: F841 Local variable console is assigned to but never used
[failure] 96-96: Ruff (W293)
claude_monitor.py:96:1: W293 Blank line contains whitespace
[failure] 77-77: Ruff (W293)
claude_monitor.py:77:1: W293 Blank line contains whitespace
[failure] 32-32: Ruff (W293)
claude_monitor.py:32:1: W293 Blank line contains whitespace
[failure] 25-25: Ruff (W291)
claude_monitor.py:25:65: W291 Trailing whitespace
[failure] 5-5: Ruff (F401)
claude_monitor.py:5:8: F401 subprocess imported but unused
[failure] 4-4: Ruff (F401)
claude_monitor.py:4:8: F401 json imported but unused
[failure] 3-14: Ruff (I001)
claude_monitor.py:3:1: I001 Import block is un-sorted or un-formatted
🪛 GitHub Check: Lint with Ruff (3.12)
claude_monitor.py
[failure] 109-109: Ruff (W293)
claude_monitor.py:109:1: W293 Blank line contains whitespace
[failure] 104-104: Ruff (W293)
claude_monitor.py:104:1: W293 Blank line contains whitespace
[failure] 103-103: Ruff (F841)
claude_monitor.py:103:5: F841 Local variable console is assigned to but never used
[failure] 96-96: Ruff (W293)
claude_monitor.py:96:1: W293 Blank line contains whitespace
[failure] 77-77: Ruff (W293)
claude_monitor.py:77:1: W293 Blank line contains whitespace
[failure] 32-32: Ruff (W293)
claude_monitor.py:32:1: W293 Blank line contains whitespace
[failure] 25-25: Ruff (W291)
claude_monitor.py:25:65: W291 Trailing whitespace
[failure] 5-5: Ruff (F401)
claude_monitor.py:5:8: F401 subprocess imported but unused
[failure] 4-4: Ruff (F401)
claude_monitor.py:4:8: F401 json imported but unused
[failure] 3-14: Ruff (I001)
claude_monitor.py:3:1: I001 Import block is un-sorted or un-formatted
🪛 GitHub Actions: Lint
claude_monitor.py
[error] 3-14: Ruff: Import block is un-sorted or un-formatted (I001). Organize imports.
[error] 4-4: Ruff: json imported but unused (F401). Remove unused import.
[error] 5-5: Ruff: subprocess imported but unused (F401). Remove unused import.
[error] 25-25: Ruff: Trailing whitespace (W291). Remove trailing whitespace.
[error] 32-32: Ruff: Blank line contains whitespace (W293). Remove whitespace from blank line.
[error] 77-77: Ruff: Blank line contains whitespace (W293). Remove whitespace from blank line.
[error] 96-96: Ruff: Blank line contains whitespace (W293). Remove whitespace from blank line.
[error] 103-103: Ruff: Local variable console is assigned to but never used (F841). Remove assignment to unused variable.
[error] 104-104: Ruff: Blank line contains whitespace (W293). Remove whitespace from blank line.
[error] 109-109: Ruff: Blank line contains whitespace (W293). Remove whitespace from blank line.
[error] 326-326: Ruff: Blank line contains whitespace (W293). Remove whitespace from blank line.
[error] 334-334: Ruff: Blank line contains whitespace (W293). Remove whitespace from blank line.
[error] 340-340: Ruff: Blank line contains whitespace (W293). Remove whitespace from blank line.
[error] 442-442: Ruff: Blank line contains whitespace (W293). Remove whitespace from blank line.
[error] 469-469: Ruff: Blank line contains whitespace (W293). Remove whitespace from blank line.
[error] 484-484: Ruff: Blank line contains whitespace (W293). Remove whitespace from blank line.
[error] 524-524: Ruff: Blank line contains whitespace (W293). Remove whitespace from blank line.
[error] 579-579: Ruff: f-string without any placeholders (F541). Remove extraneous f prefix.
usage_analyzer/output/json_formatter.py
[error] 8-14: Ruff: Import block is un-sorted or un-formatted (I001). Organize imports.
[error] 9-9: Ruff: typing.Any imported but unused (F401). Remove unused import.
[error] 9-9: Ruff: typing.Dict imported but unused (F401). Remove unused import.
[error] 24-24: Ruff: Blank line contains whitespace (W293). Remove whitespace from blank line.
[error] 27-27: Ruff: Local variable console is assigned to but never used (F841). Remove assignment to unused variable.
[error] 28-28: Ruff: Blank line contains whitespace (W293). Remove whitespace from blank line.
[error] 32-32: Ruff: Blank line contains whitespace (W293). Remove whitespace from blank line.
[error] 35-35: Ruff: Blank line contains whitespace (W293). Remove whitespace from blank line.
[error] 39-39: Ruff: Blank line contains whitespace (W293). Remove whitespace from blank line.
[error] 44-44: Ruff: Blank line contains whitespace (W293). Remove whitespace from blank line.
[error] 52-52: Ruff: Blank line contains whitespace (W293). Remove whitespace from blank line.
[error] 56-56: Ruff: Trailing whitespace (W291). Remove trailing whitespace.
[error] 59-59: Ruff: Blank line contains whitespace (W293). Remove whitespace from blank line.
[error] 69-69: Ruff: Import block is un-sorted or un-formatted (I001). Organize imports.
[error] 69-69: Ruff: Redefinition of unused Dict from line 9 (F811). Remove redefinition.
[error] 69-69: Ruff: Redefinition of unused Any from line 9 (F811). Remove redefinition.
[error] 154-154: Ruff: Blank line contains whitespace (W293). Remove whitespace from blank line.
[error] 167-167: Ruff: Blank line contains whitespace (W293). Remove whitespace from blank line.
[error] 180-180: Ruff: Blank line contains whitespace (W293). Remove whitespace from blank line.
🔇 Additional comments (3)
usage_analyzer/output/json_formatter.py (1)
45-61: Well-implemented cost calculation with appropriate stylingThe method correctly recalculates per-model costs and applies sensible styling thresholds based on the total cost.
claude_monitor.py (2)
255-276: Token limits correctly updated with improved custom_max logicThe function properly implements the updated token limits mentioned in the PR objectives:
- Pro: 45K tokens
- Max5: 225K tokens
- Max20: 900K tokens
The custom_max implementation using a generator expression is more efficient than previous approaches.
145-216: Excellent implementation of hourly burn rate calculationThe function correctly handles complex scenarios:
- Sessions overlapping with the last hour window
- Proportional token allocation based on time overlap
- Proper timezone handling with UTC conversions
- Active vs completed session logic
This PR fixes the incorrect token calculation logic in the Claude Code Usage Monitor. The current implementation arbitrarily multiplies Opus tokens by 5, which doesn't reflect actual Claude token limits or usage patterns.
Summary by CodeRabbit