You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(agent): recover from unknown tool calls via opt-in handler
When the LLM hallucinates a tool name not registered on the agent,
turn_resolution previously raised ModelBehaviorError and crashed the
entire run. Add an opt-in Agent.unknown_tool_behavior field with
"raise" (default, preserves existing behavior) and "respond" (append a
synthetic tool-call output naming the available tools and let the run
continue so the model can recover). Refs #325.
Copy file name to clipboardExpand all lines: docs/agents.md
+16Lines changed: 16 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -43,6 +43,7 @@ The most common properties of an agent are:
43
43
|`hooks`| no | Agent-scoped lifecycle callbacks. See [Lifecycle events (hooks)](#lifecycle-events-hooks). |
44
44
|`tool_use_behavior`| no | Control whether tool results loop back to the model or end the run. See [Tool use behavior](#tool-use-behavior). |
45
45
|`reset_tool_choice`| no | Reset `tool_choice` after a tool call (default: `True`) to avoid tool-use loops. See [Forcing tool use](#forcing-tool-use). |
46
+
|`unknown_tool_behavior`| no | What to do when the model calls a tool that is not registered (default: `"raise"`). Set to `"respond"` to feed an error tool output back to the LLM and let the run continue. See [Recovering from unknown tool calls](#recovering-from-unknown-tool-calls). |
46
47
47
48
```python
48
49
from agents import Agent, ModelSettings, function_tool
@@ -423,3 +424,18 @@ agent = Agent(
423
424
!!! note
424
425
425
426
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.
427
+
428
+
## Recovering from unknown tool calls
429
+
430
+
By default, the SDK raises [`ModelBehaviorError`][agents.exceptions.ModelBehaviorError] if the model hallucinates a tool that the agent does not expose. This is the safest behavior for development, but it can crash a long-running agent run when the model occasionally invents tool names.
431
+
432
+
Set `unknown_tool_behavior="respond"` on the agent to recover instead. When the model calls an unknown tool, the SDK appends a synthetic tool output describing the error and the list of available tools, and lets the agent continue. The LLM sees the error on the next turn and can pick a real tool.
0 commit comments