Skip to content

feat(widget): add cache-hit-rate widget#409

Open
sebryu wants to merge 1 commit into
sirmalloc:mainfrom
sebryu:feat/cache-hit-rate-widget
Open

feat(widget): add cache-hit-rate widget#409
sebryu wants to merge 1 commit into
sirmalloc:mainfrom
sebryu:feat/cache-hit-rate-widget

Conversation

@sebryu

@sebryu sebryu commented Jun 1, 2026

Copy link
Copy Markdown

Summary

Adds a cache-hit-rate widget that displays the prompt-cache hit rate for the current session, using the same formula the Anthropic Console reports.

Formula:

hit_rate = cache_read / (cache_read + cache_creation + fresh_input)

Renders as Cache: 87% (or 87% in raw-value mode). Returns null until any cache or fresh-input tokens are seen, so it does not flash 0% on empty transcripts.

Why

Prompt-cache effectiveness is one of the most useful signals for tuning Claude Code workflows — high hit rate means cheaper, faster turns. It is visible on console.anthropic.com but there is no way to see it at a glance during a session. This widget surfaces the same number directly in the status line.

Changes

  • src/widgets/CacheHitRate.ts — new widget, follows the existing token-widget pattern (raw-value support, colors, preview)
  • src/widgets/index.ts, src/utils/widget-manifest.ts — register cache-hit-rate
  • src/types/TokenMetrics.ts — adds two optional fields: cacheReadTokens and cacheCreationTokens. The existing cachedTokens (read + creation) is preserved unchanged, so all existing widgets, configs, and downstream consumers are unaffected
  • src/utils/jsonl-metrics.ts — sums cache_read_input_tokens and cache_creation_input_tokens separately while still rolling them into cachedTokens
  • Tests:
    • src/widgets/__tests__/CacheHitRate.test.ts — widget behaviour (null guards, formula, raw mode, preview)
    • src/utils/__tests__/jsonl-metrics.test.ts — per-field aggregation in the existing fixtures

Net diff: +177 / -3 across 7 files (3 of those are 1-line registrations/exports).

Compatibility

  • TokenMetrics.cachedTokens semantics unchanged
  • New fields on TokenMetrics are optional
  • No changes to public CLI surface or settings format
  • New widget is opt-in via the TUI

Test plan

  • bun run lint passes (tsc --noEmit + ESLint --max-warnings=0)
  • bun test — all 1401 tests pass, including 7 new widget tests and 6 added assertions in existing jsonl-metrics fixtures
  • Manual: rendered against a real transcript piped through bun run src/ccstatusline.ts and verified the percentage matches what Anthropic Console reports for the same session

Adds a Cache Hit Rate widget that mirrors the Anthropic Console's
prompt-cache hit-rate formula: cache_read / (cache_read + cache_creation
+ fresh_input). Returns null on empty transcripts so it does not flash
0% before any tokens are recorded.

Splits cacheReadTokens and cacheCreationTokens out of the existing
cachedTokens roll-up on TokenMetrics (both new fields are optional and
cachedTokens is preserved unchanged), so downstream widgets and configs
are unaffected.
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.

1 participant