Skip to content

fix(claude_agent_sdk): attribute usage to model call spans#572

Closed
Abhijeet Prasad (AbhiPrasad) wants to merge 1 commit into
mainfrom
abhi-fix-claude-agent-per-span-usage
Closed

fix(claude_agent_sdk): attribute usage to model call spans#572
Abhijeet Prasad (AbhiPrasad) wants to merge 1 commit into
mainfrom
abhi-fix-claude-agent-per-span-usage

Conversation

@AbhiPrasad

@AbhiPrasad Abhijeet Prasad (AbhiPrasad) commented Jul 13, 2026

Copy link
Copy Markdown
Member

Supercedes #565
resolves #564
resolves https://linear.app/braintrustdata/issue/SDK-52/claude-agent-sdk-handle-result-logs-main-agent-usage-under-counts

AI Summary:

Claude Agent runs may make several orchestrator and delegated model calls, but cumulative result usage was being assigned to one final span. This hid delegated usage and could price tokens using the wrong model.

Before:

Claude Agent [TASK]
├── root LLM [opus] metrics: none
├── Agent [TOOL]
│ └── subagent [TASK]
│ └── LLM [haiku] metrics: none
└── final LLM [opus] metrics: cumulative usage for every call

After:

Claude Agent [TASK]
├── root LLM [opus] metrics: root call usage
├── Agent [TOOL]
│ └── subagent [TASK]
│ └── LLM [haiku] metrics: delegated call usage
└── final LLM [opus] metrics: reconciled final-call remainder

Each LLM span now keeps its own metadata.model and token/cache metrics, which makes calculations for everything correct.

Claude Agent runs may make several orchestrator and delegated model calls, but cumulative result usage was being assigned to one final span. This hid delegated usage and could price tokens using the wrong model.

Before:

  Claude Agent [TASK]
  ├── root LLM [opus]       metrics: none
  ├── Agent [TOOL]
  │   └── subagent [TASK]
  │       └── LLM [haiku]   metrics: none
  └── final LLM [opus]      metrics: cumulative usage for every call

After:

  Claude Agent [TASK]
  ├── root LLM [opus]       metrics: root call usage
  ├── Agent [TOOL]
  │   └── subagent [TASK]
  │       └── LLM [haiku]   metrics: delegated call usage
  └── final LLM [opus]      metrics: reconciled final-call remainder

Each LLM span now keeps its own metadata.model and token/cache metrics, including cache-read, cache-creation, 5m/1h cache TTL, and server-tool usage. Repeated cumulative snapshots are converted to deltas so trace totals do not double-count them. Result usage also provides a safe fallback when assistant usage is unavailable.

Result total_cost_usd remains request metadata on the root task rather than a parent-owned estimated cost. This keeps trace totals and per-model cost estimates accurate across root, subagent, concurrent-agent, and older SDK flows.

Extend cassette-backed coverage for multi-turn and concurrent-agent requests on both supported Claude Agent SDK versions.
@AbhiPrasad

Copy link
Copy Markdown
Member Author

actually this approach is problematic as well. Going back to the drawing board.

@AbhiPrasad Abhijeet Prasad (AbhiPrasad) deleted the abhi-fix-claude-agent-per-span-usage branch July 13, 2026 20:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Claude Agent SDK: _handle_result logs main-agent usage, under-counts turns with subagents

1 participant