Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/ai/agents/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -631,9 +631,11 @@ def kwargs(self) -> dict[str, Any]:

async def __call__(self, **overrides: Any) -> events_.ToolCallResult:
"""Execute the tool and return a :class:`ToolCallResult`."""
spec = self._tool.tool.spec
data = telemetry.ToolExecutionSpanData(
tool_name=self._part.tool_name,
tool_call_id=self._part.tool_call_id,
tool_description=spec.description if spec is not None else None,
)

# Replay-from-deferred-hook short-circuit: if a prior run already
Expand Down Expand Up @@ -1453,6 +1455,7 @@ async def _stream() -> AsyncGenerator[events_.AgentEvent]:
params=params,
)
) as sp:
initial_count = len(context.messages)
mw_token: middleware_.Token | None = None
if _middleware is not None:
parent = middleware_.get()
Expand Down Expand Up @@ -1485,6 +1488,15 @@ async def _stream() -> AsyncGenerator[events_.AgentEvent]:
),
None,
)
total: types.usage.Usage | None = None
for msg in context.messages[initial_count:]:
if msg.usage is not None:
total = (
msg.usage
if total is None
else total + msg.usage
)
sp.data.usage = total

async with (
hooks_.use_hook_registry(registry),
Expand Down
Loading
Loading