Skip to content

Conversation

@tifoji
Copy link
Contributor

@tifoji tifoji commented Jun 25, 2025

Summary

  • Fixes live usage monitor and blocks command to include all four token types (input, output, cache_creation_input, cache_read_input)
  • Adds getTotalTokens() helper function to correctly sum all token types
  • Updates burn rate and projection calculations to include cache tokens

Test plan

  • Build the project with bun run build
  • Run ./dist/index.js blocks --live to see live monitoring with accurate token counts
  • Verify cache tokens are included in the total (should show values >9M tokens if cache tokens are present)
  • Run unit tests with bun run test to verify calculations include cache tokens

Fixes #179

Summary by CodeRabbit

  • Improvements
    • Enhanced the accuracy of total token count displayed in live rendering and token usage reports.
    • Improved alignment of session progress, usage, projection bars, and model information for better visual consistency.
    • Centralized token counting logic for consistent and reliable token usage calculations across the app.

Previously, the live usage monitor and blocks command only counted input and output tokens, excluding cache_creation_input_tokens and cache_read_input_tokens from the total. This caused inaccurate token usage reporting, especially when cache creation tokens (which cost more than input tokens) were significant.

Changes:
- Add getTotalTokens() helper function to sum all four token types
- Update live rendering display to use getTotalTokens() for accurate counts
- Fix burn rate calculation to include cache tokens in tokens/minute metric
- Fix usage projection to include cache tokens in projected totals
- Update blocks command to count cache tokens in all calculations
- Add unit tests verifying cache tokens are included in calculations
- Fix UI alignment issues in live display (SESSION and Models lines)

This ensures users see accurate total token usage that matches their actual costs.

Fixes ryoppippi#179
@coderabbitai
Copy link

coderabbitai bot commented Jun 25, 2025

Caution

Review failed

The pull request is closed.

"""

Walkthrough

A helper function getTotalTokens was added to calculate total token usage by summing input, output, cache creation, and cache read tokens. The live rendering logic now uses this function for token totals. Additionally, padding offsets for string alignment in the live display were adjusted to improve layout consistency. Token counting logic in commands, session blocks, and cost calculation was refactored to use the shared utility.

Changes

File(s) Change Summary
src/_live-rendering.ts Added getTotalTokens helper; updated token total calculation in renderLiveDisplay; adjusted string padding offsets
src/_token-utils.ts Added new module with types for token counts and getTotalTokens function handling both raw and aggregated token counts; included unit tests
src/calculate-cost.ts Removed local token counting; aliased TokenData to shared type; re-exported getTotalTokens from shared module
src/commands/blocks.ts, src/_session-blocks.ts Replaced manual token summations with calls to getTotalTokens in multiple token usage calculations

Sequence Diagram(s)

sequenceDiagram
    participant renderLiveDisplay
    participant getTotalTokens

    renderLiveDisplay->>getTotalTokens: Call with tokenCounts
    getTotalTokens-->>renderLiveDisplay: Return sum of all token types
    renderLiveDisplay->>renderLiveDisplay: Use totalTokens for display
Loading

Assessment against linked issues

Objective Addressed Explanation
Live usage token count includes cache create and cache read tokens in addition to input/output (#179)

Poem

A bunny hopped through code so neat,
Summing tokens—none obsolete!
Input, output, cache create, cache read,
Now the live count fills every need.
With padding aligned, the box looks right—
This rabbit’s code is out of sight!
🐇✨
"""


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d428bc7 and 105a10f.

📒 Files selected for processing (4)
  • src/_live-rendering.ts (10 hunks)
  • src/_token-utils.ts (1 hunks)
  • src/calculate-cost.ts (3 hunks)
  • src/commands/blocks.ts (5 hunks)
✨ Finishing Touches
  • 📝 Generate Docstrings

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.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need 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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary of Changes

Hello @tifoji, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a critical issue in the live usage monitor by ensuring that all token types, including cache-related tokens, are correctly accounted for in total token calculations. By introducing a dedicated helper function and integrating it into the live display, the PR significantly improves the accuracy of burn rate and projection metrics, providing users with a more complete understanding of their token usage. Additionally, minor UI refinements enhance the display's readability.

Highlights

  • Token Calculation Accuracy: Introduced a new helper function, getTotalTokens, to accurately sum all four token types: input, output, cache creation input, and cache read input. This ensures that the live usage monitor provides a comprehensive view of token consumption.
  • Live Display Integration: Updated the renderLiveDisplay function to utilize the new getTotalTokens helper. This change directly impacts the live burn rate and projection calculations, making them more precise by including previously omitted cache tokens.
  • UI Formatting Adjustments: Made minor adjustments to padding calculations for the session bar and models line within the live display, improving the visual alignment and presentation of information.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request includes cache tokens in live usage token count calculations, updating the live usage monitor and blocks command to include all four token types (input, output, cache_creation_input, cache_read_input). A getTotalTokens() helper function is added to sum all token types, and burn rate and projection calculations are updated to include cache tokens.

Comment on lines 38 to 45
function getTotalTokens(tokenCounts: SessionBlock['tokenCounts']): number {
return (
tokenCounts.inputTokens
+ tokenCounts.outputTokens
+ tokenCounts.cacheCreationInputTokens
+ tokenCounts.cacheReadInputTokens
);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This getTotalTokens function duplicates the exported function in src/calculate-cost.ts. To avoid code duplication, import and reuse the existing function instead.

import { getTotalTokens } from './calculate-cost.ts';

Copy link

@coderabbitai coderabbitai bot left a 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)
src/_live-rendering.ts (1)

38-44: Consider potential code duplication with existing getTotalTokens function.

A similar function exists in src/calculate-cost.ts with slightly different property names (cacheCreationTokens vs cacheCreationInputTokens, cacheReadTokens vs cacheReadInputTokens). While the different property names suggest they handle different data structures, consider if there's an opportunity to consolidate or ensure naming consistency.

Let me check if these functions could be unified or if the naming differences are intentional:

#!/bin/bash
# Compare the two getTotalTokens functions and their usage patterns
echo "=== Function in calculate-cost.ts ==="
ast-grep --pattern 'function getTotalTokens($$$) { $$$ }'

echo "=== TokenCounts type definition ==="
ast-grep --pattern 'type TokenCounts = $$$'

echo "=== TokenData type definition ==="  
ast-grep --pattern 'type TokenData = $$$'
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d56374f and 3499b08.

📒 Files selected for processing (1)
  • src/_live-rendering.ts (4 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
`**/*.ts`: Use .ts extensions for local file imports (e.g., import { foo } from './utils.ts'). No console.log allowed except where explicitly disabled with eslint-disable; use logg...

**/*.ts: Use .ts extensions for local file imports (e.g., import { foo } from './utils.ts').
No console.log allowed except where explicitly disabled with eslint-disable; use logger.ts instead.
Tests are written directly in the same files as the source code, not in separate test files. Use in-source testing pattern with if (import.meta.vitest != null) blocks.
Dynamic imports using await import() should only be used within test blocks to avoid tree-shaking issues.
Only export constants, functions, and types that are actually used by other modules. Internal/private constants that are only used within the same file should NOT be exported.
Variables should start with lowercase (camelCase), types with uppercase (PascalCase), and constants can use UPPER_SNAKE_CASE.
After making any code changes, always run bun run format, bun typecheck, and bun run test to ensure code quality.
File paths must always use Node.js path utilities for cross-platform compatibility.
Dependencies should always be added as devDependencies unless explicitly requested otherwise.

  • src/_live-rendering.ts
`**/_*.ts`: Internal files should use underscore prefix (e.g., _types.ts, _utils.ts, _consts.ts).

**/_*.ts: Internal files should use underscore prefix (e.g., _types.ts, _utils.ts, _consts.ts).

  • src/_live-rendering.ts
🧬 Code Graph Analysis (1)
src/_live-rendering.ts (2)
src/calculate-cost.ts (1)
  • getTotalTokens (76-83)
src/_session-blocks.ts (1)
  • SessionBlock (49-60)
🪛 ESLint
src/_live-rendering.ts

[error] 161-161: Unsafe call of a(n) error type typed value.

(ts/no-unsafe-call)


[error] 347-347: Unsafe call of a(n) error type typed value.

(ts/no-unsafe-call)

🔇 Additional comments (3)
src/_live-rendering.ts (3)

33-45: LGTM! Helper function correctly implements cache token inclusion.

The function properly sums all four token types as specified in the PR objectives, ensuring cache tokens are included in live usage calculations.


110-110: Excellent update to use the new helper function.

This change ensures the live display now includes cache tokens in the total count, addressing the core issue described in PR #179.


161-161: Static analysis errors appear to be false positives.

The ESLint errors about "unsafe call of error type" seem incorrect here, as both sessionBarStr and modelsLine are constructed from string concatenations and should be string types. The padding adjustments from offset 3 to 2 appear to be legitimate layout improvements.

Let me verify the string types to confirm these are false positives:

#!/bin/bash
# Check the type definitions and usage patterns for the variables flagged by ESLint
ast-grep --pattern 'const sessionBarStr = $$$'
ast-grep --pattern 'const modelsLine = $$$'

Also applies to: 347-347

…e duplication

- Create _token-utils.ts with shared getTotalTokens function
- Support both raw usage data format (cacheCreationInputTokens) and aggregated format (cacheCreationTokens)
- Remove duplicate implementations from 4 files
- Maintain backward compatibility with re-export in calculate-cost.ts
- Add comprehensive tests for both token formats

Addresses code review feedback about duplication.
Copy link

@coderabbitai coderabbitai bot left a 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 (2)
src/_token-utils.ts (2)

40-56: Improve type safety in the function implementation.

The function logic is correct, but the type casting on lines 44 and 48 can be improved for better type safety.

Apply this diff to improve type safety:

-	const cacheCreation = 'cacheCreationInputTokens' in tokenCounts
-		? tokenCounts.cacheCreationInputTokens
-		: (tokenCounts).cacheCreationTokens;
+	const cacheCreation = 'cacheCreationInputTokens' in tokenCounts
+		? tokenCounts.cacheCreationInputTokens
+		: (tokenCounts as AggregatedTokenCounts).cacheCreationTokens;

-	const cacheRead = 'cacheReadInputTokens' in tokenCounts
-		? tokenCounts.cacheReadInputTokens
-		: (tokenCounts).cacheReadTokens;
+	const cacheRead = 'cacheReadInputTokens' in tokenCounts
+		? tokenCounts.cacheReadInputTokens
+		: (tokenCounts as AggregatedTokenCounts).cacheReadTokens;

58-121: Comprehensive test coverage with minor styling issue.

The test suite provides excellent coverage for both token formats and edge cases. The vitest typing errors in static analysis are likely false positives due to type configuration.

Add a newline at the end of the file to satisfy the ESLint rule:

	});
}
+
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3499b08 and 5dce915.

📒 Files selected for processing (1)
  • src/_token-utils.ts (1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
`**/*.ts`: Use .ts extensions for local file imports (e.g., import { foo } from './utils.ts'). No console.log allowed except where explicitly disabled with eslint-disable; use logg...

**/*.ts: Use .ts extensions for local file imports (e.g., import { foo } from './utils.ts').
No console.log allowed except where explicitly disabled with eslint-disable; use logger.ts instead.
Tests are written directly in the same files as the source code, not in separate test files. Use in-source testing pattern with if (import.meta.vitest != null) blocks.
Dynamic imports using await import() should only be used within test blocks to avoid tree-shaking issues.
Only export constants, functions, and types that are actually used by other modules. Internal/private constants that are only used within the same file should NOT be exported.
Variables should start with lowercase (camelCase), types with uppercase (PascalCase), and constants can use UPPER_SNAKE_CASE.
After making any code changes, always run bun run format, bun typecheck, and bun run test to ensure code quality.
File paths must always use Node.js path utilities for cross-platform compatibility.
Dependencies should always be added as devDependencies unless explicitly requested otherwise.

  • src/_token-utils.ts
`**/_*.ts`: Internal files should use underscore prefix (e.g., _types.ts, _utils.ts, _consts.ts).

**/_*.ts: Internal files should use underscore prefix (e.g., _types.ts, _utils.ts, _consts.ts).

  • src/_token-utils.ts
🪛 ESLint
src/_token-utils.ts

[error] 60-60: Unsafe call of a(n) error type typed value.

(ts/no-unsafe-call)


[error] 61-61: Unsafe call of a(n) error type typed value.

(ts/no-unsafe-call)


[error] 68-68: Unsafe call of a(n) error type typed value.

(ts/no-unsafe-call)


[error] 68-68: Unsafe call of a(n) error type typed value.

(ts/no-unsafe-call)


[error] 68-68: Unsafe member access .toBe on an error typed value.

(ts/no-unsafe-member-access)


[error] 71-71: Unsafe call of a(n) error type typed value.

(ts/no-unsafe-call)


[error] 78-78: Unsafe call of a(n) error type typed value.

(ts/no-unsafe-call)


[error] 78-78: Unsafe call of a(n) error type typed value.

(ts/no-unsafe-call)


[error] 78-78: Unsafe member access .toBe on an error typed value.

(ts/no-unsafe-member-access)


[error] 81-81: Unsafe call of a(n) error type typed value.

(ts/no-unsafe-call)


[error] 88-88: Unsafe call of a(n) error type typed value.

(ts/no-unsafe-call)


[error] 88-88: Unsafe call of a(n) error type typed value.

(ts/no-unsafe-call)


[error] 88-88: Unsafe member access .toBe on an error typed value.

(ts/no-unsafe-member-access)


[error] 91-91: Unsafe call of a(n) error type typed value.

(ts/no-unsafe-call)


[error] 98-98: Unsafe call of a(n) error type typed value.

(ts/no-unsafe-call)


[error] 98-98: Unsafe call of a(n) error type typed value.

(ts/no-unsafe-call)


[error] 98-98: Unsafe member access .toBe on an error typed value.

(ts/no-unsafe-member-access)


[error] 101-101: Unsafe call of a(n) error type typed value.

(ts/no-unsafe-call)


[error] 108-108: Unsafe call of a(n) error type typed value.

(ts/no-unsafe-call)


[error] 108-108: Unsafe call of a(n) error type typed value.

(ts/no-unsafe-call)


[error] 108-108: Unsafe member access .toBe on an error typed value.

(ts/no-unsafe-member-access)


[error] 111-111: Unsafe call of a(n) error type typed value.

(ts/no-unsafe-call)


[error] 118-118: Unsafe call of a(n) error type typed value.

(ts/no-unsafe-call)


[error] 118-118: Unsafe call of a(n) error type typed value.

(ts/no-unsafe-call)


[error] 118-118: Unsafe member access .toBe on an error typed value.

(ts/no-unsafe-member-access)


[error] 121-121: Newline required at end of file but not found.

(style/eol-last)

🔇 Additional comments (2)
src/_token-utils.ts (2)

1-7: Excellent documentation and file structure.

The file header provides clear context about the module's purpose and usage throughout the application. Good adherence to internal file naming convention with underscore prefix.


9-32: Well-designed type system for token counting.

The type definitions effectively handle both naming conventions used across the application. The union type AnyTokenCounts provides flexibility while maintaining type safety.

tifoji added 3 commits June 24, 2025 22:46
- Import getTotalTokens from shared _token-utils.ts
- Remove duplicate helper function
- Maintain consistent token counting across commands
- Import getTotalTokens from shared _token-utils.ts
- Add re-export for backward compatibility
- Update TokenData type alias to use AggregatedTokenCounts
- Maintain existing public API while eliminating duplication
- Import getTotalTokens from shared _token-utils.ts
- Remove duplicate helper function
- Fix alignment issues by using consistent token counting
- Maintain consistent live display behavior
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 6

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5dce915 and 98f2d5a.

📒 Files selected for processing (3)
  • src/_live-rendering.ts (12 hunks)
  • src/calculate-cost.ts (4 hunks)
  • src/commands/blocks.ts (8 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
`**/*.ts`: Use .ts extensions for local file imports (e.g., import { foo } from './utils.ts'). No console.log allowed except where explicitly disabled with eslint-disable; use logg...

**/*.ts: Use .ts extensions for local file imports (e.g., import { foo } from './utils.ts').
No console.log allowed except where explicitly disabled with eslint-disable; use logger.ts instead.
Tests are written directly in the same files as the source code, not in separate test files. Use in-source testing pattern with if (import.meta.vitest != null) blocks.
Dynamic imports using await import() should only be used within test blocks to avoid tree-shaking issues.
Only export constants, functions, and types that are actually used by other modules. Internal/private constants that are only used within the same file should NOT be exported.
Variables should start with lowercase (camelCase), types with uppercase (PascalCase), and constants can use UPPER_SNAKE_CASE.
After making any code changes, always run bun run format, bun typecheck, and bun run test to ensure code quality.
File paths must always use Node.js path utilities for cross-platform compatibility.
Dependencies should always be added as devDependencies unless explicitly requested otherwise.

  • src/calculate-cost.ts
  • src/_live-rendering.ts
  • src/commands/blocks.ts
`**/_*.ts`: Internal files should use underscore prefix (e.g., _types.ts, _utils.ts, _consts.ts).

**/_*.ts: Internal files should use underscore prefix (e.g., _types.ts, _utils.ts, _consts.ts).

  • src/_live-rendering.ts
🧬 Code Graph Analysis (2)
src/_live-rendering.ts (1)
src/_utils.ts (2)
  • formatNumber (293-295)
  • formatCurrency (302-304)
src/commands/blocks.ts (2)
src/calculate-cost.ts (1)
  • getTotalTokens (70-70)
src/_consts.ts (1)
  • BLOCKS_WARNING_THRESHOLD (20-20)
🪛 ESLint
src/calculate-cost.ts

[error] 211-211: Newline required at end of file but not found.

(style/eol-last)

src/_live-rendering.ts

[error] 148-148: Unsafe call of a(n) error type typed value.

(ts/no-unsafe-call)


[error] 162-162: Unsafe call of a(n) error type typed value.

(ts/no-unsafe-call)


[error] 185-185: Expected indentation of 4 tabs but found 3.

(style/indent)


[error] 186-186: Expected indentation of 4 tabs but found 3.

(style/indent)


[error] 187-187: Expected indentation of 4 tabs but found 3.

(style/indent)


[error] 188-188: Expected indentation of 4 tabs but found 3.

(style/indent)


[error] 189-189: Expected indentation of 5 tabs but found 4.

(style/indent)


[error] 190-190: Expected indentation of 5 tabs but found 4.

(style/indent)


[error] 190-190: Unsafe assignment of an error typed value.

(ts/no-unsafe-assignment)


[error] 190-190: Unsafe call of a(n) error type typed value.

(ts/no-unsafe-call)


[error] 191-191: Expected indentation of 5 tabs but found 4.

(style/indent)


[error] 191-191: Unsafe assignment of an error typed value.

(ts/no-unsafe-assignment)


[error] 191-191: Unsafe call of a(n) error type typed value.

(ts/no-unsafe-call)


[error] 191-191: Unsafe member access .gray on an error typed value.

(ts/no-unsafe-member-access)


[error] 192-192: Expected indentation of 5 tabs but found 4.

(style/indent)


[error] 193-193: Expected indentation of 5 tabs but found 4.

(style/indent)


[error] 194-194: Expected indentation of 4 tabs but found 3.

(style/indent)


[error] 195-195: Expected indentation of 3 tabs but found 2.

(style/indent)


[error] 216-216: Expected indentation of 4 tabs but found 3.

(style/indent)


[error] 217-217: Expected indentation of 4 tabs but found 3.

(style/indent)


[error] 217-217: Unsafe call of a(n) error type typed value.

(ts/no-unsafe-call)


[error] 217-217: Unsafe member access .gray on an error typed value.

(ts/no-unsafe-member-access)


[error] 218-218: Expected indentation of 4 tabs but found 3.

(style/indent)


[error] 218-218: Unsafe call of a(n) error type typed value.

(ts/no-unsafe-call)


[error] 218-218: Unsafe member access .gray on an error typed value.

(ts/no-unsafe-member-access)


[error] 219-219: Expected indentation of 4 tabs but found 3.

(style/indent)


[error] 219-219: Unsafe call of a(n) error type typed value.

(ts/no-unsafe-call)


[error] 219-219: Unsafe member access .gray on an error typed value.

(ts/no-unsafe-member-access)


[error] 220-220: Expected indentation of 3 tabs but found 2.

(style/indent)


[error] 222-222: Expected indentation of 4 tabs but found 3.

(style/indent)


[error] 223-223: Expected indentation of 4 tabs but found 3.

(style/indent)


[error] 223-223: Unsafe call of a(n) error type typed value.

(ts/no-unsafe-call)


[error] 223-223: Unsafe member access .gray on an error typed value.

(ts/no-unsafe-member-access)


[error] 224-224: Expected indentation of 4 tabs but found 3.

(style/indent)


[error] 225-225: Expected indentation of 4 tabs but found 3.

(style/indent)


[error] 225-225: Unsafe call of a(n) error type typed value.

(ts/no-unsafe-call)


[error] 225-225: Unsafe member access .gray on an error typed value.

(ts/no-unsafe-member-access)


[error] 226-226: Expected indentation of 3 tabs but found 2.

(style/indent)


[error] 229-229: Unsafe call of a(n) error type typed value.

(ts/no-unsafe-call)


[error] 238-238: Unsafe call of a(n) error type typed value.

(ts/no-unsafe-call)


[error] 264-264: Expected indentation of 5 tabs but found 4.

(style/indent)


[error] 265-265: Expected indentation of 5 tabs but found 4.

(style/indent)


[error] 266-266: Expected indentation of 5 tabs but found 4.

(style/indent)


[error] 267-267: Expected indentation of 5 tabs but found 4.

(style/indent)


[error] 268-268: Expected indentation of 6 tabs but found 5.

(style/indent)


[error] 269-269: Expected indentation of 6 tabs but found 5.

(style/indent)


[error] 269-269: Unsafe assignment of an error typed value.

(ts/no-unsafe-assignment)


[error] 269-269: Unsafe call of a(n) error type typed value.

(ts/no-unsafe-call)


[error] 270-270: Expected indentation of 6 tabs but found 5.

(style/indent)


[error] 270-270: Unsafe assignment of an error typed value.

(ts/no-unsafe-assignment)


[error] 270-270: Unsafe call of a(n) error type typed value.

(ts/no-unsafe-call)


[error] 270-270: Unsafe member access .gray on an error typed value.

(ts/no-unsafe-member-access)


[error] 271-271: Expected indentation of 6 tabs but found 5.

(style/indent)


[error] 272-272: Expected indentation of 6 tabs but found 5.

(style/indent)


[error] 273-273: Expected indentation of 5 tabs but found 4.

(style/indent)


[error] 274-274: Expected indentation of 4 tabs but found 3.

(style/indent)


[error] 275-275: Unsafe call of a(n) error type typed value.

(ts/no-unsafe-call)


[error] 275-275: Unsafe member access .green on an error typed value.

(ts/no-unsafe-member-access)


[error] 275-275: Unsafe call of a(n) error type typed value.

(ts/no-unsafe-call)


[error] 275-275: Unsafe member access .gray on an error typed value.

(ts/no-unsafe-member-access)


[error] 277-283: Unsafe assignment of an error typed value.

(ts/no-unsafe-assignment)


[error] 279-279: Expected indentation of 5 tabs but found 4.

(style/indent)


[error] 279-279: Unsafe call of a(n) error type typed value.

(ts/no-unsafe-call)


[error] 279-279: Unsafe member access .red on an error typed value.

(ts/no-unsafe-member-access)


[error] 280-280: Expected indentation of 5 tabs but found 4.

(style/indent)


[error] 281-281: Expected indentation of 6 tabs but found 5.

(style/indent)


[error] 281-281: Unsafe call of a(n) error type typed value.

(ts/no-unsafe-call)


[error] 281-281: Unsafe member access .yellow on an error typed value.

(ts/no-unsafe-member-access)


[error] 282-282: Expected indentation of 6 tabs but found 5.

(style/indent)


[error] 282-282: Unsafe call of a(n) error type typed value.

(ts/no-unsafe-call)


[error] 282-282: Unsafe member access .green on an error typed value.

(ts/no-unsafe-member-access)


[error] 283-283: Unsafe call of a(n) error type typed value.

(ts/no-unsafe-call)


[error] 283-283: Unsafe member access .green on an error typed value.

(ts/no-unsafe-member-access)


[error] 286-286: Unsafe assignment of an error typed value.

(ts/no-unsafe-assignment)


[error] 286-286: Unsafe call of a(n) error type typed value.

(ts/no-unsafe-call)


[error] 286-286: Unsafe member access .bold on an error typed value.

(ts/no-unsafe-member-access)


[error] 287-287: Unsafe assignment of an error typed value.

(ts/no-unsafe-assignment)


[error] 287-287: Unsafe call of a(n) error type typed value.

(ts/no-unsafe-call)


[error] 290-290: Unsafe call of a(n) error type typed value.

(ts/no-unsafe-call)


[error] 304-304: Unsafe call of a(n) error type typed value.

(ts/no-unsafe-call)


[error] 309-309: Unsafe call of a(n) error type typed value.

(ts/no-unsafe-call)


[error] 323-323: Unsafe call of a(n) error type typed value.

(ts/no-unsafe-call)


[error] 334-334: Unsafe call of a(n) error type typed value.

(ts/no-unsafe-call)


[error] 423-423: Newline required at end of file but not found.

(style/eol-last)

src/commands/blocks.ts

[error] 29-29: Expected indentation of 4 tabs but found 3.

(style/indent)


[error] 30-30: Expected indentation of 4 tabs but found 3.

(style/indent)


[error] 31-31: Expected indentation of 4 tabs but found 3.

(style/indent)


[error] 32-32: Expected indentation of 4 tabs but found 3.

(style/indent)


[error] 33-33: Expected indentation of 3 tabs but found 2.

(style/indent)


[error] 39-39: Expected indentation of 5 tabs but found 4.

(style/indent)


[error] 40-40: Expected indentation of 5 tabs but found 4.

(style/indent)


[error] 41-41: Expected indentation of 4 tabs but found 3.

(style/indent)


[error] 278-278: Expected indentation of 9 tabs but found 8.

(style/indent)


[error] 278-278: Unsafe argument of type any assigned to a parameter of type string | undefined.

(ts/no-unsafe-argument)


[error] 278-278: Unsafe member access .values on an any value.

(ts/no-unsafe-member-access)


[error] 279-279: Expected indentation of 9 tabs but found 8.

(style/indent)


[error] 280-280: Expected indentation of 10 tabs but found 9.

(style/indent)


[error] 281-281: Expected indentation of 12 tabs but found 10.

(style/indent)


[error] 282-282: Expected indentation of 12 tabs but found 10.

(style/indent)


[error] 283-283: Expected indentation of 12 tabs but found 10.

(style/indent)


[error] 284-284: Expected indentation of 12 tabs but found 10.

(style/indent)


[error] 285-285: Expected indentation of 13 tabs but found 11.

(style/indent)


[error] 286-286: Expected indentation of 13 tabs but found 11.

(style/indent)


[error] 287-287: Expected indentation of 11 tabs but found 9.

(style/indent)


[error] 288-288: Expected indentation of 10 tabs but found 9.

(style/indent)


[error] 289-289: Expected indentation of 8 tabs but found 7.

(style/indent)


[error] 109-486: Unsafe assignment of an error typed value.

(ts/no-unsafe-assignment)


[error] 486-486: Newline required at end of file but not found.

(style/eol-last)

🔇 Additional comments (8)
src/commands/blocks.ts (2)

14-14: LGTM: Proper import of shared token utility.

The import correctly brings in the centralized getTotalTokens function, aligning with the PR objective to standardize token counting across the codebase.


184-184: Excellent refactoring to use shared token utility.

Replacing manual token summation with getTotalTokens(block.tokenCounts) ensures that all four token types (input, output, cache_creation_input, cache_read_input) are properly included in calculations, which directly addresses the PR objective.

Also applies to: 342-342, 406-406, 430-430

src/_live-rendering.ts (3)

19-19: LGTM: Proper import resolves previous duplication concern.

The import correctly uses the centralized getTotalTokens function from the shared utility module, addressing the previous review comment about code duplication.


97-97: Excellent adoption of shared token utility.

Replacing the manual summation block.tokenCounts.inputTokens + block.tokenCounts.outputTokens with getTotalTokens(block.tokenCounts) ensures cache tokens are properly included in the live monitoring display.


148-148: Layout padding adjustments improve alignment.

The changes from subtracting 3 to subtracting 2 in the padding calculations appear to correct alignment issues in the live display box layout.

Also applies to: 162-162, 229-229, 238-238, 290-290, 304-304, 309-309, 323-323, 334-334

src/calculate-cost.ts (3)

11-11: LGTM: Clean import of shared token utilities.

The imports correctly bring in the centralized types and functions from the shared utility module, supporting the PR's goal to standardize token counting logic.

Also applies to: 13-13


24-27: Good backward compatibility approach with deprecation notice.

The type alias maintains compatibility while encouraging migration to the shared type. The deprecation comment provides clear guidance for future updates.


69-70: Excellent backward compatibility strategy.

Re-exporting getTotalTokens ensures existing code continues to work while centralizing the implementation in the shared utility module.

@tifoji
Copy link
Contributor Author

tifoji commented Jun 25, 2025

Created a shared _token-utils.ts file that eliminates all code duplication. All 4 files now import from this single source. Also I ran npx eslint src --fix on the latest code and it found no issues to fix. The code already complies with the project's formatting standards. The warnings shown might be from an intermediate commit or using a different ESLint configuration than the project's current setup.

The refactoring is complete and all tests are passing (except for some timezone-related date formatting tests which are unrelated to this PR).

@drewcrawford
Copy link
Contributor

This reports a more accurate token count but the projection seems off. The attached screenshots compare with and without this PR for an account that is over the usage limit.

CleanShot 2025-06-26 at 22 24 53@2x
CleanShot 2025-06-26 at 22 25 02@2x

@tifoji
Copy link
Contributor Author

tifoji commented Jun 27, 2025

Yah I think we need to use getTotalTokens

Too bad I cannot share my screenshot but a bit disappointed that the right side border is still not aligned. Thought it looks perfect on mine.

@tifoji
Copy link
Contributor Author

tifoji commented Jun 27, 2025

This reports a more accurate token count but the projection seems off. The attached screenshots compare with and without this PR for an account that is over the usage limit.

It's cleaner

Screenshot 2025-06-27 at 2 30 16 PM

@ryoppippi
Copy link
Owner

Thank you for your contribution
@tifoji
I'll take a look tomorrow. let me know if this PR is ready

@ryoppippi
Copy link
Owner

Also @a-c-m could you review this?

@tifoji
Copy link
Contributor Author

tifoji commented Jun 29, 2025

Thank you for your contribution @tifoji I'll take a look tomorrow. let me know if this PR is ready

Yah would be great if others can test and confirm but the calcs were updated and UI slightly tweaked to get the aligned border.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Jun 29, 2025

Open in StackBlitz

npm i https://pkg.pr.new/ryoppippi/ccusage@193

commit: 105a10f

Copy link
Contributor

@a-c-m a-c-m left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks reasonable, from the code review. Moves some stuff around, but the main thing is:

export function getTotalTokens(tokens: TokenData): number {
	return (
		tokens.inputTokens
		+ tokens.outputTokens
		+ tokens.cacheCreationTokens
		+ tokens.cacheReadTokens
	);
}

into

export function getTotalTokens(tokenCounts: AnyTokenCounts): number {
	// Support both property naming conventions
	const cacheCreation = 'cacheCreationInputTokens' in tokenCounts
		? tokenCounts.cacheCreationInputTokens
		: (tokenCounts).cacheCreationTokens;

	const cacheRead = 'cacheReadInputTokens' in tokenCounts
		? tokenCounts.cacheReadInputTokens
		: (tokenCounts).cacheReadTokens;

	return (
		tokenCounts.inputTokens
		+ tokenCounts.outputTokens
		+ cacheCreation
		+ cacheRead
	);
}

No idea if this is a good idea or not. But, swapping to use the getTotalTokens in the blocks.ts is a good idea for sure.

@ryoppippi
Copy link
Owner

Thank you @a-c-m
well I'll fix the format and merge it tomorrow

Copy link
Owner

@ryoppippi ryoppippi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for late, but LGTM
Guys good job!!!

@ryoppippi ryoppippi merged commit 3190df5 into ryoppippi:main Jul 3, 2025
6 of 7 checks passed
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.

Live usage token count does not include cached tokens

4 participants