claude_cli: add usage_total for the whole run (#190) #195

Closed
johannes wants to merge 1 commits from usage-total-claude-cli into main
pull from: usage-total-claude-cli
Owner

Done.usage stays the last request (context size); usage_total is the sum
over all requests. Exposed in the NDJSON done line, RPC replies and hx stats.

Co-Authored-By: Claude Sonnet 5.5 noreply@anthropic.com

Done.usage stays the last request (context size); usage_total is the sum over all requests. Exposed in the NDJSON done line, RPC replies and hx stats. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
johannes added 1 commit 2026-11-21 11:32:58 +00:00
Done.usage stays the last request (context size); usage_total is the sum
over all requests. Exposed in the NDJSON done line, RPC replies and hx stats.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Author
Owner

/review

/review

PR Reviewer Guide 🔍

(Review updated until commit 0184627f27)

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Cost Uses Wrong Usage

JSON done line now emits usage_total, but the text-mode summary below still reads agent.usage() for pricing(...) cost. With claude_cli, usage is last-request context size, so cost and token totals may be wrong. Check whether it should use usage_total().

        "usage": agent.usage(), "usage_total": agent.usage_total(), "session": path}),
    );
}
let u = &agent.usage();
let cost = pricing(agent.model())
Serde Compatibility

StreamEvent::Done gains usage_total. The README documents it as optional (usage_total?). Verify the field has #[serde(default, skip_serializing_if = "Option::is_none")]. Otherwise non-claude_cli providers emit "usage_total": null, and old persisted or RPC events may fail to deserialize. The updated roundtrip test only covers None.

let e = StreamEvent::Done {
    message: m,
    stop: StopReason::ToolUse,
    usage: Usage::default(),
    usage_total: None,
};
let s = serde_json::to_string(&e).unwrap();
assert_eq!(serde_json::from_str::<StreamEvent>(&s).unwrap(), e);
Unverified Totals

New report line relies on total and turns, defined outside the visible diff. Confirm total sums every request per turn without double counting usage. Confirm the "in" figure (input + cache_read + cache_write) matches how the other stats lines compute input. The claude_cli test asserts input_tokens == 2, but no test covers this report line.

if turns > 0 {
    let _ = writeln!(
        s,
        "claude total over {turns} turns (all requests): {} in ({} cached) / {} out",
        total.input_tokens + total.cache_read_tokens + total.cache_write_tokens,
        total.cache_read_tokens,
        total.output_tokens
    );
}
## PR Reviewer Guide 🔍 #### (Review updated until commit https://git.jokresner.de/johannes/hx/commit/0184627f272677e93e2c9303590ef84bf44b2100) Here are some key observations to aid the review process: <table> <tr><td>⏱️&nbsp;<strong>Estimated effort to review</strong>: 2 🔵🔵⚪⚪⚪</td></tr> <tr><td>🧪&nbsp;<strong>PR contains tests</strong></td></tr> <tr><td>🔒&nbsp;<strong>No security concerns identified</strong></td></tr> <tr><td>⚡&nbsp;<strong>Recommended focus areas for review</strong><br><br> <details><summary><a href='https://git.jokresner.de/johannes/hx/src/branch/usage-total-claude-cli/crates/hx-cli/src/main.rs#L1336-L1340'><strong>Cost Uses Wrong Usage</strong></a> JSON `done` line now emits `usage_total`, but the text-mode summary below still reads `agent.usage()` for `pricing(...)` cost. With `claude_cli`, `usage` is last-request context size, so cost and token totals may be wrong. Check whether it should use `usage_total()`. </summary> ```rust "usage": agent.usage(), "usage_total": agent.usage_total(), "session": path}), ); } let u = &agent.usage(); let cost = pricing(agent.model()) ``` </details> <details><summary><a href='https://git.jokresner.de/johannes/hx/src/branch/usage-total-claude-cli/crates/hx-ai/src/lib.rs#L349-L356'><strong>Serde Compatibility</strong></a> `StreamEvent::Done` gains `usage_total`. The README documents it as optional (`usage_total?`). Verify the field has `#[serde(default, skip_serializing_if = "Option::is_none")]`. Otherwise non-`claude_cli` providers emit `"usage_total": null`, and old persisted or RPC events may fail to deserialize. The updated roundtrip test only covers `None`. </summary> ```rust let e = StreamEvent::Done { message: m, stop: StopReason::ToolUse, usage: Usage::default(), usage_total: None, }; let s = serde_json::to_string(&e).unwrap(); assert_eq!(serde_json::from_str::<StreamEvent>(&s).unwrap(), e); ``` </details> <details><summary><a href='https://git.jokresner.de/johannes/hx/src/branch/usage-total-claude-cli/crates/hx-core/src/stats.rs#L118-L126'><strong>Unverified Totals</strong></a> New report line relies on `total` and `turns`, defined outside the visible diff. Confirm `total` sums every request per turn without double counting `usage`. Confirm the "in" figure (`input + cache_read + cache_write`) matches how the other stats lines compute input. The `claude_cli` test asserts `input_tokens == 2`, but no test covers this report line. </summary> ```rust if turns > 0 { let _ = writeln!( s, "claude total over {turns} turns (all requests): {} in ({} cached) / {} out", total.input_tokens + total.cache_read_tokens + total.cache_write_tokens, total.cache_read_tokens, total.output_tokens ); } ``` </details> </td></tr> </table>

Persistent review updated to latest commit 0184627f27

**[Persistent review](https://git.jokresner.de/johannes/hx/pulls/195#issuecomment-1359)** updated to latest commit https://git.jokresner.de/johannes/hx/commit/0184627f272677e93e2c9303590ef84bf44b2100

PR Code Suggestions ✨

No code suggestions found for the PR.

## PR Code Suggestions ✨ No code suggestions found for the PR.
johannes closed this pull request 2026-11-27 23:21:46 +00:00
johannes deleted branch usage-total-claude-cli 2026-11-27 23:21:46 +00:00

Pull request closed

This pull request cannot be reopened because the branch was deleted.
Sign in to join this conversation.
No Reviewers
2 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: johannes/hx#195