feat: cost savings calculator — show tokens, cost, time saved after replay#62
Conversation
…lculator Implements B1 of the GTM sprint — hardcoded price table for ~10 LLM models (gpt-4o, claude-sonnet, etc.) with estimate_cost() and compute_savings() functions. ReplaySavings struct summarizes tokens, cost, and time saved when fork-and-execute serves cached steps instead of re-running. 12 unit tests covering price lookups, cost calculation, and edge cases. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
When a session has forked timelines (from fork-and-execute replay), `rewind show` now displays a "Replay Savings" section showing cached steps, tokens saved, estimated cost saved, and time saved. Cumulative savings are shown when a session has multiple forks. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
After fork-and-execute replay exits, prints cached steps, tokens saved, estimated cost, and time saved to stderr. Mirrors the Rust pricing table with the same ~10 model price entries. Adds _cached_steps_count, _cached_tokens, _cached_duration_ms counters to Recorder, incremented in _try_replay_cached(). estimate_cost() and _estimate_cost_from_steps() exposed for reuse. 10 new tests: pricing lookups, cost-from-steps, savings counter tracking. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Adds a web API endpoint that returns cumulative replay savings for a session (steps cached, tokens saved, estimated cost, time saved). Computes savings by comparing parent timeline steps against forked timeline boundaries. Updates docs/replay-and-forking.md with Replay Savings section covering CLI, Python SDK, and API usage. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Code Review: Cost Savings Calculator (Feature B)Overall: Clean implementation across all 4 tasks. 1 architectural concern, 1 minor bug, 2 nits. Architectural Concern (P1)1.
Fix: Move Not blocking for this PR, but worth a follow-up refactor before the crate graph gets more tangled. Minor Bug (P2)2. Python
cost = _estimate_cost_from_steps(self._replay_steps or [], self._fork_at_step or 0)
The Rust side ( Suggested fix: cost = estimate_cost("default", self._cached_tokens // 2, self._cached_tokens // 2)Or better: track Nits (P3)3.
let secs = cumulative.time_saved_ms / 1000;For 1500ms, this shows 4. Duplicated savings computation logic in CLI and web API Both pub fn session_savings(store: &Store, session_id: &str) -> Result<ReplaySavings>Both call sites then become one-liners. Not blocking. What's Done Well
Summary
Fix #2 (use tracked counters instead of re-computing from step list) before merge. #1 and #3 are follow-up refactors. |
… cost Addresses review feedback on PR #62: P1: Moved pricing.rs from rewind-proxy to rewind-store so rewind-web no longer depends on rewind-proxy. rewind-proxy re-exports for backwards compat. Removes the cross-concern dependency coupling. P2: Python _print_replay_savings now uses self._cached_cost (tracked per actual cache hit in _try_replay_cached) instead of re-computing from the full parent step list. Fixes bug where steps with empty response_blob would be counted as savings despite not being served. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Summary
pricing.rsmodule inrewind-proxywith hardcoded price table (~10 models: gpt-4o, claude-sonnet, etc.),estimate_cost(),compute_savings(), andReplaySavingsstruct. 12 unit tests.rewind showdisplays a "Replay Savings" section for sessions with forked timelines — cached steps, tokens saved, estimated cost, time saved. Cumulative across multiple forks.Recordertracks cached steps/tokens/duration during fork-and-execute replay, prints savings summary to stderr whenreplay()exits. Mirrors the Rust price table. 10 new tests.GET /api/sessions/{id}/savingsweb endpoint returns cumulative savings JSON.docs/replay-and-forking.mdupdated with Replay Savings section (CLI, SDK, API).Test plan
cargo clippy --workspace -- -D warningscleanruff check .clean🤖 Generated with Claude Code