claude-code - 💡(How to fix) Fix SubagentStop hook payload should include token usage data [2 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
anthropics/claude-code#47045Fetched 2026-04-13 05:42:56
View on GitHub
Comments
2
Participants
2
Timeline
5
Reactions
0
Timeline (top)
labeled ×3commented ×2

The SubagentStop hook event fires for /btw (aside question) subagents but the payload does not include any token usage data. The transcript file is also not written (skipTranscript:true in runForkedAgent), leaving no programmatic path to capture per-call token counts for /btw invocations.

Root Cause

The SubagentStop hook event fires for /btw (aside question) subagents but the payload does not include any token usage data. The transcript file is also not written (skipTranscript:true in runForkedAgent), leaving no programmatic path to capture per-call token counts for /btw invocations.

Fix Action

Workaround

We currently use OTel (OTEL_LOGS_EXPORTER=otlp) pointed at a local HTTP receiver to capture /btw token data, matching events by session.id + absence of prompt.id. This works but requires running additional infrastructure.

Code Example

{
  "session_id": "...",
  "agent_id": "...",
  "agent_type": "",
  "agent_transcript_path": "...agent-xxx.jsonl",
  "last_assistant_message": "...",
  "hook_event_name": "SubagentStop",
  "stop_hook_active": false
}

---

Forked agent [side_question] finished: 4 messages,
  totalUsage: input=253 output=11 cacheRead=17873 cacheCreate=7

---

{
  "input_tokens": 253,
  "output_tokens": 11,
  "cache_read_input_tokens": 17873,
  "cache_creation_input_tokens": 7,
  "model": "claude-opus-4-6",
  "cost_usd": 0.027707
}
RAW_BUFFERClick to expand / collapse

Summary

The SubagentStop hook event fires for /btw (aside question) subagents but the payload does not include any token usage data. The transcript file is also not written (skipTranscript:true in runForkedAgent), leaving no programmatic path to capture per-call token counts for /btw invocations.

Current payload

{
  "session_id": "...",
  "agent_id": "...",
  "agent_type": "",
  "agent_transcript_path": "...agent-xxx.jsonl",
  "last_assistant_message": "...",
  "hook_event_name": "SubagentStop",
  "stop_hook_active": false
}

What's missing

Token usage fields — the same data that appears in the debug log:

Forked agent [side_question] finished: 4 messages,
  totalUsage: input=253 output=11 cacheRead=17873 cacheCreate=7

Evidence the data exists internally

  • --debug "api" logs the full totalUsage for /btw calls (confirmed CC v2.1.104)
  • OpenTelemetry claude_code.api_request events include input_tokens, output_tokens, cache_read_tokens, cache_creation_tokens, cost_usd for every API call including /btw
  • The data is available at the point SubagentStop fires — it just isn't included in the hook payload

Requested change

Add token usage fields to the SubagentStop payload:

{
  "input_tokens": 253,
  "output_tokens": 11,
  "cache_read_input_tokens": 17873,
  "cache_creation_input_tokens": 7,
  "model": "claude-opus-4-6",
  "cost_usd": 0.027707
}

This would also benefit normal subagent hooks — currently SubagentStop requires reading the transcript JSONL to get token data, which adds I/O and complexity.

Context

Building a FinOps cost monitoring tool that tracks per-session and per-subagent costs via Stop/SubagentStop hooks. /btw costs (~$0.01-0.04 per invocation) are currently invisible without an OTel workaround.

Workaround

We currently use OTel (OTEL_LOGS_EXPORTER=otlp) pointed at a local HTTP receiver to capture /btw token data, matching events by session.id + absence of prompt.id. This works but requires running additional infrastructure.

extent analysis

TL;DR

Add token usage fields to the SubagentStop payload to capture per-call token counts for /btw invocations.

Guidance

  • Verify that the SubagentStop hook event is firing correctly for /btw subagents and that the payload is being received as expected.
  • Investigate modifying the runForkedAgent function to include token usage data in the SubagentStop payload, potentially by removing the skipTranscript:true flag or adding a separate data collection mechanism.
  • Consider using the OpenTelemetry claude_code.api_request events as a temporary workaround to capture token usage data, as described in the issue.
  • Review the debug logs and OpenTelemetry events to confirm that the token usage data is available internally and can be accessed at the point where the SubagentStop hook fires.

Example

No code snippet is provided, as the issue does not include sufficient information to generate a specific example.

Notes

The issue lacks information about the specific implementation details of the SubagentStop hook and the runForkedAgent function, which would be necessary to provide a more detailed solution.

Recommendation

Apply workaround: Use OpenTelemetry claude_code.api_request events to capture token usage data, as this provides a temporary solution to the problem and allows for the collection of token usage data without modifying the SubagentStop payload.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

claude-code - 💡(How to fix) Fix SubagentStop hook payload should include token usage data [2 comments, 2 participants]