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
Raising `ModelRetry` also generates a `RetryPromptPart` containing the exception message, which is sent back to the LLM to guide its next attempt. Both `ValidationError` and `ModelRetry` respect the `retries` setting configured on the `Tool` or `Agent`.
373
373
374
+
### Tool Timeout
375
+
376
+
You can set a timeout for tool execution to prevent tools from running indefinitely. If a tool exceeds its timeout, it is treated as a failure and a retry prompt is sent to the model (counting towards the retry limit).
377
+
378
+
```python
379
+
import asyncio
380
+
381
+
from pydantic_ai import Agent
382
+
383
+
# Set a default timeout for all tools on the agent
384
+
agent = Agent('test', tool_timeout=30)
385
+
386
+
387
+
@agent.tool_plain
388
+
asyncdefslow_tool() -> str:
389
+
"""This tool will use the agent's default timeout (30 seconds)."""
390
+
await asyncio.sleep(10)
391
+
return'Done'
392
+
393
+
394
+
@agent.tool_plain(timeout=5)
395
+
asyncdeffast_tool() -> str:
396
+
"""This tool has its own timeout (5 seconds) that overrides the agent default."""
397
+
await asyncio.sleep(1)
398
+
return'Done'
399
+
```
400
+
401
+
-**Agent-level timeout**: Set `tool_timeout` on the [`Agent`][pydantic_ai.Agent] to apply a default timeout to all tools.
402
+
-**Per-tool timeout**: Set `timeout` on individual tools via [`@agent.tool`][pydantic_ai.Agent.tool], [`@agent.tool_plain`][pydantic_ai.Agent.tool_plain], or the [`Tool`][pydantic_ai.tools.Tool] dataclass. This overrides the agent-level default.
403
+
404
+
When a timeout occurs, the tool is considered to have failed and the model receives a retry prompt with the message `"Timed out after {timeout} seconds."`. This counts towards the tool's retry limit just like validation errors or explicit [`ModelRetry`][pydantic_ai.exceptions.ModelRetry] exceptions.
405
+
374
406
### Parallel tool calls & concurrency
375
407
376
408
When a model returns multiple tool calls in one response, Pydantic AI schedules them concurrently using `asyncio.create_task`.
0 commit comments