Skip to content

fix: cache hit % shouldn't include cache writes in denominator - #107

Merged
dylanneve1 merged 1 commit into
mainfrom
fix/cache-hit-pct-rebased
May 7, 2026
Merged

fix: cache hit % shouldn't include cache writes in denominator#107
dylanneve1 merged 1 commit into
mainfrom
fix/cache-hit-pct-rebased

Conversation

@claudiusthebot

Copy link
Copy Markdown
Collaborator

Summary

Supersedes #11 (which was filed 2026-04-05 against the old src/backend/claude-sdk/index.ts and went stale after the v1.9.0 SDK refactor split that file into 8 focused modules).

The cache hit % calculation in two display surfaces still has the original bug from #11 on current main. The fix is mechanically identical — drop cacheWrite from the denominator — applied at the post-refactor locations.

The bug

Cache hit % was calculated as cache_read / (input + cache_read + cache_write). That dilutes the metric every time the cache is being warmed for the first time, because cache_write tokens are tokens being written to cache on this call — they're not "readable input served from cache." The correct denominator is just the readable input (input + cache_read).

What changed (6 lines)

File Lines Change
src/backend/claude-sdk/handler.ts 230-233 totalPrompt (incl. sdkCacheWrite) → cacheTotal (just sdkInputTokens + sdkCacheRead)
src/frontend/telegram/commands.ts 532-535 totalPrompt (incl. displayCacheWrite) → cacheTotal (just displayInputTokens + displayCacheRead)

The commands.ts location now reads enriched values from getSessionSnapshot() (the displayCacheRead/displayCacheWrite locals) — same shape, same fix, slightly different line numbers than #11's diff.

Test plan

  • npx tsc --noEmit clean
  • npx vitest run — 1635 / 1635 passing
  • npm run lint — 0 errors (9 pre-existing warnings, none in changed files)
  • Diff is minimal (+4 / -6 across 2 files)
  • Manual: run /status after merge and confirm cache hit % looks reasonable

Notes

🤖 Heartbeat agent — picked up #11 from heartbeat #65's "review one of the older PRs (#11) and leave a substantive comment IF something real to say." The substantive thing was the rebased fix, which had been deferred since heartbeat #44's review left "APPROVE — once rebased."

Cache hit % was calculated as cache_read / (input + cache_read +
cache_write), which dilutes the metric every time the cache is being
warmed for the first time — cache_write tokens are tokens being
*written to* cache on this call, they're not "readable input served
from cache."

Now: cache_read / (input + cache_read).

Same fix as PR #11, applied at the post-SDK-refactor file locations:
- src/backend/claude-sdk/handler.ts (was claude-sdk/index.ts on #11)
- src/frontend/telegram/commands.ts (slightly different lines now,
  reads displayCacheRead/Write from getSessionSnapshot enrichment)

Six-line change. Supersedes #11.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@claudiusthebot
claudiusthebot requested a review from dylanneve1 as a code owner May 4, 2026 10:54
@dylanneve1
dylanneve1 merged commit 379eace into main May 7, 2026
13 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.

2 participants