Skip to content

Commit 2ae2d10

Browse files
Docs: Improve and clarify tool behavior definitions Enhances the "Tool Behavior Definitions" documentation to provide clearer explanations and more robust examples for agent tool usage. Key improvements include: - Explicitly defining import paths for StopAtTools and ToolsToFinalOutputFunction. - Providing comprehensive and corrected code examples for all tool_choice and tool_use_behavior configurations, including "stop_on_first_tool", StopAtTools, and the usage of ToolsToFinalOutputFunction. - Ensuring proper Markdown formatting for code blocks and notes to enhance readability and accuracy. This update aims to significantly reduce ambiguity and improve the developer experience by offering ready-to-use and well-explained code snippets.
1 parent 656ee0c commit 2ae2d10

File tree

1 file changed

+100
-1
lines changed

1 file changed

+100
-1
lines changed

docs/agents.md

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

145+
```python
146+
from agents import Agent, Runner, function_tool, ModelSettings
147+
148+
@function_tool
149+
def get_stock_price(ticker: str) -> str:
150+
"""Fetches the stock price for a given ticker symbol."""
151+
prices = {"AAPL": "$150.00", "GOOGL": "$2750.00"}
152+
return prices.get(ticker, "Stock not found")
153+
154+
agent = Agent(
155+
name="Stock Price Agent",
156+
instructions="Retrieve the stock price if relevant, otherwise respond directly.",
157+
tools=[get_stock_price],
158+
model_settings=ModelSettings(tool_choice="get_stock_price")
159+
)
160+
161+
```
162+
163+
## Tool Use Behavior
164+
165+
The `tool_use_behavior` parameter in the `Agent` configuration controls how tool outputs are handled:
166+
- `"run_llm_again"`: The default. Tools are run, and the LLM processes the results to produce a final response.
167+
- `"stop_on_first_tool"`: The output of the first tool call is used as the final response, without further LLM processing.
168+
169+
```python
170+
from agents import Agent, Runner, function_tool, ModelSettings
171+
172+
@function_tool
173+
def get_stock_price(ticker: str) -> str:
174+
"""Fetches the stock price for a given ticker symbol."""
175+
prices = {"AAPL": "$150.00", "GOOGL": "$2750.00"}
176+
return prices.get(ticker, "Stock not found")
177+
178+
# Create the agent
179+
agent = Agent(
180+
name="Stock Price Agent",
181+
instructions="Retrieve the stock price.",
182+
tools=[get_stock_price],
183+
tool_use_behavior="stop_on_first_tool"
184+
)
185+
```
186+
187+
- `StopAtTools(stop_at_tool_names=[...])`: Stops if any specified tool is called, using its output as the final response.
188+
```python
189+
from agents import Agent, Runner, function_tool
190+
from agents.agent import StopAtTools
191+
192+
@function_tool
193+
def get_stock_price(ticker: str) -> str:
194+
"""Fetches the stock price for a given ticker symbol."""
195+
prices = {"AAPL": "$150.00", "GOOGL": "$2750.00"}
196+
return prices.get(ticker, "Stock not found")
197+
198+
agent = Agent(
199+
name="Stop At Stock Agent",
200+
instructions="Get stock price and stop.",
201+
tools=[get_stock_price],
202+
tool_use_behavior=StopAtTools(stop_at_tool_names=["get_stock_price"])
203+
)
204+
```
205+
- `ToolsToFinalOutputFunction`: A custom function that processes tool results and decides whether to stop or continue with the LLM.
206+
207+
```python
208+
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_product_price(product_id: str) -> str:
215+
"""Fetches the price for a given product ID."""
216+
prices = {"P101": "$25.00", "P102": "$49.99"}
217+
return prices.get(product_id, "Product not found")
218+
219+
def custom_tool_handler(
220+
context: RunContextWrapper[Any],
221+
tool_results: List[FunctionToolResult]
222+
) -> ToolsToFinalOutputResult:
223+
"""Processes tool results to decide final output."""
224+
for result in tool_results:
225+
if result.output and "$25.00" in result.output:
226+
return ToolsToFinalOutputResult(
227+
is_final_output=True,
228+
final_output=f"Final result: {result.output}"
229+
)
230+
return ToolsToFinalOutputResult(
231+
is_final_output=False,
232+
final_output=None
233+
)
234+
235+
# Create the agent
236+
agent = Agent(
237+
name="Product Price Agent",
238+
instructions="Retrieve product prices and format them.",
239+
tools=[get_product_price],
240+
tool_use_behavior=custom_tool_handler
241+
)
242+
243+
```
244+
145245
!!! note
146246

147247
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.
148248

149-
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)