As a developer advocate at CrewAI, I get asked a lot about how to get started with building AI agents. In this brief blog post, I'll walk you through the process of getting started with CrewAI and creating your first AI agent.
What is CrewAI?
CrewAI is an innovative framework designed to orchestrate role-playing AI agents. It allows you to create autonomous AI agents that can:
- Work together in a hierarchical structure
- Share context and information
- Execute complex tasks sequentially or in parallel
- Integrate with various LLM providers
Setting Up Your Environment
First, you'll need to install CrewAI and CrewAI tools:
pip install crewai crewai-tools
You'll also need to configure your LLM provider. CrewAI supports various options including:
- OpenAI
- Anthropic
- Local models via Ollama
- Google Vertex AI
- Azure OpenAI
- More here
Creating Your First Agent
There are various ways to create an agent, but I'll show you how to create a simple agent in CrewAI.
First, configure your API keys:
# Get your free API key here: https://serper.dev export SERPER_API_KEY='your_serper_api_key'
Then create a file called "main.py" and add the following code:
from crewai import Agent, Task, Crew, Process, LLM from crewai.tools import SerperDevTool # Create an LLM provider 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 and providing actionable insights.""", tools=[SerperDevTool()], llm=llm, verbose=True )
Defining Tasks
Tasks are what agents need to accomplish:
# Create a task research_task = Task( description="""Analyze the latest developments in AI agents and autonomous systems. Focus on real-world applications and emerging trends.""", expected_output="An executive summary of comprehensive insights into the current state of AI agent technology", agent=researcher, )
Assembling Your Crew
Now let's put it all together:
# Create the crew with our agents and tasks crew = Crew( agents=[researcher], tasks=[research_task], process=Process.sequential, verbose=True ) # Kick off the work result = crew.kickoff()
Run your code with the following command:
python main.py
What Did We Just Do?
That's it! You've successfully created your first AI agent using CrewAI. You should see a full report of your task in the terminal.
You can further customize your agents, tasks, and crew as needed and add more complex workflows.
Another thing to note is that you can use Pydantic models to make sure you get consistent task outputs and agent responses. Watch this tutorial for more details on how to use Pydantic models with CrewAI.
Best Practices to Keep in Mind When Building with CrewAI
- Give agents clear, specific roles and goals
- Provide relevant context in task descriptions
- Use appropriate tools for the task
- Not all LLMs are created equal; for example, some are not suitable for tool calling
- Use Pydantic models to ensure consistent task outputs and agent responses
Check out CrewAI documentation for more detailed information and advanced usage examples.
PS: I manage the docs for CrewAI. If you have any questions or feedback, don't hesitate to reach out to me on Twitter.