@@ -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
@@ -569,7 +577,7 @@ async def main():
569577 output_toolset .max_retries = self ._max_result_retries
570578 output_toolset .output_validators = output_validators
571579 toolset = self ._get_toolset (output_toolset = output_toolset , additional_toolsets = toolsets )
572- tool_manager = ToolManager [AgentDepsT ](toolset )
580+ tool_manager = ToolManager [AgentDepsT ](toolset , default_timeout = self . _tool_timeout )
573581
574582 # Build the graph
575583 graph = _agent_graph .build_agent_graph (self .name , self ._deps_type , output_type_ )
@@ -1031,6 +1039,7 @@ def tool(
10311039 sequential : bool = False ,
10321040 requires_approval : bool = False ,
10331041 metadata : dict [str , Any ] | None = None ,
1042+ timeout : float | None = None ,
10341043 ) -> Callable [[ToolFuncContext [AgentDepsT , ToolParams ]], ToolFuncContext [AgentDepsT , ToolParams ]]: ...
10351044
10361045 def tool (
@@ -1049,6 +1058,7 @@ def tool(
10491058 sequential : bool = False ,
10501059 requires_approval : bool = False ,
10511060 metadata : dict [str , Any ] | None = None ,
1061+ timeout : float | None = None ,
10521062 ) -> Any :
10531063 """Decorator to register a tool function which takes [`RunContext`][pydantic_ai.tools.RunContext] as its first argument.
10541064
@@ -1098,6 +1108,8 @@ async def spam(ctx: RunContext[str], y: float) -> float:
10981108 requires_approval: Whether this tool requires human-in-the-loop approval. Defaults to False.
10991109 See the [tools documentation](../deferred-tools.md#human-in-the-loop-tool-approval) for more info.
11001110 metadata: Optional metadata for the tool. This is not sent to the model but can be used for filtering and tool behavior customization.
1111+ timeout: Timeout in seconds for tool execution. If the tool takes longer, a retry prompt is returned to the model.
1112+ Defaults to None (no timeout). Overrides the agent-level `tool_timeout` if set.
11011113 """
11021114
11031115 def tool_decorator (
@@ -1118,6 +1130,7 @@ def tool_decorator(
11181130 sequential = sequential ,
11191131 requires_approval = requires_approval ,
11201132 metadata = metadata ,
1133+ timeout = timeout ,
11211134 )
11221135 return func_
11231136
@@ -1142,6 +1155,7 @@ def tool_plain(
11421155 sequential : bool = False ,
11431156 requires_approval : bool = False ,
11441157 metadata : dict [str , Any ] | None = None ,
1158+ timeout : float | None = None ,
11451159 ) -> Callable [[ToolFuncPlain [ToolParams ]], ToolFuncPlain [ToolParams ]]: ...
11461160
11471161 def tool_plain (
@@ -1160,6 +1174,7 @@ def tool_plain(
11601174 sequential : bool = False ,
11611175 requires_approval : bool = False ,
11621176 metadata : dict [str , Any ] | None = None ,
1177+ timeout : float | None = None ,
11631178 ) -> Any :
11641179 """Decorator to register a tool function which DOES NOT take `RunContext` as an argument.
11651180
@@ -1209,6 +1224,8 @@ async def spam(ctx: RunContext[str]) -> float:
12091224 requires_approval: Whether this tool requires human-in-the-loop approval. Defaults to False.
12101225 See the [tools documentation](../deferred-tools.md#human-in-the-loop-tool-approval) for more info.
12111226 metadata: Optional metadata for the tool. This is not sent to the model but can be used for filtering and tool behavior customization.
1227+ timeout: Timeout in seconds for tool execution. If the tool takes longer, a retry prompt is returned to the model.
1228+ Defaults to None (no timeout). Overrides the agent-level `tool_timeout` if set.
12121229 """
12131230
12141231 def tool_decorator (func_ : ToolFuncPlain [ToolParams ]) -> ToolFuncPlain [ToolParams ]:
@@ -1227,6 +1244,7 @@ def tool_decorator(func_: ToolFuncPlain[ToolParams]) -> ToolFuncPlain[ToolParams
12271244 sequential = sequential ,
12281245 requires_approval = requires_approval ,
12291246 metadata = metadata ,
1247+ timeout = timeout ,
12301248 )
12311249 return func_
12321250
0 commit comments