fix: cache hit % shouldn't include cache writes in denominator - #107
Merged
Conversation
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>
This was referenced May 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Supersedes #11 (which was filed 2026-04-05 against the old
src/backend/claude-sdk/index.tsand 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 — dropcacheWritefrom 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, becausecache_writetokens 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)
src/backend/claude-sdk/handler.tstotalPrompt(incl.sdkCacheWrite) →cacheTotal(justsdkInputTokens + sdkCacheRead)src/frontend/telegram/commands.tstotalPrompt(incl.displayCacheWrite) →cacheTotal(justdisplayInputTokens + displayCacheRead)The
commands.tslocation now reads enriched values fromgetSessionSnapshot()(thedisplayCacheRead/displayCacheWritelocals) — same shape, same fix, slightly different line numbers than #11's diff.Test plan
npx tsc --noEmitcleannpx vitest run— 1635 / 1635 passingnpm run lint— 0 errors (9 pre-existing warnings, none in changed files)+4 / -6across 2 files)/statusafter merge and confirm cache hit % looks reasonableNotes
claude-sdk/index.ts) whose lines no longer exist post-refactor, and force-resetting Dylan's branch felt more invasive than landing the fix on a fresh branch and pointing at Fix cache hit percentage calculation #11.🤖 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."