Skip to main content

SubagentUsageRollup

Struct SubagentUsageRollup 

Source
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: u64

Total tokens consumed by subagents — sum of SubagentResult::total_tokens over every observed Task result.

§agent_count: u32

Number of subagent runs observed (<agent_count>).

§tool_uses: u64

Total subagent tool invocations — sum of total_tool_use_count.

§duration_ms: u64

Total subagent wall-clock milliseconds — sum of total_duration_ms.

Implementations§

Source§

impl SubagentUsageRollup

Source

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.

Source

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

Source§

fn clone(&self) -> SubagentUsageRollup

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SubagentUsageRollup

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for SubagentUsageRollup

Source§

fn default() -> SubagentUsageRollup

Returns the “default value” for a type. Read more
Source§

impl Eq for SubagentUsageRollup

Source§

impl PartialEq for SubagentUsageRollup

Source§

fn eq(&self, other: &SubagentUsageRollup) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for SubagentUsageRollup

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.