fix: increment tool usage count in native function calling path#5348
Open
Ricardo-M-L wants to merge 1 commit intocrewAIInc:mainfrom
Open
fix: increment tool usage count in native function calling path#5348Ricardo-M-L wants to merge 1 commit intocrewAIInc:mainfrom
Ricardo-M-L wants to merge 1 commit intocrewAIInc:mainfrom
Conversation
The `max_usage_count` limit on tools was never enforced when using native function calling (GPT-4, Claude, Gemini, etc.) because `current_usage_count` was not incremented after successful tool execution in `_execute_single_native_tool_call`. The usage-limit CHECK existed (comparing `current_usage_count` against `max_usage_count`) but the count was never bumped, so the check always passed and tools could be called an unlimited number of times. The ReAct/text-based path in `tool_usage.py` already increments the count correctly via `_increment_usage_count()`. This commit adds the same increment logic to the native function calling path, using the `structured_tool` wrapper (which syncs the count back to the original tool) when available, and falling back to directly incrementing `original_tool.current_usage_count`.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit f365379. Configure here.
| elif original_tool and hasattr( | ||
| original_tool, "current_usage_count" | ||
| ): | ||
| original_tool.current_usage_count += 1 |
There was a problem hiding this comment.
Double-counting tool usage causes premature blocking
High Severity
The tool usage count is incremented twice per execution: once by BaseTool.run() (via _claim_usage()) and again by the new logic. This causes tools to hit their max_usage_count limit prematurely, effectively halving the configured usage.
Reviewed by Cursor Bugbot for commit f365379. Configure here.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Summary
max_usage_counton tools is completely broken when using native function calling (GPT-4, Claude, Gemini, etc.) becausecurrent_usage_countis never incremented after successful tool execution in_execute_single_native_tool_call.crew_agent_executor.pylines 896-906) — it comparescurrent_usage_countagainstmax_usage_counttool_usage.pycorrectly increments via_increment_usage_count()after each successful callChanges
Added the same increment logic that exists in
tool_usage.pyto the native function calling path increw_agent_executor.py, after the successful tool execution block inside_execute_single_native_tool_call:Uses
structured_tool._increment_usage_count()(which syncs the count back to the original tool) when available, falling back to directly incrementingoriginal_tool.current_usage_count.Test plan
max_usage_count=2on a tool and run an agent withfunction_calling_llm(native path) — verify the tool is blocked after 2 callsmax_usage_countmax_usage_countare unaffectedNote
Low Risk
Small behavioral fix in the native tool execution path to correctly increment usage counters; low risk but may newly block tools once their configured
max_usage_countis reached.Overview
Fixes
max_usage_countenforcement for native function-calling tool execution by incrementing a tool’s usage counter after a successful call inCrewAgentExecutor._execute_single_native_tool_call.When available, it uses
CrewStructuredTool._increment_usage_count()to keep counts in sync with the original tool, and otherwise falls back to incrementingoriginal_tool.current_usage_countdirectly.Reviewed by Cursor Bugbot for commit f365379. Bugbot is set up for automated code reviews on this repo. Configure here.