feat: /cost per-turn breakdown dashboard#13
Conversation
Show per-turn token usage, cost, and model in /cost command. Tracks each turn's input/output tokens and estimated cost. /cost --total still shows all-time cumulative cost.
📝 WalkthroughWalkthroughThe changes add per-turn cost tracking to the CLI/TUI application. The Changes
Sequence DiagramsequenceDiagram
participant CLI as CLI Handler
participant App as App State
participant Cmd as Cost Command
CLI->>CLI: Receive UiMessage::Usage
CLI->>CLI: Compute per-turn cost
CLI->>App: Push (turn_num, input, output, cost, model)
CLI->>App: Update cumulative totals
Cmd->>App: Request turn_costs vector
App-->>Cmd: Return per-turn records
Cmd->>Cmd: Format and display breakdown
Cmd-->>Cmd: Show session total + per-turn details
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@mc/crates/mc-cli/src/main.rs`:
- Around line 349-351: Run rustfmt on the repository and commit the changes so
the file compiles under `cargo fmt --check`; specifically reformat the block
around the code that computes `turn_cost` and `turn_num` and the
`app.turn_costs.push(...)` call in `main.rs` (look for the `turn_cost`,
`turn_num`, and `app.turn_costs.push` symbols) by running `cargo fmt` locally
and committing the resulting formatting changes.
- Around line 349-351: The per-turn cost is recorded with the model used for
that turn (turn_cost and app.turn_costs push), but the session total is being
re-priced using the current model later, causing drift after /model switches;
fix by ensuring you persist the computed turn_cost (as already done when pushing
(turn_num, input, output, turn_cost, app.model.clone())) and change the session
total calculation to sum the stored per-turn cost values instead of re-calling
registry.estimate_cost with the current model — locate places that compute the
session total and replace re-estimation logic with summing the 4th tuple element
from app.turn_costs so totals remain consistent with the model used per turn.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: bf8ffce5-cf88-4ef1-bab8-5c3f1c243f27
📒 Files selected for processing (3)
mc/crates/mc-cli/src/main.rsmc/crates/mc-tui/src/app.rsmc/crates/mc-tui/src/commands.rs
| let turn_cost = registry.estimate_cost(&app.model, input, output); | ||
| let turn_num = app.turn_costs.len() as u32 + 1; | ||
| app.turn_costs.push((turn_num, input, output, turn_cost, app.model.clone())); |
There was a problem hiding this comment.
Rustfmt mismatch is currently failing CI.
The pipeline reports cargo fmt --check failure around this hunk (Line 348-350). Please run cargo fmt and commit the formatted output.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@mc/crates/mc-cli/src/main.rs` around lines 349 - 351, Run rustfmt on the
repository and commit the changes so the file compiles under `cargo fmt
--check`; specifically reformat the block around the code that computes
`turn_cost` and `turn_num` and the `app.turn_costs.push(...)` call in `main.rs`
(look for the `turn_cost`, `turn_num`, and `app.turn_costs.push` symbols) by
running `cargo fmt` locally and committing the resulting formatting changes.
Fix session total cost drift after model switches.
Line 349+ stores per-turn cost with that turn’s model, but session total is still derived from cumulative tokens priced with the current model. After /model changes, /cost total can be wrong and inconsistent with the per-turn breakdown.
Proposed fix
- app.session_cost = registry.estimate_cost(
- &app.model,
- app.total_input_tokens,
- app.total_output_tokens,
- );
- let turn_cost = registry.estimate_cost(&app.model, input, output);
+ let turn_cost = registry.estimate_cost(&app.model, input, output);
let turn_num = app.turn_costs.len() as u32 + 1;
- app.turn_costs.push((turn_num, input, output, turn_cost, app.model.clone()));
+ app.turn_costs
+ .push((turn_num, input, output, turn_cost, app.model.clone()));
+ app.session_cost = app.turn_costs.iter().map(|(_, _, _, c, _)| c).sum();🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@mc/crates/mc-cli/src/main.rs` around lines 349 - 351, The per-turn cost is
recorded with the model used for that turn (turn_cost and app.turn_costs push),
but the session total is being re-priced using the current model later, causing
drift after /model switches; fix by ensuring you persist the computed turn_cost
(as already done when pushing (turn_num, input, output, turn_cost,
app.model.clone())) and change the session total calculation to sum the stored
per-turn cost values instead of re-calling registry.estimate_cost with the
current model — locate places that compute the session total and replace
re-estimation logic with summing the 4th tuple element from app.turn_costs so
totals remain consistent with the model used per turn.
What
Enhanced
/costcommand with per-turn breakdown showing input/output tokens, cost, and model for each turn.Example output
Changes
mc-tui/src/app.rs: Addturn_costsfield to Appmc-tui/src/commands.rs: Enhancedcmd_costwith per-turn breakdownmc-cli/src/main.rs: Record per-turn cost on each Usage event152 tests pass.
Summary by CodeRabbit
/costcommand now displays detailed per-turn cost breakdowns, including input/output token counts and model information for each conversation turn, alongside session totals.