Skip to content

fix(tools): avoid double counting tool usage#5837

Open
pragnyanramtha wants to merge 8 commits into
crewAIInc:mainfrom
pragnyanramtha:fix/tool-usage-count
Open

fix(tools): avoid double counting tool usage#5837
pragnyanramtha wants to merge 8 commits into
crewAIInc:mainfrom
pragnyanramtha:fix/tool-usage-count

Conversation

@pragnyanramtha
Copy link
Copy Markdown

@pragnyanramtha pragnyanramtha commented May 17, 2026

Summary

  • remove the extra ToolUsage-side usage-count increment after CrewStructuredTool invocation
  • add a regression test that executes a limited structured tool through ToolUsage.use()

Root cause

CrewStructuredTool.invoke() already increments current_usage_count and syncs the original BaseTool. ToolUsage._use() and _ause() then incremented the same structured tool again after successful execution, causing tools with max_usage_count=2 to be exhausted after one agent invocation.

Validation

  • uv run pytest lib/crewai/tests/tools/test_tool_usage_limit.py -q
  • uv run ruff check lib/crewai/src/crewai/tools/tool_usage.py lib/crewai/tests/tools/test_tool_usage_limit.py
  • uv run ruff format --check lib/crewai/src/crewai/tools/tool_usage.py lib/crewai/tests/tools/test_tool_usage_limit.py
  • git diff --check

Contributor note

This PR was AI-authored. I attempted to apply the required llm-generated label, but GitHub denied label permissions for the fork author.

Summary by CodeRabbit

  • Bug Fixes

    • Modified tool usage count tracking behavior in synchronous and asynchronous execution paths.
  • Tests

    • Added test to verify tool usage limit enforcement for structured tools derived from base tools.

Review Change Stack

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 17, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 73ffabbf-4487-4040-92fd-0554672776a3

📥 Commits

Reviewing files that changed from the base of the PR and between a95d267 and 19c82e1.

📒 Files selected for processing (2)
  • lib/crewai/src/crewai/tools/tool_usage.py
  • lib/crewai/tests/tools/test_tool_usage_limit.py
💤 Files with no reviewable changes (1)
  • lib/crewai/src/crewai/tools/tool_usage.py

📝 Walkthrough

Walkthrough

The PR removes manual tool usage count incrementing and verbose logging from both synchronous and asynchronous tool execution paths in ToolUsage, then validates that usage counting still functions correctly with structured tools derived from BaseTool through a newly added test.

Changes

Tool Usage Counting Refactor

Layer / File(s) Summary
Remove usage count incrementing from execution paths
lib/crewai/src/crewai/tools/tool_usage.py
Removes _increment_usage_count / current_usage_count logic and optional verbose progress output from both the async and sync tool execution paths.
Verify structured tool usage counting
lib/crewai/tests/tools/test_tool_usage_limit.py
Adds import of ToolCalling and introduces test_tool_usage_increments_structured_tool_once_per_call to validate that usage counting works correctly with structured tools, including enforcement of max_usage_count=2 and synchronization of both structured and original tool counters.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 A rabbit hops through usage counts,
Where tools once tracked each climb and mount—
Now simpler paths, less code to trace,
Yet tests ensure we keep our pace!
Structured tools still count just right, 🎯

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix(tools): avoid double counting tool usage' accurately describes the main change: removing duplicate usage count increments in tool execution paths.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands and usage tips.

@pragnyanramtha pragnyanramtha marked this pull request as ready for review May 17, 2026 02:44
Copilot AI review requested due to automatic review settings May 17, 2026 02:44
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

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.

2 participants