From 643c1b511cef0e4d8e3c40bcb71a263fe4835f0e Mon Sep 17 00:00:00 2001 From: Resham Joshi <65915470+iamtoruk@users.noreply.github.com> Date: Thu, 24 Sep 2026 02:54:50 +0200 Subject: [PATCH] optimize: flag sessions with a low prompt cache hit rate (#1536) * optimize: flag sessions with a low prompt cache hit rate * optimize: make low cache hit fix manual advice --- src/optimize.ts | 64 +++++++++++++++++++++++++++++++++++- tests/optimize-apply.test.ts | 1 + tests/optimize.test.ts | 40 ++++++++++++++++++++++ 3 files changed, 104 insertions(+), 1 deletion(-) diff --git a/src/optimize.ts b/src/optimize.ts index d507ac76..261b58e3 100644 --- a/src/optimize.ts +++ b/src/optimize.ts @@ -16,7 +16,7 @@ import { formatTokens } from './format.js' import { recommendModelDefault, type ModelDefaultRecommendation } from './act/model-defaults.js' import { appliedFixGlyph, formatAppliedFix, type AppliedFix } from './act/types.js' import { isUserStartedSession, userStartedProjects } from './session-population.js' -import { sessionBillableOutputTokens } from './session-output.js' +import { inferSessionProvider, sessionBillableOutputTokens } from './session-output.js' import { aggregateFileChurn, buildCoachingNotes, scanUserCorrections, medianTimeToFirstEditMs, worstOneShotCategory, type ReworkedFile } from './workflow-insights.js' // ============================================================================ @@ -108,6 +108,10 @@ const CONTEXT_BLOAT_HIGH_MIN_CANDIDATES = 10 const CONTEXT_BLOAT_GROWTH_RATIO = 2 const CONTEXT_BLOAT_GROWTH_MAX_GAP_MS = 7 * 24 * 60 * 60 * 1000 const CONTEXT_BLOAT_RATIO_DISPLAY_CAP = 1000 +const LOW_CACHE_HIT_TARGET_PERCENT = 80 +const LOW_CACHE_HIT_MIN_INPUT_TOKENS = CONTEXT_BLOAT_MIN_INPUT_TOKENS +const LOW_CACHE_HIT_MIN_CALLS = 5 +const LOW_CACHE_HIT_PREVIEW = 5 const WORTH_IT_MIN_COST_USD = 2 const WORTH_IT_NO_EDIT_MIN_COST_USD = 3 // A zero-edit session's full cost is an upper bound on recoverable waste, not @@ -357,6 +361,7 @@ export type FindingId = | 'low-worth-sessions' | 'context-heavy-sessions' | 'cost-outliers' + | 'low-cache-hit-sessions' | 'claude-md-too-long' | 'bash-output-cap' | 'unused-agents' @@ -395,6 +400,7 @@ export const FINDING_CLASS: Record = { 'low-worth-sessions': 'nudge', 'context-heavy-sessions': 'keep', // context-heavy work is often load-bearing 'cost-outliers': 'nudge', + 'low-cache-hit-sessions': 'nudge', 'claude-md-too-long': 'nudge', // trimming is a judgement call, not a rule block 'bash-output-cap': 'fix', 'unused-agents': 'fix', @@ -435,6 +441,7 @@ export const FINDING_BASIS: Record = { 'low-worth-sessions': 'estimated', // real session tokens x recovery fraction 'context-heavy-sessions': 'measured', // counted input/cache tokens above the target ratio 'cost-outliers': 'measured', // counted session tokens above the peer average + 'low-cache-hit-sessions': 'estimated', // counted tokens below the target hit ratio x read discount 'claude-md-too-long': 'estimated', // lines x CLAUDEMD_TOKENS_PER_LINE 'bash-output-cap': 'estimated', // chars x BASH_TOKENS_PER_CHAR 'unused-agents': 'estimated', // count x TOKENS_PER_AGENT_DEF @@ -3822,6 +3829,60 @@ export function detectSessionOutliers(projects: ProjectSummary[], excludedSessio } } +export function detectLowCacheHitSessions(projects: ProjectSummary[]): WasteFinding | null { + type Candidate = { project: string; sessionId: string; provider: string; hitPercent: number; totalInput: number; missedReads: number } + const candidates: Candidate[] = [] + + for (const project of projects) { + for (const session of project.sessions) { + if (!isOptimizeSession(session)) continue + const reads = session.totalCacheReadTokens + const writes = session.totalCacheWriteTokens + // Parsers record unreported cache usage as 0, so a session with no cache + // tokens at all is unmeasured, not a 0% hit rate. + if (reads + writes === 0) continue + if (session.apiCalls < LOW_CACHE_HIT_MIN_CALLS) continue + const totalInput = session.totalInputTokens + reads + writes + if (totalInput < LOW_CACHE_HIT_MIN_INPUT_TOKENS) continue + // Same formula as the dashboard overview: reads over all input incl. writes. + const hitPercent = (reads / totalInput) * 100 + if (hitPercent >= LOW_CACHE_HIT_TARGET_PERCENT) continue + candidates.push({ + project: project.project, + sessionId: session.sessionId, + provider: inferSessionProvider(session), + hitPercent, + totalInput, + missedReads: totalInput * LOW_CACHE_HIT_TARGET_PERCENT / 100 - reads, + }) + } + } + + if (candidates.length === 0) return null + + candidates.sort((a, b) => b.missedReads - a.missedReads || a.sessionId.localeCompare(b.sessionId)) + const preview = candidates.slice(0, LOW_CACHE_HIT_PREVIEW) + const list = preview + .map(c => `${c.project}/${c.sessionId} (${providerDisplayName(c.provider)}): ${c.hitPercent.toFixed(1)}% of ${formatTokens(c.totalInput)} input`) + .join('; ') + const extra = candidates.length > preview.length ? `; +${candidates.length - preview.length} more` : '' + const tokensSaved = Math.round(candidates.reduce((sum, c) => sum + c.missedReads, 0) * (1 - CACHE_READ_DISCOUNT)) + + return { + id: 'low-cache-hit-sessions', + title: `${candidates.length} session${candidates.length === 1 ? '' : 's'} with a low cache hit rate`, + explanation: `These sessions read less than ${LOW_CACHE_HIT_TARGET_PERCENT}% of their input from the prompt cache: ${list}${extra}. Healthy agent sessions usually sit above ${LOW_CACHE_HIT_TARGET_PERCENT}%. Low rates come from switching models or tools mid-session, editing instruction files mid-session, or long pauses that let the cache expire, so the full prompt is paid again.`, + impact: candidates.length >= 3 || tokensSaved >= CONTEXT_BLOAT_HIGH_INPUT_TOKENS ? 'high' : 'medium', + tokensSaved, + fix: { + type: 'paste', + destination: 'manual', + label: 'Keep the prompt cache valid:', + text: 'Stay on one model per session, and start a new session after editing instruction files or MCP config instead of continuing the old one.', + }, + } +} + function findYoungProjectFirstSessionIds(projects: ProjectSummary[]): Set { const firstSessionIds = new Set() @@ -4023,6 +4084,7 @@ export async function scanAndDetect( () => detectLowWorthSessions(behavioralProjects, provider), () => detectContextBloat(behavioralProjects, lowWorthSessionIds, provider), () => detectSessionOutliers(behavioralProjects, outlierExclusions, provider), + () => detectLowCacheHitSessions(behavioralProjects), claudeOnly(() => detectBloatedClaudeMd(projectCwds)), claudeOnly(() => detectBashBloat()), claudeOnly(() => detectRecurringContext(openers)), diff --git a/tests/optimize-apply.test.ts b/tests/optimize-apply.test.ts index ab93a2fe..701705fa 100644 --- a/tests/optimize-apply.test.ts +++ b/tests/optimize-apply.test.ts @@ -956,6 +956,7 @@ describe('finding class', () => { 'low-worth-sessions': makeFinding('low-worth-sessions', OPENER_FIX), 'context-heavy-sessions': makeFinding('context-heavy-sessions', OPENER_FIX), 'cost-outliers': makeFinding('cost-outliers', OPENER_FIX), + 'low-cache-hit-sessions': makeFinding('low-cache-hit-sessions', OPENER_FIX), 'claude-md-too-long': makeFinding('claude-md-too-long', PROMPT_FIX), 'bash-output-cap': makeFinding('bash-output-cap', SHELL_FIX), 'unused-agents': makeFinding('unused-agents', CMD_FIX, { kind: 'archive', names: ['ghost'] }), diff --git a/tests/optimize.test.ts b/tests/optimize.test.ts index 4c34cb74..6905614c 100644 --- a/tests/optimize.test.ts +++ b/tests/optimize.test.ts @@ -21,6 +21,7 @@ import { detectCapabilityReliability, detectLowWorthSessions, detectSessionOutliers, + detectLowCacheHitSessions, scanAndDetect, cacheKey, computeHealth, @@ -604,6 +605,45 @@ describe('detectContextBloat', () => { }) }) +describe('detectLowCacheHitSessions', () => { + it('flags a big session under the 80% hit target', () => { + const project = projectWithContextSessions([ + contextSession(0, { apiCalls: 10, totalInputTokens: 60_000, totalCacheReadTokens: 40_000 }), + contextSession(1, { apiCalls: 10, totalInputTokens: 5_000, totalCacheReadTokens: 95_000 }), + ]) + const finding = detectLowCacheHitSessions([project]) + expect(finding).not.toBeNull() + expect(finding!.id).toBe('low-cache-hit-sessions') + expect(finding!.title).toBe('1 session with a low cache hit rate') + expect(finding!.explanation).toContain('app/s1') + expect(finding!.explanation).toContain('40.0% of 100.0K input') + expect(finding!.explanation).not.toContain('app/s2') + expect(finding!.tokensSaved).toBe(36_000) + }) + + it('counts cache writes as misses', () => { + const project = projectWithContextSessions([ + contextSession(0, { apiCalls: 10, totalInputTokens: 1_000, totalCacheWriteTokens: 60_000, totalCacheReadTokens: 39_000 }), + ]) + expect(detectLowCacheHitSessions([project])!.explanation).toContain('39.0% of 100.0K input') + }) + + it('ignores sessions with no cache token fields', () => { + const project = projectWithContextSessions([ + contextSession(0, { apiCalls: 10, totalInputTokens: 500_000 }), + ]) + expect(detectLowCacheHitSessions([project])).toBeNull() + }) + + it('ignores tiny sessions', () => { + const project = projectWithContextSessions([ + contextSession(0, { apiCalls: 10, totalInputTokens: 50_000, totalCacheReadTokens: 10_000 }), + contextSession(1, { apiCalls: 4, totalInputTokens: 500_000, totalCacheReadTokens: 10_000 }), + ]) + expect(detectLowCacheHitSessions([project])).toBeNull() + }) +}) + type LowWorthTurn = TestSession['turns'][number] function lowWorthTurn(overrides: Partial = {}): LowWorthTurn {