Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

feat(usage): let a detached sub-agent's spend reach the composer

M3gA-Mindde33adcbcf77
diff --git a/app/src/providers/ChatRuntimeProvider.tsx b/app/src/providers/ChatRuntimeProvider.tsx
index bba1c8f90f..f4a989bd04 100644
--- a/app/src/providers/ChatRuntimeProvider.tsx
+++ b/app/src/providers/ChatRuntimeProvider.tsx
@@ -817,6 +817,44 @@ const ChatRuntimeProvider = ({ children }: { children: React.ReactNode }) => {
})
);
+ // A detached sub-agent's spend reaches the composer here or nowhere.
+ //
+ // The parent turn's `chat_done` fired before this child finished, and
+ // for a detached spawn the child's usage never entered the parent's
+ // ledger — `detached_child()` sets `parent_subagent_usage` to `None` —
+ // so `holistic_last_turn_usage` folded nothing and the reported totals
+ // are parent-only. The core populates these fields ONLY when that is
+ // the case, so adding them unconditionally is correct: for a blocking
+ // spawn they are absent, because its spend is already inside the
+ // `chat_done` figures (tokens AND cost). See #6459.
+ const childInput = event.subagent?.input_tokens;
+ const childOutput = event.subagent?.output_tokens;
+ if (childInput !== undefined || childOutput !== undefined) {
+ dispatch(
+ recordChatTurnUsage({
+ threadId: event.thread_id,
+ // The child's tokens go at the TOP level because that is what
+ // `applyTurnUsage` folds into the thread totals the composer
+ // shows — mirroring `chat_done`, whose top-level figures are
+ // already parent+child. The `subAgents` entry below is the
+ // per-agent breakdown, not the total.
+ inputTokens: childInput ?? 0,
+ outputTokens: childOutput ?? 0,
+ cachedTokens: event.subagent?.cached_input_tokens ?? 0,
+ costUsd: event.subagent?.cost_usd ?? 0,
+ subAgentSpendOnly: true,
+ subAgents: [
+ {
+ agentId: event.tool_name ?? 'subagent',
+ inputTokens: childInput ?? 0,
+ outputTokens: childOutput ?? 0,
+ costUsd: event.subagent?.cost_usd ?? 0,
+ },
+ ],
+ })
+ );
+ }
+
const current = store.getState().chatRuntime.inferenceStatusByThread[event.thread_id];
if (!current) return;
dispatch(
diff --git a/app/src/services/chatService.ts b/app/src/services/chatService.ts
index 63acf05490..84665acd35 100644
--- a/app/src/services/chatService.ts
+++ b/app/src/services/chatService.ts
@@ -412,6 +412,20 @@ export interface SubagentProgressDetail {
changed_files?: string[];
/** Whether the worker's worktree had uncommitted changes (on `subagent_completed`). */
dirty_status?: boolean;
+ /**
+ * This child's own spend (on `subagent_completed`) — present **only when it
+ * is not already inside the parent turn's totals**.
+ *
+ * A blocking spawn records into the parent's ledger and `chat_done` already
+ * carries the child's tokens AND cost; a detached spawn does not, and the
+ * core populates these instead. So the consumer adds whatever arrives
+ * unconditionally: absent means "already counted, or this emit site does not
+ * know", and both resolve to "add nothing".
+ */
+ input_tokens?: number;
+ output_tokens?: number;
+ cached_input_tokens?: number;
+ cost_usd?: number;
}
/** Extended payload for `subagent_spawned`. */
diff --git a/app/src/store/__tests__/chatRuntimeSlice.subagentSpend.test.ts b/app/src/store/__tests__/chatRuntimeSlice.subagentSpend.test.ts
new file mode 100644
index 0000000000..a313bbe978
--- /dev/null
+++ b/app/src/store/__tests__/chatRuntimeSlice.subagentSpend.test.ts
@@ -0,0 +1,116 @@
+/**
+ * Sub-agent spend reaches the composer exactly ONCE, whichever way it was
+ * spawned (#6459).
+ *
+ * The composer's totals are already sub-agent-inclusive: `applyTurnUsage`
+ * folds the top-level figures, which `chat_done` sends as parent+child. The
+ * defect is that a DETACHED child's spend never arrives — `detached_child()`
+ * severs `parent_subagent_usage`, so `holistic_last_turn_usage` folds nothing
+ * and `chat_done` reports parent-only.
+ *
+ * Fixing that creates the opposite risk, which is worse: a BLOCKING child's
+ * spend is already inside `chat_done` (tokens **and** `charged_amount_usd`),
+ * so adding it again would double the user's reported money. A doubled cost
+ * reads as fact and gets quoted; a missing one reads as "not implemented".
+ *
+ * So the assertion here is not "sub-agent tokens appear" — that passes the
+ * moment the field exists. It is **once, in both modes, tokens and cost**.
+ */
+import { describe, expect, it } from 'vitest';
+
+import chatRuntimeReducer, { recordChatTurnUsage } from '../chatRuntimeSlice';
+
+const THREAD = 't-spend';
+
+function reduce(actions: ReturnType<typeof recordChatTurnUsage>[]) {
+ let state = chatRuntimeReducer(undefined, { type: '@@INIT' });
+ for (const action of actions) state = chatRuntimeReducer(state, action);
+ return state.usageByThread[THREAD];
+}
+
+/** `chat_done` for a BLOCKING spawn: top-level totals already include the child. */
+const blockingChatDone = recordChatTurnUsage({
+ threadId: THREAD,
+ inputTokens: 1000, // 700 parent + 300 child
+ outputTokens: 200, // 150 parent + 50 child
+ costUsd: 0.05, // parent + child
+ contextWindow: 100_000,
+ subAgents: [{ agentId: 'researcher', inputTokens: 300, outputTokens: 50, costUsd: 0.02 }],
+});
+
+/** `chat_done` for a DETACHED spawn: parent-only, because the ledger was severed. */
+const detachedChatDone = recordChatTurnUsage({
+ threadId: THREAD,
+ inputTokens: 700,
+ outputTokens: 150,
+ costUsd: 0.03,
+ contextWindow: 100_000,
+});
+
+/** The late `subagent_completed` delta the detached child now sends. */
+const detachedChildSpend = recordChatTurnUsage({
+ threadId: THREAD,
+ inputTokens: 300,
+ outputTokens: 50,
+ costUsd: 0.02,
+ subAgentSpendOnly: true,
+ subAgents: [{ agentId: 'researcher', inputTokens: 300, outputTokens: 50, costUsd: 0.02 }],
+});
+
+describe('sub-agent spend reaches the composer once', () => {
+ it('counts a blocking child once — its spend is already in chat_done', () => {
+ const usage = reduce([blockingChatDone]);
+
+ expect(usage?.inputTokens).toBe(1000);
+ expect(usage?.outputTokens).toBe(200);
+ expect(usage?.costUsd).toBeCloseTo(0.05, 6);
+ });
+
+ it('counts a detached child once — its spend arrives separately', () => {
+ // Same real-world turn as the blocking case: 700+300 in, 150+50 out,
+ // $0.03+$0.02. The totals must land identically however it was spawned.
+ const usage = reduce([detachedChatDone, detachedChildSpend]);
+
+ expect(usage?.inputTokens).toBe(1000);
+ expect(usage?.outputTokens).toBe(200);
+ expect(usage?.costUsd).toBeCloseTo(0.05, 6);
+ });
+
+ it('does NOT double a blocking child if a late delta also arrives', () => {
+ // The failure this guard exists for. The core must not populate usage on
+ // `subagent_completed` for a blocking spawn; if it ever did, this is what
+ // the user would see. Asserted so the expected number is written down.
+ const usage = reduce([blockingChatDone, detachedChildSpend]);
+
+ expect(usage?.inputTokens).toBe(1300);
+ expect(usage?.costUsd).toBeCloseTo(0.07, 6);
+ });
+
+ it('does not count a sub-agent delta as another turn', () => {
+ const blocking = reduce([blockingChatDone]);
+ const detached = reduce([detachedChatDone, detachedChildSpend]);
+
+ expect(blocking?.turns).toBe(1);
+ // One user turn happened, not two — the child is more spend on the same
+ // turn, and counting it skews every per-turn average derived from this.
+ expect(detached?.turns).toBe(1);
+ });
+
+ it('leaves the context gauge alone when a sub-agent delta lands', () => {
+ // `lastTurnContextUsed` is the orchestrator's own window occupancy and
+ // deliberately excludes children (#4271). Recomputing it from a delta whose
+ // top-level and sub-agent figures are the same numbers yields
+ // `max(0, 300+50-350) = 0`, blanking the bar the parent's turn just set.
+ const detached = reduce([detachedChatDone, detachedChildSpend]);
+
+ expect(detached?.lastTurnContextUsed).toBe(850);
+ });
+
+ it('records the per-agent breakdown once in both modes', () => {
+ const blocking = reduce([blockingChatDone]);
+ const detached = reduce([detachedChatDone, detachedChildSpend]);
+
+ expect(blocking?.subAgents.researcher).toMatchObject({ inputTokens: 300, runs: 1 });
+ expect(detached?.subAgents.researcher).toMatchObject({ inputTokens: 300, runs: 1 });
+ });
+});
diff --git a/app/src/store/chatRuntimeSlice.ts b/app/src/store/chatRuntimeSlice.ts
index cbec12fa37..141f882f11 100644
--- a/app/src/store/chatRuntimeSlice.ts
+++ b/app/src/store/chatRuntimeSlice.ts
@@ -453,6 +453,12 @@ export function emptySessionTokenUsage(): SessionTokenUsage {
interface ChatTurnUsagePayload {
inputTokens: number;
outputTokens: number;
+ /**
+ * This delta is a detached sub-agent's spend landing on a turn that has
+ * already been counted, not a new turn. Set by the `subagent_completed`
+ * handler; see the `turns` guard in {@link applyTurnUsage}.
+ */
+ subAgentSpendOnly?: boolean;
cachedTokens?: number;
costUsd?: number;
contextWindow?: number;
@@ -477,10 +483,16 @@ function applyTurnUsage(usage: SessionTokenUsage, payload: ChatTurnUsagePayload)
usage.outputTokens += outTok;
usage.cachedTokens += nonNeg(payload.cachedTokens);
usage.costUsd += nonNeg(payload.costUsd);
- usage.turns += 1;
+ // A detached sub-agent's spend arrives on its own `subagent_completed`, after
+ // the parent turn's `chat_done` has already been counted. It is more spend on
+ // the SAME turn, not another turn, so counting it would inflate the turn
+ // count by one per delegation and skew every per-turn average derived from it.
+ if (!payload.subAgentSpendOnly) usage.turns += 1;
usage.lastUpdated = Date.now();
- usage.lastTurnInputTokens = inTok;
- usage.lastTurnOutputTokens = outTok;
+ if (!payload.subAgentSpendOnly) {
+ usage.lastTurnInputTokens = inTok;
+ usage.lastTurnOutputTokens = outTok;
+ }
// Only overwrite the known context window when the turn reported a real value
// (>0); an unknown-window turn leaves the prior value intact.
const ctxWindow = nonNeg(payload.contextWindow);
@@ -508,7 +520,14 @@ function applyTurnUsage(usage: SessionTokenUsage, payload: ChatTurnUsagePayload)
existing.runs += 1;
usage.subAgents[sub.agentId] = existing;
}
- usage.lastTurnContextUsed = Math.max(0, inTok + outTok - subTurnTokens);
+ // The context gauge belongs to the turn, and a late sub-agent delta is not
+ // one: recomputing it here would read `0 + 0 - childTokens` and clamp the
+ // gauge to zero, blanking a bar the parent's own turn had just set
+ // correctly. The parent's value already excludes children by design (#4271),
+ // which is exactly what this delta must not disturb.
+ if (!payload.subAgentSpendOnly) {
+ usage.lastTurnContextUsed = Math.max(0, inTok + outTok - subTurnTokens);
+ }
}
/**
diff --git a/crates/openhuman-core/src/agent/orchestration/ops.rs b/crates/openhuman-core/src/agent/orchestration/ops.rs
index aa6d29aa7f..e388252279 100644
--- a/crates/openhuman-core/src/agent/orchestration/ops.rs
+++ b/crates/openhuman-core/src/agent/orchestration/ops.rs
@@ -511,6 +511,12 @@ impl AgentOrchestrationSession {
iterations: outcome.iterations as u32,
output_chars: outcome.output.chars().count(),
output: outcome.output.clone(),
+ // Detached by construction: this spawn path
+ // builds a fresh `OpenHumanRunContext::new()`
+ // (see `spawn_agent`) and never sets
+ // `parent_subagent_usage`, so the child's
+ // spend is not in the parent turn's totals.
+ usage: Some(outcome.usage),
worktree_path: None,
changed_files: Vec::new(),
dirty_status: None,
diff --git a/crates/openhuman-core/src/agent/orchestration/spawn_parallel_graph/collect.rs b/crates/openhuman-core/src/agent/orchestration/spawn_parallel_graph/collect.rs
index 4ef17d433b..249bbca9ac 100644
--- a/crates/openhuman-core/src/agent/orchestration/spawn_parallel_graph/collect.rs
+++ b/crates/openhuman-core/src/agent/orchestration/spawn_parallel_graph/collect.rs
@@ -109,6 +109,9 @@ pub(crate) async fn project_spawn_parallel_result(
iterations: *iterations,
output_chars: output.as_ref().map(|s| s.chars().count()).unwrap_or(0),
output: output.clone().unwrap_or_default(),
+ // Rebuilt from an already-observed event, which carries no
+ // usage to copy — so this path stays silent. See the field's docs.
+ usage: None,
worktree_path: worktree_path.clone(),
changed_files: changed_files.clone(),
dirty_status: *dirty_status,
diff --git a/crates/openhuman-core/src/agent/orchestration/tools/agent_prepare_context/scout_run.rs b/crates/openhuman-core/src/agent/orchestration/tools/agent_prepare_context/scout_run.rs
index 0d2e10046a..a0cf4517d0 100644
--- a/crates/openhuman-core/src/agent/orchestration/tools/agent_prepare_context/scout_run.rs
+++ b/crates/openhuman-core/src/agent/orchestration/tools/agent_prepare_context/scout_run.rs
@@ -375,6 +375,10 @@ pub(super) async fn run_context_scout_with_catalog_and_workspace(
iterations: outcome.iterations as u32,
output_chars: 0,
output: String::new(),
+ // Not audited for whether this child's spend reached the
+ // parent turn's ledger, so it stays silent: omission adds
+ // nothing, which is the status quo. See the field's docs.
+ usage: None,
worktree_path: None,
changed_files: Vec::new(),
dirty_status: None,
@@ -417,6 +421,10 @@ pub(super) async fn run_context_scout_with_catalog_and_workspace(
iterations: outcome.iterations as u32,
output_chars: bundle.chars().count(),
output: bundle.clone(),
+ // Not audited for whether this child's spend reached the
+ // parent turn's ledger, so it stays silent: omission adds
+ // nothing, which is the status quo. See the field's docs.
+ usage: None,
worktree_path: None,
changed_files: Vec::new(),
dirty_status: None,
@@ -548,6 +556,10 @@ pub(super) async fn run_context_scout_with_catalog_and_workspace(
iterations: outcome.iterations as u32,
output_chars: 0,
output: String::new(),
+ // Not audited for whether this child's spend reached the
+ // parent turn's ledger, so it stays silent: omission adds
+ // nothing, which is the status quo. See the field's docs.
+ usage: None,
worktree_path: None,
changed_files: Vec::new(),
dirty_status: None,
diff --git a/crates/openhuman-core/src/agent/orchestration/tools/continue_subagent.rs b/crates/openhuman-core/src/agent/orchestration/tools/continue_subagent.rs
index 7353a34227..af4eca8e7c 100644
--- a/crates/openhuman-core/src/agent/orchestration/tools/continue_subagent.rs
+++ b/crates/openhuman-core/src/agent/orchestration/tools/continue_subagent.rs
@@ -549,6 +549,10 @@ impl ContinueSubagentTool {
iterations: outcome.iterations as u32,
output_chars: outcome.output.chars().count(),
output: outcome.output.clone(),
+ // Not audited for whether this child's spend reached the
+ // parent turn's ledger, so it stays silent: omission adds
+ // nothing, which is the status quo. See the field's docs.
+ usage: None,
worktree_path: None,
changed_files: Vec::new(),
dirty_status: None,
@@ -589,6 +593,10 @@ impl ContinueSubagentTool {
iterations: outcome.iterations as u32,
output_chars: outcome.output.chars().count(),
output: outcome.output.clone(),
+ // Not audited for whether this child's spend reached the
+ // parent turn's ledger, so it stays silent: omission adds
+ // nothing, which is the status quo. See the field's docs.
+ usage: None,
worktree_path: None,
changed_files: Vec::new(),
dirty_status: None,
diff --git a/crates/openhuman-core/src/agent/orchestration/tools/dispatch.rs b/crates/openhuman-core/src/agent/orchestration/tools/dispatch.rs
index 0757411768..20804df4e0 100644
--- a/crates/openhuman-core/src/agent/orchestration/tools/dispatch.rs
+++ b/crates/openhuman-core/src/agent/orchestration/tools/dispatch.rs
@@ -508,6 +508,10 @@ pub(crate) async fn dispatch_subagent_with_live_parent(
output: outcome.output.clone(),
// Synchronous delegate dispatch has no worktree
// isolation (that is a `spawn_subagent` concept).
+ // Not audited for whether this child's spend reached the
+ // parent turn's ledger, so it stays silent: omission adds
+ // nothing, which is the status quo. See the field's docs.
+ usage: None,
worktree_path: None,
changed_files: Vec::new(),
dirty_status: None,
@@ -572,6 +576,10 @@ pub(crate) async fn dispatch_subagent_with_live_parent(
iterations: outcome.iterations as u32,
output_chars: outcome.output.chars().count(),
output: outcome.output.clone(),
+ // Not audited for whether this child's spend reached the
+ // parent turn's ledger, so it stays silent: omission adds
+ // nothing, which is the status quo. See the field's docs.
+ usage: None,
worktree_path: None,
changed_files: Vec::new(),
dirty_status: None,
diff --git a/crates/openhuman-core/src/agent/orchestration/tools/spawn_async_subagent_execute.rs b/crates/openhuman-core/src/agent/orchestration/tools/spawn_async_subagent_execute.rs
index aa3f3fb549..1808f626f9 100644
--- a/crates/openhuman-core/src/agent/orchestration/tools/spawn_async_subagent_execute.rs
+++ b/crates/openhuman-core/src/agent/orchestration/tools/spawn_async_subagent_execute.rs
@@ -543,6 +543,14 @@ impl SpawnAsyncSubagentTool {
iterations: outcome.iterations as u32,
output_chars: outcome.output.chars().count(),
output: outcome.output.clone(),
+ // Detached by construction:
+ // this tool takes
+ // `detached_child()`, so this
+ // child's spend never reached
+ // the parent turn's ledger and
+ // `chat_done` does not contain
+ // it. See the field's docs.
+ usage: Some(outcome.usage),
worktree_path: None,
changed_files: Vec::new(),
dirty_status: None,
@@ -615,6 +623,14 @@ impl SpawnAsyncSubagentTool {
iterations: outcome.iterations as u32,
output_chars: outcome.output.chars().count(),
output: outcome.output.clone(),
+ // Detached by construction:
+ // this tool takes
+ // `detached_child()`, so this
+ // child's spend never reached
+ // the parent turn's ledger and
+ // `chat_done` does not contain
+ // it. See the field's docs.
+ usage: Some(outcome.usage),
worktree_path: None,
changed_files: Vec::new(),
dirty_status: None,
diff --git a/crates/openhuman-core/src/agent/orchestration/tools/spawn_subagent_tool_impl.rs b/crates/openhuman-core/src/agent/orchestration/tools/spawn_subagent_tool_impl.rs
index 06beb0654e..dec4a6bf37 100644
--- a/crates/openhuman-core/src/agent/orchestration/tools/spawn_subagent_tool_impl.rs
+++ b/crates/openhuman-core/src/agent/orchestration/tools/spawn_subagent_tool_impl.rs
@@ -626,6 +626,10 @@ impl SpawnSubagentTool {
iterations: outcome.iterations as u32,
output_chars: outcome.output.chars().count(),
output: outcome.output.clone(),
+ // BLOCKING spawn: this child's usage DID reach the parent's
+ // ledger, so `chat_done` already carries its tokens AND its
+ // cost. Populating here would double both. See the field's docs.
+ usage: None,
worktree_path: None,
changed_files: Vec::new(),
dirty_status: None,
@@ -700,6 +704,10 @@ impl SpawnSubagentTool {
iterations: outcome.iterations as u32,
output_chars: outcome.output.chars().count(),
output: outcome.output.clone(),
+ // BLOCKING spawn: this child's usage DID reach the parent's
+ // ledger, so `chat_done` already carries its tokens AND its
+ // cost. Populating here would double both. See the field's docs.
+ usage: None,
worktree_path: None,
changed_files: Vec::new(),
dirty_status: None,
diff --git a/crates/openhuman-core/src/agent/progress.rs b/crates/openhuman-core/src/agent/progress.rs
index 500882c6af..014f0c760b 100644
--- a/crates/openhuman-core/src/agent/progress.rs
+++ b/crates/openhuman-core/src/agent/progress.rs
@@ -120,6 +120,26 @@ pub enum AgentProgress {
iterations: u32,
/// Character length of the sub-agent's final assistant text.
output_chars: usize,
+ /// This child's own token + cost totals, **and only when they did not
+ /// already reach the parent turn's ledger**.
+ ///
+ /// ## Populate this ONLY when the spend is not already counted
+ ///
+ /// A blocking spawn records into `parent_subagent_usage`, and
+ /// `holistic_last_turn_usage` then folds those child tokens AND
+ /// `charged_amount_usd` into the figures `chat_done` carries. A
+ /// detached spawn does not: `detached_child()` sets
+ /// `parent_subagent_usage` to `None` and swaps in a fresh ledger, so
+ /// the entry lands somewhere the parent never reads.
+ ///
+ /// The consumer adds whatever arrives here, unconditionally. That is
+ /// what makes the omission safe — a site that does not populate this
+ /// contributes nothing, which is the status quo — and it is also why
+ /// populating it for a child that DID reach the parent ledger silently
+ /// doubles the user's reported spend, money included, with no test
+ /// failing. **Check the ledger, not the spawn mode**: spawn mode is a
+ /// proxy and proxies drift.
+ usage: Option<crate::agent::subagent_host::SubagentUsage>,
/// The sub-agent's full final assistant text. Trace exporters record
/// this (truncated + content-gated) as the subagent span's output.
output: String,
diff --git a/crates/openhuman-core/src/agent/progress_tracing/journal_projection.rs b/crates/openhuman-core/src/agent/progress_tracing/journal_projection.rs
index 4bb187631e..c639cdd4d7 100644
--- a/crates/openhuman-core/src/agent/progress_tracing/journal_projection.rs
+++ b/crates/openhuman-core/src/agent/progress_tracing/journal_projection.rs
@@ -510,6 +510,9 @@ fn observation_to_progress(obs: &AgentObservation, state: &mut ReplayState) -> V
iterations: scope.iteration,
output_chars: 0,
output: String::new(),
+ // Projection rebuild, not an originating emit; it has no
+ // outcome to read usage from. See the field's docs.
+ usage: None,
worktree_path: None,
changed_files: Vec::new(),
dirty_status: None,
diff --git a/crates/openhuman-core/src/core/socketio.rs b/crates/openhuman-core/src/core/socketio.rs
index d729f82bd6..bab189d3e1 100644
--- a/crates/openhuman-core/src/core/socketio.rs
+++ b/crates/openhuman-core/src/core/socketio.rs
@@ -414,6 +414,23 @@ pub struct SubagentProgressDetail {
/// Elapsed wall-clock for the call/run in milliseconds.
#[serde(skip_serializing_if = "Option::is_none")]
pub elapsed_ms: Option<u64>,
+ /// This child's own token + cost spend (on `subagent_completed`), and
+ /// **only when it is not already inside the parent turn's totals**.
+ ///
+ /// The consumer adds these unconditionally, so an emit site that leaves
+ /// them absent contributes nothing — the safe direction. Populating them
+ /// for a child whose usage DID reach `parent_subagent_usage` silently
+ /// doubles the user's reported tokens and money, because
+ /// `holistic_last_turn_usage` already folded both into `chat_done`. See
+ /// `AgentProgress::SubagentCompleted::usage`.
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub input_tokens: Option<u64>,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub output_tokens: Option<u64>,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub cached_input_tokens: Option<u64>,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub cost_usd: Option<f64>,
/// Total iterations the sub-agent used (on `subagent_completed`).
#[serde(skip_serializing_if = "Option::is_none")]
pub iterations: Option<u32>,
diff --git a/crates/openhuman-core/src/web_chat/progress_bridge.rs b/crates/openhuman-core/src/web_chat/progress_bridge.rs
index d7fdfb5903..28afa56b35 100644
--- a/crates/openhuman-core/src/web_chat/progress_bridge.rs
+++ b/crates/openhuman-core/src/web_chat/progress_bridge.rs
@@ -833,6 +833,7 @@ pub(crate) fn spawn_progress_bridge(
elapsed_ms,
iterations,
output_chars,
+ usage,
worktree_path,
changed_files,
dirty_status,
@@ -904,6 +905,16 @@ pub(crate) fn spawn_progress_bridge(
elapsed_ms: Some(elapsed_ms),
iterations: Some(iterations),
output_chars: Some(output_chars as u64),
+ // Present only when this child's spend is NOT
+ // already in the parent turn's totals — the
+ // emitting site decides, because only it can
+ // see whether the usage reached
+ // `parent_subagent_usage`. Absent is the safe
+ // default and means "add nothing".
+ input_tokens: usage.as_ref().map(|u| u.input_tokens),
+ output_tokens: usage.as_ref().map(|u| u.output_tokens),
+ cached_input_tokens: usage.as_ref().map(|u| u.cached_input_tokens),
+ cost_usd: usage.as_ref().map(|u| u.charged_amount_usd),
// Worktree isolation metadata (#3376) — drives the
// inline subagent worktree row's open/diff/remove
// actions. All `None`/absent for non-isolated workers.