How to beginner · 3 min read

How to create an agent in CrewAI

Quick answer
To create an agent in CrewAI, use the CrewAIClient from the official Python SDK, authenticate with your API key, and call the create_agent method with your agent's configuration. This initializes a new agent ready for interaction or deployment.

PREREQUISITES

  • Python 3.8+
  • CrewAI API key
  • pip install crewai-sdk

Setup

Install the official crewai-sdk Python package and set your API key as an environment variable for secure authentication.

bash
pip install crewai-sdk

Step by step

Use the following Python code to create an agent in CrewAI. Replace os.environ["CREWAI_API_KEY"] with your environment variable holding the API key.

python
import os
from crewai_sdk import CrewAIClient

# Initialize client with API key from environment
client = CrewAIClient(api_key=os.environ["CREWAI_API_KEY"])

# Define agent configuration
agent_config = {
    "name": "MyAgent",
    "description": "An example agent created via CrewAI SDK",
    "model": "gpt-4o",
    "capabilities": ["chat", "task_execution"]
}

# Create the agent
agent = client.create_agent(config=agent_config)

print(f"Agent created with ID: {agent.id}")
output
Agent created with ID: agent_1234567890abcdef

Common variations

You can create agents with different models like gemini-1.5-pro or claude-3-5-sonnet-20241022 by changing the model field in the configuration. Async creation is supported via asyncio if your application requires concurrency.

python
import asyncio
import os
from crewai_sdk import CrewAIClient

async def create_agent_async():
    client = CrewAIClient(api_key=os.environ["CREWAI_API_KEY"])
    agent_config = {
        "name": "AsyncAgent",
        "description": "Created asynchronously",
        "model": "gemini-1.5-pro",
        "capabilities": ["chat"]
    }
    agent = await client.create_agent_async(config=agent_config)
    print(f"Async agent created with ID: {agent.id}")

asyncio.run(create_agent_async())
output
Async agent created with ID: agent_abcdef1234567890

Troubleshooting

If you receive authentication errors, verify your API key is correctly set in the environment variable CREWAI_API_KEY. For network timeouts, check your internet connection and retry. If the agent creation fails due to invalid configuration, ensure all required fields like name and model are correctly specified.

Key Takeaways

  • Use the official crewai-sdk and authenticate via environment variables for security.
  • Agent creation requires specifying a valid model and capabilities in the configuration.
  • Async agent creation is supported for scalable applications.
  • Check environment variables and config fields carefully to avoid common errors.
Verified 2026-04 · gpt-4o, gemini-1.5-pro, claude-3-5-sonnet-20241022
Verify ↗