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
# Allow at most one executed tool call in this run
630
+
agent.run_sync('Please call the tool twice', usage_limits=UsageLimits(tool_calls_limit=1))
631
+
except UsageLimitExceeded as e:
632
+
print(e)
633
+
#> The next tool call would exceed the tool_calls_limit of 1 (tool_calls=1)
634
+
```
635
+
613
636
!!! note
614
-
- Usage limits are especially relevant if you've registered many tools. The`request_limit`can be used to prevent the model from calling them in a loop too many times.
637
+
- Usage limits are especially relevant if you've registered many tools. Use`request_limit` to bound the number of model turns, and `tool_calls_limit` to cap the number of successful tool executions within a run.
615
638
- These limits are enforced at the final stage before the LLM is called. If your limits are stricter than your retry settings, the usage limit will be reached before all retries are attempted.
Copy file name to clipboardExpand all lines: docs/multi-agent-applications.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,7 +19,7 @@ Since agents are stateless and designed to be global, you do not need to include
19
19
You'll generally want to pass [`ctx.usage`][pydantic_ai.RunContext.usage] to the [`usage`][pydantic_ai.agent.AbstractAgent.run] keyword argument of the delegate agent run so usage within that run counts towards the total usage of the parent agent run.
20
20
21
21
!!! note "Multiple models"
22
-
Agent delegation doesn't need to use the same model for each agent. If you choose to use different models within a run, calculating the monetary cost from the final [`result.usage()`][pydantic_ai.agent.AgentRunResult.usage] of the run will not be possible, but you can still use [`UsageLimits`][pydantic_ai.usage.UsageLimits] to avoid unexpected costs.
22
+
Agent delegation doesn't need to use the same model for each agent. If you choose to use different models within a run, calculating the monetary cost from the final [`result.usage()`][pydantic_ai.agent.AgentRunResult.usage] of the run will not be possible, but you can still use [`UsageLimits`][pydantic_ai.usage.UsageLimits]— including `request_limit`, `total_tokens_limit`, and `tool_calls_limit` — to avoid unexpected costs or runaway tool loops.
23
23
24
24
```python {title="agent_delegation_simple.py"}
25
25
from pydantic_ai import Agent, RunContext, UsageLimits
@@ -52,7 +52,7 @@ result = joke_selection_agent.run_sync(
52
52
print(result.output)
53
53
#> Did you hear about the toothpaste scandal? They called it Colgate.
Copy file name to clipboardExpand all lines: docs/tools-advanced.md
+3Lines changed: 3 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -377,6 +377,9 @@ When a model returns multiple tool calls in one response, Pydantic AI schedules
377
377
378
378
Async functions are run on the event loop, while sync functions are offloaded to threads. To get the best performance, _always_ use an async function _unless_ you're doing blocking I/O (and there's no way to use a non-blocking library instead) or CPU-bound work (like `numpy` or `scikit-learn` operations), so that simple functions are not offloaded to threads unnecessarily.
379
379
380
+
!!! note "Limiting tool executions"
381
+
You can cap tool executions within a run using [`UsageLimits(tool_calls_limit=...)`](agents.md#usage-limits). The counter increments only after a successful tool invocation. Output tools (used for structured output) are not counted in the `tool_calls` metric.
382
+
380
383
## See Also
381
384
382
385
-[Function Tools](tools.md) - Basic tool concepts and registration
0 commit comments