Skip to content

Conversation

@tradeqvest
Copy link
Contributor

@tradeqvest tradeqvest commented Aug 21, 2025

  • Fixes Enforce usage limits by exact tool call count (not just request/turn count) #2593

  • Introduces a tool-call cap per run:

    • Adds RunUsage.tool_calls (increments on successful tool execution).
    • Adds UsageLimits.tool_calls_limit; enforced before each invocation.
  • Counting semantics:

    • Increment occurs after argument validation and a successful tool run.
    • Retries do not count as additional executions.
    • Stubbed returns (e.g., “final result already processed”) and validation failures do not count.
    • Output tools do not count as a tool call
  • Backward compatible: defaults to no limit (None).

@hyperlint-ai-deprecated
Copy link
Contributor

PR Change Summary

Implemented usage limits for tool calls in the Pydantic AI framework, enhancing control over tool execution during runs.

  • Introduced a tool-call limit feature to restrict the number of tool invocations per run.
  • Updated documentation to reflect the new tool-call counting semantics and usage limits.
  • Modified examples to demonstrate the usage of the new tool-call limit functionality.

Modified Files

  • docs/agents.md
  • docs/models/openai.md
  • docs/multi-agent-applications.md
  • docs/output.md
  • docs/tools.md

How can I customize these reviews?

Check out the Hyperlint AI Reviewer docs for more information on how to customize the review.

If you just want to ignore it on this PR, you can add the hyperlint-ignore label to the PR. Future changes won't trigger a Hyperlint review.

Note specifically for link checks, we only check the first 30 links in a file and we cache the results for several hours (for instance, if you just added a page, you might experience this). Our recommendation is to add hyperlint-ignore to the PR to ignore the link check for this PR.

@tradeqvest
Copy link
Contributor Author

Side note: I had to update the uv lock, since the pre-commit format hook would otherwise fail due to the altered lock.

@DouweM DouweM self-assigned this Aug 27, 2025
@DouweM DouweM changed the title Tool-usage-limits Add tool calls limit to UsageLimits Aug 27, 2025
@tradeqvest tradeqvest force-pushed the tool-usage-limits branch 3 times, most recently from 8f7c248 to 2569f4a Compare August 28, 2025 19:43
@tradeqvest tradeqvest requested a review from DouweM August 28, 2025 19:47
- Added `usage_limits` to `GraphRunContext` to manage usage constraints.
- Introduced `tool_calls` attribute in `RunUsage` to track tool call counts.
- Updated `UsageLimits` class to include `tool_calls_limit` and validation logic for tool calls.
- Implemented a lock in `ToolManager` to ensure thread-safe usage limit checks during tool calls.
- Updated `agents.md` to clarify the inclusion of tool calls in usage limits.
- Added examples demonstrating the use of `tool_calls_limit` in `agents.md` and `multi-agent-applications.md`.
- Revised usage output examples across multiple documentation files to reflect the new `tool_calls` attribute.
- Included a note in `tools.md` about limiting exact tool executions with `UsageLimits`.
- Modified assertions in multiple test files to include the new `tool_calls` attribute in `RunUsage`.
- Added new test cases to validate tool call limits and ensure correct behavior when exceeding limits.
- Updated existing tests to reflect changes in tool call tracking across different models and scenarios.
- Added a new test to verify that output tools do not increment the `tool_calls` metric.
…calls` usage metric

- Adjusted the tool call counting mechanism to ensure that only successful tool invocations are counted towards the `tool_calls` metric.
- Updated documentation in `tools.md` to clarify that output tools do not increment the `tool_calls` count.
- Modified multiple test cases to reflect the correct counting of tool calls, including tests for failed tool calls and their impact on usage metrics.
- Updated docstrings in `RunContext`, `RunUsage`, and `UsageLimits` to specify that only successful tool calls are counted and to clarify the meaning of usage limits.
- Updated the `ToolManager` and related functions to incorporate `usage_limits` when handling tool calls.
- Removed the `usage_limits` attribute from `RunContext` as it is now managed directly within the tool manager.
- Ensured that usage limits are checked for non-output tool calls to maintain accurate tracking of tool usage.
@tradeqvest tradeqvest requested a review from DouweM September 3, 2025 18:33
@DouweM DouweM changed the title Add tool calls limit to UsageLimits Add tool_calls_limit to UsageLimits and tool_calls to RunUsage Sep 3, 2025
@DouweM DouweM merged commit 881cd7a into pydantic:main Sep 3, 2025
30 checks passed
@DouweM
Copy link
Collaborator

DouweM commented Sep 3, 2025

@tradeqvest Thanks Niko!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enforce usage limits by exact tool call count (not just request/turn count)

2 participants