CodeWhale icon indicating copy to clipboard operation
CodeWhale copied to clipboard

feat: persist session cost and token usage across save/load cycles

Open Radiant303 opened this issue 7 months ago • 1 comments

Problem

When saving and reloading a session (via /save, /load, or the session picker), the cumulative cost and detailed token usage breakdown are lost. After reopening a previously saved session, the footer cost display resets to $0.00, and /cost /tokens commands show zero data.

The root cause is that session_cost, subagent_cost, displayed_cost_high_water in App (crates/tui/src/tui/app.rs) are purely in-memory. SavedSession saves total_tokens but not the computed USD cost or per-turn token breakdown (cache hits/misses, reasoning tokens, etc.).

Proposed solution

Add cost and cumulative token fields to SavedSession/SessionMetadata:

  • session_cost: f64
  • total_input_tokens, total_output_tokens
  • total_cache_hit_tokens, total_cache_miss_tokens
  • total_reasoning_tokens

Persist these on save, restore on load. When continuing a conversation, new turn costs accumulate on top of the restored values — the persisted cost reflects what was charged at the time, and subsequent turns simply add to it.

Alternatives considered

Additional context

The infrastructure already exists — SessionMetadata persists total_tokens, and SessionManager handles atomic save/load with schema versioning. The change is additive (new optional fields in SavedSession) and backward-compatible with existing session files.

Radiant303 avatar May 02 '26 14:05 Radiant303

Deferring to v0.9. v0.8.5 is shipping this week with focus on the in-flight backlog (atomic writes, panic safety, /logout fix, schema migration framework, cargo cleanup, /rlm prompt audit, Enter UX disambiguation, swarm cleanup cascade). This issue is well-scoped but doesn't fit the v0.8.5 cut. Re-open if it becomes urgent.

Hmbown avatar May 02 '26 15:05 Hmbown