feat: add cache hit rate column to all usage report commands#918
feat: add cache hit rate column to all usage report commands#918BuluBulugege wants to merge 1 commit intoryoppippi:mainfrom
Conversation
Add Hit Rate as a first-class metric alongside Input/Output/Cache columns in daily, monthly, weekly, and session reports. The hit rate is calculated as CacheRead / (Input + CacheCreate + CacheRead), color-coded green (>=70%), yellow (>=40%), or red (<40%). Also included in compact mode and JSON output. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
📝 WalkthroughWalkthroughThis pull request adds cache hit rate computation and visualization across multiple usage reporting commands. A new Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
apps/ccusage/src/commands/daily.ts (1)
102-125:⚠️ Potential issue | 🟠 MajorAdd
cacheHitRateto grouped project JSON output.The
--instances --jsonpath omitscacheHitRatewhile the non-grouped--jsonpath includes it, creating inconsistent JSON schemas. AddcacheHitRate: calculateCacheHitRate(data)to the object pushed ingroupByProject()(line 30 of_daily-grouping.ts), and update theDailyProjectOutputtype in_json-output-types.tsto include the field.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@apps/ccusage/src/commands/daily.ts` around lines 102 - 125, The grouped JSON path is missing cacheHitRate: add cacheHitRate: calculateCacheHitRate(data) to the object that groupByProject pushes for each day in _daily-grouping.ts (ensure calculateCacheHitRate is imported/accessible there), and update the DailyProjectOutput type in _json-output-types.ts to include cacheHitRate (number or nullable number consistent with other fields) so the grouped (--instances --json) schema matches the non-grouped JSON output.
🧹 Nitpick comments (1)
packages/terminal/src/table.ts (1)
523-552: Avoid formula duplication betweenformatCacheHitRateandcalculateCacheHitRate.Keep one source of truth for the formula to prevent future drift.
♻️ Suggested refactor
export function formatCacheHitRate(data: { inputTokens: number; cacheCreationTokens: number; cacheReadTokens: number; }): string { - const totalInput = data.inputTokens + data.cacheCreationTokens + data.cacheReadTokens; - const rate = totalInput > 0 ? data.cacheReadTokens / totalInput : 0; + const rate = calculateCacheHitRate(data); const pct = `${(rate * 100).toFixed(1)}%`; if (rate >= 0.7) { return pc.green(pct); }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/terminal/src/table.ts` around lines 523 - 552, The cache-hit calculation logic is duplicated between formatCacheHitRate and calculateCacheHitRate; change formatCacheHitRate to call calculateCacheHitRate(data) to obtain the rate and then perform formatting/threshold checks (pct string and color selection) so the numeric formula lives only in calculateCacheHitRate; update any variable names accordingly and remove the duplicated arithmetic in formatCacheHitRate so calculateCacheHitRate is the single source of truth for the rate.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Outside diff comments:
In `@apps/ccusage/src/commands/daily.ts`:
- Around line 102-125: The grouped JSON path is missing cacheHitRate: add
cacheHitRate: calculateCacheHitRate(data) to the object that groupByProject
pushes for each day in _daily-grouping.ts (ensure calculateCacheHitRate is
imported/accessible there), and update the DailyProjectOutput type in
_json-output-types.ts to include cacheHitRate (number or nullable number
consistent with other fields) so the grouped (--instances --json) schema matches
the non-grouped JSON output.
---
Nitpick comments:
In `@packages/terminal/src/table.ts`:
- Around line 523-552: The cache-hit calculation logic is duplicated between
formatCacheHitRate and calculateCacheHitRate; change formatCacheHitRate to call
calculateCacheHitRate(data) to obtain the rate and then perform
formatting/threshold checks (pct string and color selection) so the numeric
formula lives only in calculateCacheHitRate; update any variable names
accordingly and remove the duplicated arithmetic in formatCacheHitRate so
calculateCacheHitRate is the single source of truth for the rate.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: a0bfdabf-9d95-4731-955e-17c465e9627c
📒 Files selected for processing (5)
apps/ccusage/src/commands/daily.tsapps/ccusage/src/commands/monthly.tsapps/ccusage/src/commands/session.tsapps/ccusage/src/commands/weekly.tspackages/terminal/src/table.ts
Summary
CacheRead / (Input + CacheCreate + CacheRead), color-coded: green (>=70%), yellow (>=40%), red (<40%)cacheHitRatefield for programmatic access-b) also displays per-model hit rateTest plan
ccusage daily— verify Hit Rate column appearsccusage session— verify Hit Rate column with Last Activityccusage daily --json— verifycacheHitRatein JSON outputccusage daily --compact— verify Hit Rate in compact modeccusage daily -b— verify per-model hit rate in breakdown rows🤖 Generated with Claude Code
Summary by CodeRabbit
Release Notes
New Features