Skip to content

Commit 613476f

Browse files
committed
Add tool call arguments in ToolContext for RunHooks
1 parent 827af41 commit 613476f

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

examples/basic/lifecycle_example.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ async def on_agent_end(self, context: RunContextWrapper, agent: Agent, output: A
4646
async def on_tool_start(self, context: RunContextWrapper, agent: Agent, tool: Tool) -> None:
4747
self.event_counter += 1
4848
print(
49-
f"### {self.event_counter}: Tool {tool.name} started. Usage: {self._usage_to_str(context.usage)}"
49+
f"### {self.event_counter}: Tool {tool.name} started. name={context.tool_name}, call_id={context.tool_call_id}, args={context.tool_arguments}. Usage: {self._usage_to_str(context.usage)}" # type: ignore[attr-defined]
5050
)
5151

5252
async def on_tool_end(
5353
self, context: RunContextWrapper, agent: Agent, tool: Tool, result: str
5454
) -> None:
5555
self.event_counter += 1
5656
print(
57-
f"### {self.event_counter}: Tool {tool.name} ended with result {result}. Usage: {self._usage_to_str(context.usage)}"
57+
f"### {self.event_counter}: Tool {tool.name} finished. result={result}, name={context.tool_name}, call_id={context.tool_call_id}, args={context.tool_arguments}. Usage: {self._usage_to_str(context.usage)}" # type: ignore[attr-defined]
5858
)
5959

6060
async def on_handoff(
@@ -128,19 +128,19 @@ async def main() -> None:
128128
### 1: Agent Start Agent started. Usage: 0 requests, 0 input tokens, 0 output tokens, 0 total tokens
129129
### 2: LLM started. Usage: 0 requests, 0 input tokens, 0 output tokens, 0 total tokens
130130
### 3: LLM ended. Usage: 1 requests, 143 input tokens, 15 output tokens, 158 total tokens
131-
### 4: Tool random_number started. Usage: 1 requests, 143 input tokens, 15 output tokens, 158 total tokens
132-
### 5: Tool random_number ended with result 69. Usage: 1 requests, 143 input tokens, 15 output tokens, 158 total tokens
131+
### 4: Tool random_number started. name=random_number, call_id=call_IujmDZYiM800H0hy7v17VTS0, args={"max":250}. Usage: 1 requests, 143 input tokens, 15 output tokens, 158 total tokens
132+
### 5: Tool random_number finished. result=107, name=random_number, call_id=call_IujmDZYiM800H0hy7v17VTS0, args={"max":250}. Usage: 1 requests, 143 input tokens, 15 output tokens, 158 total tokens
133133
### 6: LLM started. Usage: 1 requests, 143 input tokens, 15 output tokens, 158 total tokens
134134
### 7: LLM ended. Usage: 2 requests, 310 input tokens, 29 output tokens, 339 total tokens
135135
### 8: Handoff from Start Agent to Multiply Agent. Usage: 2 requests, 310 input tokens, 29 output tokens, 339 total tokens
136136
### 9: Agent Multiply Agent started. Usage: 2 requests, 310 input tokens, 29 output tokens, 339 total tokens
137137
### 10: LLM started. Usage: 2 requests, 310 input tokens, 29 output tokens, 339 total tokens
138138
### 11: LLM ended. Usage: 3 requests, 472 input tokens, 45 output tokens, 517 total tokens
139-
### 12: Tool multiply_by_two started. Usage: 3 requests, 472 input tokens, 45 output tokens, 517 total tokens
140-
### 13: Tool multiply_by_two ended with result 138. Usage: 3 requests, 472 input tokens, 45 output tokens, 517 total tokens
139+
### 12: Tool multiply_by_two started. name=multiply_by_two, call_id=call_KhHvTfsgaosZsfi741QvzgYw, args={"x":107}. Usage: 3 requests, 472 input tokens, 45 output tokens, 517 total tokens
140+
### 13: Tool multiply_by_two finished. result=214, name=multiply_by_two, call_id=call_KhHvTfsgaosZsfi741QvzgYw, args={"x":107}. Usage: 3 requests, 472 input tokens, 45 output tokens, 517 total tokens
141141
### 14: LLM started. Usage: 3 requests, 472 input tokens, 45 output tokens, 517 total tokens
142142
### 15: LLM ended. Usage: 4 requests, 660 input tokens, 56 output tokens, 716 total tokens
143-
### 16: Agent Multiply Agent ended with output number=138. Usage: 4 requests, 660 input tokens, 56 output tokens, 716 total tokens
143+
### 16: Agent Multiply Agent ended with output number=214. Usage: 4 requests, 660 input tokens, 56 output tokens, 716 total tokens
144144
Done!
145145
146146
"""

src/agents/tool_context.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ class ToolContext(RunContextWrapper[TContext]):
2424
tool_call_id: str = field(default_factory=_assert_must_pass_tool_call_id)
2525
"""The ID of the tool call."""
2626

27+
tool_arguments: Optional[str] = None
28+
"""The raw arguments string of the tool call."""
29+
2730
@classmethod
2831
def from_agent_context(
2932
cls,
@@ -39,4 +42,8 @@ def from_agent_context(
3942
f.name: getattr(context, f.name) for f in fields(RunContextWrapper) if f.init
4043
}
4144
tool_name = tool_call.name if tool_call is not None else _assert_must_pass_tool_name()
42-
return cls(tool_name=tool_name, tool_call_id=tool_call_id, **base_values)
45+
tool_args = tool_call.arguments if tool_call is not None else None
46+
47+
return cls(
48+
tool_name=tool_name, tool_call_id=tool_call_id, tool_arguments=tool_args, **base_values
49+
)

0 commit comments

Comments
 (0)