How to beginner · 3 min read

How to use CrewAI with Claude

Quick answer
To use CrewAI with Claude, integrate the Anthropic SDK in your Python project and call client.messages.create with your claude-3-5-sonnet-20241022 model. Use system= to set instructions and pass user messages in the messages parameter for seamless AI workflow automation.

PREREQUISITES

  • Python 3.8+
  • Anthropic API key
  • pip install anthropic>=0.20

Setup

Install the anthropic Python SDK and set your API key as an environment variable for secure authentication.

bash
pip install anthropic>=0.20

Step by step

Use the Anthropic SDK to send messages to Claude within a CrewAI workflow. Below is a complete example demonstrating how to create a client, send a prompt, and receive a response.

python
import os
import anthropic

# Initialize the Anthropic client with your API key
client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])

# Define the system prompt and user message
system_prompt = "You are a helpful assistant integrated with CrewAI."
user_message = "Generate a summary of the latest project updates."

# Send the message to Claude
response = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=300,
    system=system_prompt,
    messages=[{"role": "user", "content": user_message}]
)

# Extract and print the assistant's reply
print(response.content[0].text)
output
Here is a summary of the latest project updates: The team completed the initial design phase, started development on the core modules, and resolved key bugs from the last sprint.

Common variations

You can customize your CrewAI integration with Claude by using different Claude models like claude-3-opus-20240229 for faster responses or adjusting max_tokens for longer outputs. Async calls and streaming responses are also supported by the Anthropic SDK for real-time applications.

python
import asyncio
import os
import anthropic

async def async_claude_call():
    client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
    response = await client.messages.acreate(
        model="claude-3-opus-20240229",
        max_tokens=150,
        system="You are a fast assistant.",
        messages=[{"role": "user", "content": "List key features of CrewAI."}]
    )
    print(response.content[0].text)

asyncio.run(async_claude_call())
output
CrewAI key features include workflow automation, AI integration, real-time collaboration, and customizable task management.

Troubleshooting

  • If you receive authentication errors, verify your ANTHROPIC_API_KEY environment variable is set correctly.
  • For rate limit errors, implement exponential backoff retries or reduce request frequency.
  • If responses are incomplete, increase max_tokens or check for network issues.

Key Takeaways

  • Use the Anthropic SDK with claude-3-5-sonnet-20241022 for best coding and workflow integration performance.
  • Always set your API key securely via environment variables to avoid credential leaks.
  • Customize model parameters like max_tokens and model version to fit your CrewAI use case.
  • Async and streaming calls enable responsive, real-time AI interactions within CrewAI workflows.
  • Handle common errors by verifying API keys, managing rate limits, and adjusting token limits.
Verified 2026-04 · claude-3-5-sonnet-20241022, claude-3-opus-20240229
Verify ↗