pub struct SubagentUsageRollup {
pub subagent_tokens: u64,
pub agent_count: u32,
pub tool_uses: u64,
pub duration_ms: u64,
/* private fields */
}Expand description
Session-level subagent token rollup — the <subagent_tokens> /
<agent_count> line items the Claude CLI renders in its terminal
<usage> block.
The stream-json protocol does not carry this rollup on the result
frame’s usage (confirmed against the CLI binary — the terminal renderer
computes it from Task tool results). Consumers that need it must
accumulate it the same way: feed every session message through
observe and read the totals at any point.
A Task result observed twice under the same agentId (e.g. a replayed
frame on resume) is counted once. Results with no agentId are counted
every time they are observed.
§Example
use claude_codes::{ClaudeOutput, SubagentUsageRollup};
let mut rollup = SubagentUsageRollup::default();
let json = r#"{"type":"user","message":{"role":"user","content":[]},"session_id":"7fbc568e-2bd6-45aa-b217-a1cf80004ba1","tool_use_result":{"status":"completed","agentId":"ab52f22445470d454","totalDurationMs":1853,"totalTokens":10201,"totalToolUseCount":0}}"#;
let output: ClaudeOutput = serde_json::from_str(json).unwrap();
rollup.observe(&output);
assert_eq!(rollup.subagent_tokens, 10201);
assert_eq!(rollup.agent_count, 1);Fields§
§subagent_tokens: u64Total tokens consumed by subagents — sum of
SubagentResult::total_tokens over every observed Task result.
agent_count: u32Number of subagent runs observed (<agent_count>).
tool_uses: u64Total subagent tool invocations — sum of total_tool_use_count.
duration_ms: u64Total subagent wall-clock milliseconds — sum of total_duration_ms.
Implementations§
Source§impl SubagentUsageRollup
impl SubagentUsageRollup
Sourcepub fn observe(&mut self, output: &ClaudeOutput) -> bool
pub fn observe(&mut self, output: &ClaudeOutput) -> bool
Accumulate output into the rollup if it is a Task tool result.
Returns true when the message contributed to the totals. Non-user
messages, user messages without a tool_use_result, results from
other tools, and duplicate agentIds are all ignored.
Sourcepub fn observe_user(&mut self, user: &UserMessage) -> bool
pub fn observe_user(&mut self, user: &UserMessage) -> bool
Accumulate a user message’s Task tool result, if it carries one.
Every SubagentResult field is optional, so any JSON object in
tool_use_result parses as one (e.g. a Bash or ToolSearch
result). Only results carrying an agentId or a totalTokens
line item are treated as genuine Task results.
Trait Implementations§
Source§impl Clone for SubagentUsageRollup
impl Clone for SubagentUsageRollup
Source§fn clone(&self) -> SubagentUsageRollup
fn clone(&self) -> SubagentUsageRollup
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more