fix: warmup cooldown timing + rising cost threshold + accurate TTL pricing#373
Merged
Conversation
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
Fixes cache warming economics — warmup was costing $65.89 but only saving $4.43 (net -$59.50). The dominant issue was a cooldown timing bug causing every continuation warmup to be a cold cache write (
$1.25) instead of a cheap refresh ($0.10).Root Cause
The normal-path cooldown was set to
ttlMs(300s for 5m TTL) instead ofttlMs - warmupMarginMs(255s). Since warmups fire in the margin window (4:15–5:00), a 300s cooldown means the next warmup fires right as the previous cache expires — every warmup is a cold write. The forced/tool-call path already used the correct tighter cooldown.Changes
Fix 1: Cooldown timing (the $65 → $6.50 fix)
ttlMs - warmupMarginMsas cooldown for all paths, not just forced/tool-callcooldownFor()(used by dashboard diagnostics) to matchFix 2: Rising cumulative cost threshold
cumulativeCostThreshold(k, read, write)— after k warmup cycles, break-even requires higher P(returns)P > k × read / (write - read)— rises linearly with cycles spentcostThreshold()exactly; clamped to 1.0Fix 3: TTL-aware cost recording
recordWarmupCost()andrecordWarmupHit()accept optionalttlparametercache_writeto match Anthropic's actual pricingrecordTTLSavingsintentionally uses base rate (counterfactual is against 5m, not 1h)Fix 4: Session-level ROI guard
notWarmingReasonupdated to show ROI guard and rising threshold rejectionsTests
cumulativeCostThreshold()tests: formula values, k=1 matchescostThreshold(), clamping, edge casesImpact Estimate
For the session that triggered investigation (410 turns, 65 warmups, 12 hits):