Skip to content

Conversation

@devin-ai-integration
Copy link
Contributor

@devin-ai-integration devin-ai-integration bot commented Nov 23, 2025

feat: Add extended thinking support for Anthropic Claude

Summary

Implements support for Anthropic's extended thinking feature (as described in Anthropic's documentation) to enable CrewAI agents to use Claude's deeper reasoning capabilities with tool use.

Changes:

  • Added thinking parameter to AnthropicCompletion.__init__() to accept extended thinking configuration (e.g., {"type": "enabled", "budget_tokens": 5000})
  • Modified _prepare_completion_params() to include the thinking parameter in API calls when configured
  • Verified that existing code in _handle_tool_use_conversation() already preserves thinking blocks correctly by using initial_response.content directly (line 576)
  • Added comprehensive unit tests covering:
    • Basic thinking parameter acceptance and storage
    • Thinking parameter inclusion in API calls
    • Thinking block preservation in single tool use scenarios
    • Thinking block preservation across multiple tool calls

Fixes: #3964

Review & Testing Checklist for Human

⚠️ IMPORTANT: This PR has NOT been tested with the actual Anthropic API. All tests use mocks.

  • Test with real Anthropic API: Create an agent with thinking={"type": "enabled", "budget_tokens": 5000} and verify that:
    • Extended thinking works without tools
    • Extended thinking works WITH tools (the core issue from [FEATURE] #3964)
    • Thinking blocks are preserved in the tool result messages sent back to Claude
    • The complete tool use loop completes successfully
  • Verify thinking parameter structure: Confirm that the dict[str, Any] type is acceptable or if we need stricter typing (e.g., TypedDict for {"type": "enabled", "budget_tokens": int})
  • Check for regressions: Run existing Anthropic tests to ensure no functionality broke
  • Review test coverage: The tests mock the Anthropic SDK responses - verify they accurately represent real API behavior
  • Consider documentation: Should we add user-facing docs explaining how to enable extended thinking?

Test Plan

from crewai import Agent, Task, Crew, LLM
from crewai.tools import tool

@tool
def calculator(operation: str, a: int, b: int) -> str:
    """Perform basic arithmetic operations"""
    if operation == "add":
        return str(a + b)
    return "0"

# Create agent with extended thinking
llm = LLM(
    model="anthropic/claude-3-5-sonnet-20241022",
    thinking={"type": "enabled", "budget_tokens": 5000}
)

agent = Agent(
    role="Math Assistant",
    goal="Help with calculations",
    backstory="You are a helpful math assistant",
    llm=llm,
    tools=[calculator]
)

task = Task(
    description="Calculate 15 + 27 using the calculator tool",
    expected_output="The sum",
    agent=agent
)

crew = Crew(agents=[agent], tasks=[task])
result = crew.kickoff()
print(result)

Expected: The agent should use extended thinking, call the calculator tool, and return the correct answer (42).

Notes

  • Session: https://app.devin.ai/sessions/ec24f5f0947b4f5d8cb2cecaf7d45a17
  • Requested by: João ([email protected])
  • The existing _handle_tool_use_conversation() method already preserves thinking blocks by using initial_response.content directly, so no changes were needed there
  • Type checking with MyPy timed out during development, so type safety should be verified during review
  • All tests pass locally with mocks, but real API testing is critical before merge

- Add thinking parameter to AnthropicCompletion.__init__
- Include thinking parameter in API calls via _prepare_completion_params
- Thinking blocks are automatically preserved in tool use conversations
- Add comprehensive tests for extended thinking with tool use
- Fixes #3964

Co-Authored-By: João <[email protected]>
@devin-ai-integration
Copy link
Contributor Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

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