@@ -147,6 +147,7 @@ class Agent(AbstractAgent[AgentDepsT, OutputDataT]):
147147 _prepare_output_tools : ToolsPrepareFunc [AgentDepsT ] | None = dataclasses .field (repr = False )
148148 _max_result_retries : int = dataclasses .field (repr = False )
149149 _max_tool_retries : int = dataclasses .field (repr = False )
150+ _tool_timeout : float | None = dataclasses .field (repr = False )
150151 _validation_context : Any | Callable [[RunContext [AgentDepsT ]], Any ] = dataclasses .field (repr = False )
151152
152153 _event_stream_handler : EventStreamHandler [AgentDepsT ] | None = dataclasses .field (repr = False )
@@ -179,6 +180,7 @@ def __init__(
179180 instrument : InstrumentationSettings | bool | None = None ,
180181 history_processors : Sequence [HistoryProcessor [AgentDepsT ]] | None = None ,
181182 event_stream_handler : EventStreamHandler [AgentDepsT ] | None = None ,
183+ tool_timeout : float | None = None ,
182184 ) -> None : ...
183185
184186 @overload
@@ -206,6 +208,7 @@ def __init__(
206208 instrument : InstrumentationSettings | bool | None = None ,
207209 history_processors : Sequence [HistoryProcessor [AgentDepsT ]] | None = None ,
208210 event_stream_handler : EventStreamHandler [AgentDepsT ] | None = None ,
211+ tool_timeout : float | None = None ,
209212 ) -> None : ...
210213
211214 def __init__ (
@@ -231,6 +234,7 @@ def __init__(
231234 instrument : InstrumentationSettings | bool | None = None ,
232235 history_processors : Sequence [HistoryProcessor [AgentDepsT ]] | None = None ,
233236 event_stream_handler : EventStreamHandler [AgentDepsT ] | None = None ,
237+ tool_timeout : float | None = None ,
234238 ** _deprecated_kwargs : Any ,
235239 ):
236240 """Create an agent.
@@ -285,6 +289,9 @@ def __init__(
285289 Each processor takes a list of messages and returns a modified list of messages.
286290 Processors can be sync or async and are applied in sequence.
287291 event_stream_handler: Optional handler for events from the model's streaming response and the agent's execution of tools.
292+ tool_timeout: Default timeout in seconds for tool execution. If a tool takes longer than this,
293+ a retry prompt is returned to the model. Individual tools can override this with their own timeout.
294+ Defaults to None (no timeout).
288295 """
289296 if model is None or defer_model_check :
290297 self ._model = model
@@ -318,6 +325,7 @@ def __init__(
318325
319326 self ._max_result_retries = output_retries if output_retries is not None else retries
320327 self ._max_tool_retries = retries
328+ self ._tool_timeout = tool_timeout
321329
322330 self ._validation_context = validation_context
323331
@@ -331,7 +339,10 @@ def __init__(
331339 self ._output_toolset .max_retries = self ._max_result_retries
332340
333341 self ._function_toolset = _AgentFunctionToolset (
334- tools , max_retries = self ._max_tool_retries , output_schema = self ._output_schema
342+ tools ,
343+ max_retries = self ._max_tool_retries ,
344+ default_timeout = self ._tool_timeout ,
345+ output_schema = self ._output_schema ,
335346 )
336347 self ._dynamic_toolsets = [
337348 DynamicToolset [AgentDepsT ](toolset_func = toolset )
@@ -1101,7 +1112,7 @@ async def spam(ctx: RunContext[str], y: float) -> float:
11011112 See the [tools documentation](../deferred-tools.md#human-in-the-loop-tool-approval) for more info.
11021113 metadata: Optional metadata for the tool. This is not sent to the model but can be used for filtering and tool behavior customization.
11031114 timeout: Timeout in seconds for tool execution. If the tool takes longer, a retry prompt is returned to the model.
1104- Defaults to None (no timeout).
1115+ Overrides the agent-level `tool_timeout` if set. Defaults to None (no timeout).
11051116 """
11061117
11071118 def tool_decorator (
@@ -1217,7 +1228,7 @@ async def spam(ctx: RunContext[str]) -> float:
12171228 See the [tools documentation](../deferred-tools.md#human-in-the-loop-tool-approval) for more info.
12181229 metadata: Optional metadata for the tool. This is not sent to the model but can be used for filtering and tool behavior customization.
12191230 timeout: Timeout in seconds for tool execution. If the tool takes longer, a retry prompt is returned to the model.
1220- Defaults to None (no timeout).
1231+ Overrides the agent-level `tool_timeout` if set. Defaults to None (no timeout).
12211232 """
12221233
12231234 def tool_decorator (func_ : ToolFuncPlain [ToolParams ]) -> ToolFuncPlain [ToolParams ]:
@@ -1414,7 +1425,10 @@ def toolsets(self) -> Sequence[AbstractToolset[AgentDepsT]]:
14141425
14151426 if some_tools := self ._override_tools .get ():
14161427 function_toolset = _AgentFunctionToolset (
1417- some_tools .value , max_retries = self ._max_tool_retries , output_schema = self ._output_schema
1428+ some_tools .value ,
1429+ max_retries = self ._max_tool_retries ,
1430+ default_timeout = self ._tool_timeout ,
1431+ output_schema = self ._output_schema ,
14181432 )
14191433 else :
14201434 function_toolset = self ._function_toolset
@@ -1521,11 +1535,12 @@ def __init__(
15211535 tools : Sequence [Tool [AgentDepsT ] | ToolFuncEither [AgentDepsT , ...]] = [],
15221536 * ,
15231537 max_retries : int = 1 ,
1538+ default_timeout : float | None = None ,
15241539 id : str | None = None ,
15251540 output_schema : _output .OutputSchema [Any ],
15261541 ):
15271542 self .output_schema = output_schema
1528- super ().__init__ (tools , max_retries = max_retries , id = id )
1543+ super ().__init__ (tools , max_retries = max_retries , default_timeout = default_timeout , id = id )
15291544
15301545 @property
15311546 def id (self ) -> str :
0 commit comments