Skip to content

perf(tools): add exact-match fast path to _select_tool#6321

Open
HumphreySun98 wants to merge 1 commit into
crewAIInc:mainfrom
HumphreySun98:perf/select-tool-exact-match-fast-path
Open

perf(tools): add exact-match fast path to _select_tool#6321
HumphreySun98 wants to merge 1 commit into
crewAIInc:mainfrom
HumphreySun98:perf/select-tool-exact-match-fast-path

Conversation

@HumphreySun98

@HumphreySun98 HumphreySun98 commented Jun 24, 2026

Copy link
Copy Markdown

Summary

ToolUsage._select_tool runs on every text-based (ReAct) tool-call iteration. It sorted all tools by fuzzy difflib.SequenceMatcher ratio (an O(L²) string diff per comparison, each also re-sanitizing the tool name) — even when the LLM emitted a valid, exactly-matching tool name, which is the overwhelmingly common case. An exact match has ratio 1.0 and would always win that sort anyway.

Change

Resolve an exact (sanitized) name match in a single O(n) scan before falling back to the fuzzy sort. The fuzzy fallback (and its > 0.85 threshold) is retained unchanged for typo tolerance, and scanning self.tools in order preserves the prior stable tie-break.

Behavior

Identical results — proven by test_select_tool_matches_legacy_behavior, which asserts the new selection equals a reference implementation of the old fuzzy-sort logic across exact names, sanitized names, and typos. The existing error-path test (test_tool_selection_error_event_direct) is unaffected.

Benchmark

8 typical tools, exact-match target (local, timeit):

µs/call
legacy sort-everything ~151
exact-match fast path ~28

~5× on the common path here; larger as the tool count grows, and it compounds with tool-name sanitization caching.

Tests

ruff + mypy clean; added _select_tool exact/fuzzy/equivalence tests; surrounding test_tool_usage.py green.


This PR was authored with Claude Code. Per CONTRIBUTING.md, AI-generated contributions require the llm-generated label — I don't have triage permission to set it, so could a maintainer please add it? 🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Improved tool selection so exact name matches are found immediately, making tool lookup faster and more reliable.
    • Preserved existing typo-tolerant matching when there isn’t an exact match.
  • Tests

    • Added coverage for exact-name, sanitized-name, and near-miss tool selection.
    • Verified the new behavior matches the previous matching results for typical cases.

@corridor-security corridor-security Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Summary: This PR optimizes internal tool selection by adding an exact sanitized-name match before the existing fuzzy fallback, without introducing new external inputs, authentication changes, or data access paths.

Risk: Low risk. No exploitable security vulnerabilities were identified because the change preserves the prior tool-selection behavior and does not modify authorization boundaries, file/network access, or command execution logic.

@coderabbitai

coderabbitai Bot commented Jun 24, 2026

Copy link
Copy Markdown

Review Change Stack

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: 6800a0ec-00b1-435d-8f9f-2543ab738235

📥 Commits

Reviewing files that changed from the base of the PR and between 5827abb and 564c6bc.

📒 Files selected for processing (2)
  • lib/crewai/src/crewai/tools/tool_usage.py
  • lib/crewai/tests/tools/test_tool_usage.py

📝 Walkthrough

Walkthrough

ToolUsage._select_tool now checks for exact matches on sanitized tool names before falling back to fuzzy matching. Tests cover raw-name matches, sanitized-name matches, typo fallback, and parity with the prior selection behavior.

Changes

Tool selection exact-match fast path

Layer / File(s) Summary
Exact sanitized match
lib/crewai/src/crewai/tools/tool_usage.py
ToolUsage._select_tool adds an early return when a tool's sanitized name exactly matches the sanitized request.
Selection tests
lib/crewai/tests/tools/test_tool_usage.py
Adds a mock-wired ToolUsage, a legacy fuzzy reference, a fixed-name tool stub, and tests for exact, sanitized, typo, and parity behavior.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% 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 clearly summarizes the main change: adding an exact-match fast path to _select_tool.
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

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

_select_tool sorted ALL tools by fuzzy SequenceMatcher ratio (an O(L^2)
string diff) on every tool-call iteration, even when the LLM emitted a valid,
exactly-matching tool name — the overwhelmingly common case. An exact match
has ratio 1.0 and would always win that sort anyway.

Resolve exact (sanitized) name matches with a single O(n) scan before falling
back to the fuzzy sort, which is retained unchanged for typo tolerance.
Scanning self.tools in order preserves the prior stable tie-break.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@HumphreySun98 HumphreySun98 force-pushed the perf/select-tool-exact-match-fast-path branch from 564c6bc to a56bf0c Compare June 24, 2026 20:18
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