Skip to content

Framework for orchestrating role-playing, autonomous AI agents. By fostering collaborative intelligence, CrewAI empowers agents to work together seamlessly, tackling complex tasks.

fix(llms): use auto tool choice for a single Anthropic tool (#8282)

Vidit Ostwaldec075b5e424
diff --git a/lib/crewai/src/crewai/llms/providers/anthropic/completion.py b/lib/crewai/src/crewai/llms/providers/anthropic/completion.py
index a3d8d7833..8c582db94 100644
--- a/lib/crewai/src/crewai/llms/providers/anthropic/completion.py
+++ b/lib/crewai/src/crewai/llms/providers/anthropic/completion.py
@@ -625,7 +625,8 @@ class AnthropicCompletion(BaseLLM):
system_message: Extracted system message
tools: Tool definitions
available_functions: Available functions for tool calling. When provided
- with a single tool, tool_choice is automatically set to force tool use.
+ with a single tool, tool_choice is "auto" so the model may call
+ that tool or answer in text.
Returns:
Parameters dictionary for Anthropic API
@@ -652,8 +653,7 @@ class AnthropicCompletion(BaseLLM):
# When tool_search is enabled and there are 2+ regular tools,
# inject the search tool and mark regular tools with defer_loading.
- # With only 1 tool there's nothing to search — skip tool search
- # entirely so the normal forced tool_choice optimisation still works.
+ # With only 1 tool there's nothing to search, so skip tool search.
regular_tools = [
t
for t in converted_tools
@@ -664,10 +664,11 @@ class AnthropicCompletion(BaseLLM):
params["tools"] = converted_tools
+ # available_functions means the tool can be executed, not that the
+ # model must call it. Named tool_choice is also rejected by newer
+ # Claude models (Sonnet 5.5, Opus 5.5, Fable 5.1, Mythos 5.1).
if available_functions and len(regular_tools) == 1:
- tool_name = regular_tools[0].get("name")
- if tool_name and tool_name in available_functions:
- params["tool_choice"] = {"type": "tool", "name": tool_name}
+ params["tool_choice"] = {"type": "auto"}
if self.thinking:
if isinstance(self.thinking, AnthropicThinkingConfig):
diff --git a/lib/crewai/tests/llms/anthropic/test_anthropic.py b/lib/crewai/tests/llms/anthropic/test_anthropic.py
index 059627ba9..8fce27066 100644
--- a/lib/crewai/tests/llms/anthropic/test_anthropic.py
+++ b/lib/crewai/tests/llms/anthropic/test_anthropic.py
@@ -1470,9 +1470,9 @@ def test_tool_search_passthrough_preserves_tool_search_type():
assert "input_schema" in converted[1]
-def test_tool_search_single_tool_skips_search_and_forces_choice():
- """With only 1 tool, tool_search is skipped (nothing to search) and the
- normal forced tool_choice optimisation still applies."""
+def test_tool_search_single_tool_skips_search_and_uses_auto_choice():
+ """With only 1 tool, tool_search is skipped (nothing to search) and
+ tool_choice stays auto."""
llm = LLM(model="anthropic/claude-sonnet-4-5", tool_search=True)
crewai_tools = [
@@ -1500,9 +1500,8 @@ def test_tool_search_single_tool_skips_search_and_forces_choice():
available_functions={"test_tool": lambda q: "result"},
)
- # Single tool — tool_search skipped, tool_choice forced as normal
- assert "tool_choice" in params
- assert params["tool_choice"]["name"] == "test_tool"
+ # Single tool — tool_search skipped, model chooses whether to call it
+ assert params["tool_choice"] == {"type": "auto"}
# No tool search tool should be injected
tool_types = [t.get("type", "") for t in params["tools"]]
@@ -1513,6 +1512,39 @@ def test_tool_search_single_tool_skips_search_and_forces_choice():
assert "defer_loading" not in params["tools"][0]
+def test_single_tool_with_available_functions_uses_auto_choice():
+ """One executable tool is offered, not forced.
+
+ Newer Claude models reject tool_choice {"type": "tool"}. Auto lets the
+ model call the tool or answer in text.
+ """
+ llm = LLM(model="anthropic/claude-sonnet-5-5")
+ tool = {
+ "type": "function",
+ "function": {
+ "name": "get_weather",
+ "description": "Get the weather for a city.",
+ "parameters": {
+ "type": "object",
+ "properties": {"city": {"type": "string"}},
+ "required": ["city"],
+ },
+ },
+ }
+ formatted_messages, system_message = llm._format_messages_for_anthropic(
+ [{"role": "user", "content": "What's the weather in Paris?"}]
+ )
+ params = llm._prepare_completion_params(
+ formatted_messages,
+ system_message,
+ [tool],
+ available_functions={"get_weather": lambda city: "sunny"},
+ )
+
+ assert params["tool_choice"] == {"type": "auto"}
+ assert [t["name"] for t in params["tools"]] == ["get_weather"]
+
+
def test_tool_search_via_llm_class():
"""Verify tool_search param passes through LLM class correctly."""
from crewai.llms.providers.anthropic.completion import (