Skip to content

Commit d968d7f

Browse files
Enhance Tool Behavior Documentation for Developer Clarity With Examples (#1286)
Co-authored-by: Kazuhiro Sera <[email protected]>
1 parent 4cb07d5 commit d968d7f

File tree

1 file changed

+96
-1
lines changed

1 file changed

+96
-1
lines changed

docs/agents.md

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,103 @@ Supplying a list of tools doesn't always mean the LLM will use a tool. You can f
143143
3. `none`, which requires the LLM to _not_ use a tool.
144144
4. Setting a specific string e.g. `my_tool`, which requires the LLM to use that specific tool.
145145

146+
```python
147+
from agents import Agent, Runner, function_tool, ModelSettings
148+
149+
@function_tool
150+
def get_weather(city: str) -> str:
151+
"""Returns weather info for the specified city."""
152+
return f"The weather in {city} is sunny"
153+
154+
agent = Agent(
155+
name="Weather Agent",
156+
instructions="Retrieve weather details.",
157+
tools=[get_weather],
158+
model_settings=ModelSettings(tool_choice="get_weather")
159+
)
160+
```
161+
162+
## Tool Use Behavior
163+
164+
The `tool_use_behavior` parameter in the `Agent` configuration controls how tool outputs are handled:
165+
- `"run_llm_again"`: The default. Tools are run, and the LLM processes the results to produce a final response.
166+
- `"stop_on_first_tool"`: The output of the first tool call is used as the final response, without further LLM processing.
167+
168+
```python
169+
from agents import Agent, Runner, function_tool, ModelSettings
170+
171+
@function_tool
172+
def get_weather(city: str) -> str:
173+
"""Returns weather info for the specified city."""
174+
return f"The weather in {city} is sunny"
175+
176+
agent = Agent(
177+
name="Weather Agent",
178+
instructions="Retrieve weather details.",
179+
tools=[get_weather],
180+
tool_use_behavior="stop_on_first_tool"
181+
)
182+
```
183+
184+
- `StopAtTools(stop_at_tool_names=[...])`: Stops if any specified tool is called, using its output as the final response.
185+
```python
186+
from agents import Agent, Runner, function_tool
187+
from agents.agent import StopAtTools
188+
189+
@function_tool
190+
def get_weather(city: str) -> str:
191+
"""Returns weather info for the specified city."""
192+
return f"The weather in {city} is sunny"
193+
194+
@function_tool
195+
def sum_numbers(a: int, b: int) -> int:
196+
"""Adds two numbers."""
197+
return a + b
198+
199+
agent = Agent(
200+
name="Stop At Stock Agent",
201+
instructions="Get weather or sum numbers.",
202+
tools=[get_weather, sum_numbers],
203+
tool_use_behavior=StopAtTools(stop_at_tool_names=["get_weather"])
204+
)
205+
```
206+
- `ToolsToFinalOutputFunction`: A custom function that processes tool results and decides whether to stop or continue with the LLM.
207+
208+
```python
209+
from agents import Agent, Runner, function_tool, FunctionToolResult, RunContextWrapper
210+
from agents.agent import ToolsToFinalOutputResult
211+
from typing import List, Any
212+
213+
@function_tool
214+
def get_weather(city: str) -> str:
215+
"""Returns weather info for the specified city."""
216+
return f"The weather in {city} is sunny"
217+
218+
def custom_tool_handler(
219+
context: RunContextWrapper[Any],
220+
tool_results: List[FunctionToolResult]
221+
) -> ToolsToFinalOutputResult:
222+
"""Processes tool results to decide final output."""
223+
for result in tool_results:
224+
if result.output and "sunny" in result.output:
225+
return ToolsToFinalOutputResult(
226+
is_final_output=True,
227+
final_output=f"Final weather: {result.output}"
228+
)
229+
return ToolsToFinalOutputResult(
230+
is_final_output=False,
231+
final_output=None
232+
)
233+
234+
agent = Agent(
235+
name="Weather Agent",
236+
instructions="Retrieve weather details.",
237+
tools=[get_weather],
238+
tool_use_behavior=custom_tool_handler
239+
)
240+
```
241+
146242
!!! note
147243

148244
To prevent infinite loops, the framework automatically resets `tool_choice` to "auto" after a tool call. This behavior is configurable via [`agent.reset_tool_choice`][agents.agent.Agent.reset_tool_choice]. The infinite loop is because tool results are sent to the LLM, which then generates another tool call because of `tool_choice`, ad infinitum.
149245

150-
If you want the Agent to completely stop after a tool call (rather than continuing with auto mode), you can set [`Agent.tool_use_behavior="stop_on_first_tool"`] which will directly use the tool output as the final response without further LLM processing.

0 commit comments

Comments
 (0)