-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Open
Labels
Feature requestNew feature requestNew feature request
Description
Description
Pydantic AI supports per-agent tool call limits, and per-tool retry limits, but it's currently not possible to specify per-tool usage limits, which seems like a gap. This would be useful in cases where some tools are much more expensive than others, and so their usage needs to be more tightly controlled.
For example, it would be great to be able to do
@agent.tool(max_uses=2)
async def expensive_tool(ctx: RunContext[Deps], input: str) -> str:
...
@agent.tool(max_uses=10)
async def cheap_tool(ctx: RunContext[Deps], input: str) -> str:
...The implementation should closely follow the tool max_retries and agent usage_limits setting:
- The initial system prompt should include information about the max uses for each tool.
- Calling a tool too many times should raise a
UsageLimitExceededor similar exception. - The agent should be allowed to call other tools according to
usage_limitsand any other per-tool usage limits
This should prevent some boilerplate in adding usage counts and limits to the deps and checking this in the tool definition, as in:
@agent.tool
async def my_tool(ctx: RunContext[Deps], input: str) -> str:
if ctx.deps.tool_uses >= ctx.deps.tool_use_limit:
return "You have already used the my_tool tool the maximum number of allowed times."
# tool logic
...
ctx.deps.tool_uses += 1Given potential issues with parallel tools (#3120), I think it's worth a robust pydantic-ai side implementation here.
Metadata
Metadata
Assignees
Labels
Feature requestNew feature requestNew feature request