Build Your First AI Agent with CrewAI — A Step-by-Step Guide
Stop just watching AI agent videos—it's time to actually build one. This step-by-step guide walks you through creating your first autonomous agent from scratch using CrewAI and Python.
You've read enough blog posts and watched enough YouTube videos about AI Agents. Time to actually build one. Today, we'll walk through using CrewAI—one of the most popular and well-documented agent frameworks—to create your very first AI Agent.
What is CrewAI?
CrewAI is a Python framework for creating AI agents that can:
- Work together in a hierarchical structure like a real team
- Share context and information with each other
- Execute tasks sequentially or in parallel
- Integrate with multiple LLM providers (OpenAI, Anthropic, Ollama, Google...)
Think of it this way: You're the CEO, and CrewAI is the organizational framework that helps you manage an entire team of AI employees.
Step 1: Set Up Your Environment
Install CrewAI and its tools:
pip install crewai crewai-tools
CrewAI supports multiple LLM providers:
- OpenAI (GPT-4, o1-preview)
- Anthropic (Claude)
- Ollama (run models locally, for free)
- Google Vertex AI, Azure OpenAI, and more
Step 2: Create Your First Agent
First, configure your API keys:
export SERPER_API_KEY='your_serper_api_key'
Create a file called main.py:
from crewai import Agent, Task, Crew, Process, LLM
from crewai.tools import SerperDevTool
# Configure the LLM
llm = LLM(
model='o1-preview',
api_key='your_openai_api_key',
temperature=0.7
)
# Create a research agent
researcher = Agent(
role='Research Analyst',
goal='Conduct detailed research on AI technology trends',
backstory="""
You are an expert research analyst with a focus on AI technology.
You have a track record of identifying emerging trends.
""",
tools=[SerperDevTool()],
llm=llm,
verbose=True
)
Step 3: Define a Task
A task is the actual "job" the agent needs to accomplish:
research_task = Task(
description="""
Analyze the latest developments in AI agents and autonomous systems.
Focus on real-world applications and emerging trends.
""",
expected_output="A comprehensive executive summary on the current state of AI agent technology",
agent=researcher,
)
Step 4: Assemble the Crew and Run
crew = Crew(
agents=[researcher],
tasks=[research_task],
process=Process.sequential,
verbose=True
)
result = crew.kickoff()
print(result)
Run it:
python main.py
That's it! You just created your first AI agent.
Best Practices When Building with CrewAI
- Give agents clear, specific roles—vagueness is the mother of all bugs
- Provide sufficient context in task descriptions
- Use appropriate tools matched to the task at hand
- Not all LLMs are equal—some are not suitable for tool calling
- Use Pydantic models to ensure consistent and reliable task outputs
Check out the official CrewAI documentation to go deeper. Leave a comment if you get stuck on any step!