Skip to content

fix: increment tool usage count in native function calling path#5348

Open
Ricardo-M-L wants to merge 1 commit intocrewAIInc:mainfrom
Ricardo-M-L:fix/native-tool-max-usage-count-increment
Open

fix: increment tool usage count in native function calling path#5348
Ricardo-M-L wants to merge 1 commit intocrewAIInc:mainfrom
Ricardo-M-L:fix/native-tool-max-usage-count-increment

Conversation

@Ricardo-M-L
Copy link
Copy Markdown

@Ricardo-M-L Ricardo-M-L commented Apr 8, 2026

Summary

max_usage_count on tools is completely broken when using native function calling (GPT-4, Claude, Gemini, etc.) because current_usage_count is never incremented after successful tool execution in _execute_single_native_tool_call.

  • The usage-limit check exists (crew_agent_executor.py lines 896-906) — it compares current_usage_count against max_usage_count
  • But the increment after successful execution is missing, so the check always passes and tools can be called unlimited times
  • The ReAct/text-based path in tool_usage.py correctly increments via _increment_usage_count() after each successful call

Changes

Added the same increment logic that exists in tool_usage.py to the native function calling path in crew_agent_executor.py, after the successful tool execution block inside _execute_single_native_tool_call:

if structured_tool and hasattr(structured_tool, "_increment_usage_count"):
    structured_tool._increment_usage_count()
elif original_tool and hasattr(original_tool, "current_usage_count"):
    original_tool.current_usage_count += 1

Uses structured_tool._increment_usage_count() (which syncs the count back to the original tool) when available, falling back to directly incrementing original_tool.current_usage_count.

Test plan

  • Set max_usage_count=2 on a tool and run an agent with function_calling_llm (native path) — verify the tool is blocked after 2 calls
  • Verify the ReAct path still works correctly with max_usage_count
  • Verify tools without max_usage_count are unaffected

Note

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_count is reached.

Overview
Fixes max_usage_count enforcement for native function-calling tool execution by incrementing a tool’s usage counter after a successful call in CrewAgentExecutor._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 incrementing original_tool.current_usage_count directly.

Reviewed by Cursor Bugbot for commit f365379. Bugbot is set up for automated code reviews on this repo. Configure here.

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`.
Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ 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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit f365379. Configure here.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant