# Useful but Little-Known Features of OpenAI Agent SDK
๐ Want the tool's output to be the final answer, but the model keeps adding unnecessary summaries?
With `tool_use_behavior`, you can make tool output the final result directly, stop at specific tools, or customize output handling with fine-grained control.
๐ Title: tool_use_behavior
๐ URL:
๐งฉ Overview
`tool_use_behavior` controls what happens after an agent calls a tool. Setting `"stop_on_first_tool"` makes the first tool's output the final result. `StopAtTools(stop_at_tool_names=[...])` limits this behavior to specific tools. You can also pass a custom function that returns `ToolsToFinalOutputResult(is_final_output=True, final_output=...)` to transform the output before finalizing it.
๐ How to use it
```python
from agents import Agent, StopAtTools, ToolsToFinalOutputResult
# First tool output becomes the final result
agent_direct = Agent(
name="direct",
tools=[search_tool],
tool_use_behavior="stop_on_first_tool",
)
# Stop only on specific tools
agent_selective = Agent(
name="selective",
tools=[search_tool, format_tool, send_tool],
tool_use_behavior=StopAtTools(
stop_at_tool_names=["format_tool"]
),
)
# Custom function for output control
def custom_behavior(context, tool_results):
result = tool_results[0]
if result.tool_name == "get_answer":
return ToolsToFinalOutputResult(
is_final_output=True,
final_output=f"Answer: {result.output}",
)
return ToolsToFinalOutputResult(is_final_output=False)
agent_custom = Agent(
name="custom",
tools=[get_answer, search_tool],
tool_use_behavior=custom_behavior,
)
```
๐ Building it into production
ใปUse `"stop_on_first_tool"` for proxy-style agents that return API results directly
ใปFinalize output at specific pipeline steps to reduce unnecessary LLM calls
ใปStandardize output format with custom functions for stable downstream integration
ใปPass structured data (JSON, etc.) directly to subsequent processing without model summarization
๐ก Use cases
๐ Proxy agents returning API results as-is
๐ Direct output of database query results
๐ Output finalization at pipeline intermediate steps
๐ฏ Selective control to use only specific tool results as final output
โ ๏ธ Watch out
With `"stop_on_first_tool"`, the model won't interpret or supplement results. If user-friendly output is needed, use a custom function. Also verify behavior when multiple tools are called in parallel.
โจ With `tool_use_behavior`, take agent output from "model decides" to "by design."
#
OpenAIAgentSDK# #
AIAgent#