Skip to content

Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.

fix(claude_agent_sdk): report subagent total_tokens as total, not input (#1463)

Tai An5112a1d6b4c0
diff --git a/sdk/python/src/openlit/instrumentation/claude_agent_sdk/claude_agent_sdk.py b/sdk/python/src/openlit/instrumentation/claude_agent_sdk/claude_agent_sdk.py
index fa30116a..c0d619ad 100644
--- a/sdk/python/src/openlit/instrumentation/claude_agent_sdk/claude_agent_sdk.py
+++ b/sdk/python/src/openlit/instrumentation/claude_agent_sdk/claude_agent_sdk.py
@@ -177,7 +177,7 @@ class _SubagentSpanTracker:
if total_tokens is not None:
try:
span.set_attribute(
- SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS,
+ SemanticConvention.GEN_AI_USAGE_TOTAL_TOKENS,
int(total_tokens),
)
except (TypeError, ValueError):
diff --git a/sdk/python/tests/test_claude_agent_sdk.py b/sdk/python/tests/test_claude_agent_sdk.py
index 0e08d545..8343c95b 100644
--- a/sdk/python/tests/test_claude_agent_sdk.py
+++ b/sdk/python/tests/test_claude_agent_sdk.py
@@ -1281,6 +1281,20 @@ class TestSubagentSpanTracker:
assert "task_002" not in tracker._in_flight
+ def test_end_subagent_reports_total_tokens_as_total(self):
+ tracker = self._make_tracker()
+ tracker.start_subagent("task_003", "agent")
+ span = tracker._in_flight["task_003"]
+
+ usage = {"total_tokens": 1000, "tool_uses": 3, "duration_ms": 5000}
+ tracker.end_subagent("task_003", usage=usage)
+
+ span.set_attribute.assert_any_call(
+ SemanticConvention.GEN_AI_USAGE_TOTAL_TOKENS, 1000
+ )
+ recorded = {call.args[0] for call in span.set_attribute.call_args_list}
+ assert SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS not in recorded
+
def test_end_subagent_with_error(self):
tracker = self._make_tracker()
tracker.start_subagent("task_err", "agent")