Skip to content

Commit 3499b08

Browse files
committed
fix: Include cache tokens in live usage token count calculations
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 #179
1 parent d56374f commit 3499b08

1 file changed

Lines changed: 17 additions & 3 deletions

File tree

‎src/_live-rendering.ts‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,20 @@ export type LiveMonitoringConfig = {
3030
order: SortOrder;
3131
};
3232

33+
/**
34+
* Helper function to calculate total tokens including cache tokens
35+
* @param tokenCounts - Token counts from SessionBlock
36+
* @returns Total number of tokens
37+
*/
38+
function getTotalTokens(tokenCounts: SessionBlock['tokenCounts']): number {
39+
return (
40+
tokenCounts.inputTokens
41+
+ tokenCounts.outputTokens
42+
+ tokenCounts.cacheCreationInputTokens
43+
+ tokenCounts.cacheReadInputTokens
44+
);
45+
}
46+
3347
/**
3448
* Delay with AbortSignal support and graceful error handling
3549
*/
@@ -93,7 +107,7 @@ export function renderLiveDisplay(terminal: TerminalManager, block: SessionBlock
93107
const now = new Date();
94108

95109
// Calculate key metrics
96-
const totalTokens = block.tokenCounts.inputTokens + block.tokenCounts.outputTokens;
110+
const totalTokens = getTotalTokens(block.tokenCounts);
97111
const elapsed = (now.getTime() - block.startTime.getTime()) / (1000 * 60);
98112
const remaining = (block.endTime.getTime() - now.getTime()) / (1000 * 60);
99113

@@ -144,7 +158,7 @@ export function renderLiveDisplay(terminal: TerminalManager, block: SessionBlock
144158
const sessionLabel = pc.bold('⏱️ SESSION');
145159
const sessionLabelWidth = stringWidth(sessionLabel);
146160
const sessionBarStr = `${sessionLabel}${''.padEnd(Math.max(0, labelWidth - sessionLabelWidth))} ${sessionProgressBar} ${sessionPercent.toFixed(1).padStart(6)}%`;
147-
const sessionBarPadded = sessionBarStr + ' '.repeat(Math.max(0, boxWidth - 3 - stringWidth(sessionBarStr)));
161+
const sessionBarPadded = sessionBarStr + ' '.repeat(Math.max(0, boxWidth - 2 - stringWidth(sessionBarStr)));
148162
terminal.write(`${marginStr}│ ${sessionBarPadded}│\n`);
149163

150164
// Session details (indented)
@@ -330,7 +344,7 @@ export function renderLiveDisplay(terminal: TerminalManager, block: SessionBlock
330344
if (block.models.length > 0) {
331345
terminal.write(`${marginStr}├${'─'.repeat(boxWidth - 2)}┤\n`);
332346
const modelsLine = `⚙️ Models: ${formatModelsDisplay(block.models)}`;
333-
const modelsLinePadded = modelsLine + ' '.repeat(Math.max(0, boxWidth - 3 - stringWidth(modelsLine)));
347+
const modelsLinePadded = modelsLine + ' '.repeat(Math.max(0, boxWidth - 2 - stringWidth(modelsLine)));
334348
terminal.write(`${marginStr}│ ${modelsLinePadded}│\n`);
335349
}
336350

0 commit comments

Comments
 (0)