Setting Up Crew
Learn to set up and run your first AI crew with CrewAI. This guide covers installation, defining agents, tasks, and executing multi-agent pipelines for LLM automation.
Module 3: Setting Up Your First Crew
This module guides you through the essential steps of setting up and running your first crew with CrewAI. We'll cover installation, defining agents, configuring their tasks, and executing a basic multi-agent pipeline.
1. Installing CrewAI and Dependencies
Before you can start building your crew, you need to install the CrewAI library and any necessary dependencies.
Prerequisites
Python 3.8+: Ensure you have a compatible Python version installed.
Pip: Python's package installer.
Installation Steps
You can install CrewAI using pip. It's highly recommended to use a virtual environment to manage your project's dependencies.
Create and activate a virtual environment:
## Create a virtual environment
python -m venv venv
## Activate the virtual environment
## On Windows:
venv\Scripts\activate
## On macOS/Linux:
source venv/bin/activate
Install CrewAI:
pip install crewai
This command installs the core CrewAI library. Depending on your specific use case and the tools your agents will interact with, you might need to install additional libraries. For example, if your agents will use tools that require specific API keys, you'll need to install those libraries separately.
2. Writing Your First Agent: Role, Goal, and Toolset
An agent is the fundamental building block of a crew. Each agent is defined by its Role, Goal, and Toolset.
Agent Components
Role: A descriptive title for the agent, indicating its function or persona (e.g., "Content Writer", "Code Reviewer", "Research Analyst").
Goal: A clear and concise statement of what the agent aims to achieve. This should be specific and actionable.
Toolset: A collection of tools the agent can use to perform its tasks. These tools are Python functions or classes that provide specific capabilities.
Example Agent Definition
Here's a Python snippet demonstrating how to define a simple agent:
from crewai import Agent
from crewai_tools import SerperDevTool # Example tool
## Define your tools
search_tool = SerperDevTool() # You'll need to set up Serper API key
## Define an agent
writer = Agent(
role='Content Writer',
goal='Write engaging and informative blog posts about AI advancements',
backstory="""You are a seasoned content writer with a knack for explaining complex topics
in a simple and accessible way. You love to share the latest news and insights in the AI world.""",
tools=[search_tool],
verbose=True, # Set to True for more detailed output during execution
allow_delegation=False # Set to True if you want this agent to delegate tasks to others
)
Explanation:
We import the
Agent
class fromcrewai
and an example tool (SerperDevTool
).We instantiate a
SerperDevTool
(assuming you've configured yourSERPER_API_KEY
environment variable).We create an
Agent
instance namedwriter
.role
: "Content Writer"goal
: "Write engaging and informative blog posts about AI advancements"backstory
: Provides context and personality to the agent. This helps in shaping its responses.tools
: A list containing thesearch_tool
that this agent can utilize.verbose
: When set toTrue
, the agent's thought process and tool usage will be printed during execution, which is invaluable for debugging.allow_delegation
: If set toTrue
, this agent can delegate tasks to other agents within the same crew if it deems it necessary.
3. Running a Basic Multi-Agent Pipeline
A pipeline in CrewAI is orchestrated by a Crew
. A crew is a collection of agents working collaboratively to achieve a common goal, typically by executing a series of tasks.
Creating and Running a Crew
To run a pipeline, you need to define a Crew
object, assign tasks to your agents, and then execute the crew.
from crewai import Crew, Task
from agents import writer # Assuming your agent definition is in an 'agents.py' file
## Define a task for the agent
write_blog_post_task = Task(
description="""Research and write a comprehensive blog post about the latest advancements in Generative AI.
Ensure the post is at least 1000 words long, well-researched, and engaging.""",
expected_output='A fully written blog post in markdown format about Generative AI advancements.',
agent=writer # Assign the task to the writer agent
)
## Instantiate your crew
my_crew = Crew(
agents=[writer], # List of agents in the crew
tasks=[write_blog_post_task], # List of tasks the crew will execute
verbose=2 # Set to 1 or 2 for more detailed logging
)
## Execute the crew
result = my_crew.kickoff()
print("## Output:")
print(result)
Explanation:
Import necessary classes:
Crew
,Task
fromcrewai
, and your definedagents
.Define a
Task
:description
: The specific instructions for the agent to perform.expected_output
: What the final output of this task should look like. This helps guide the agent.agent
: The agent responsible for executing this task.
Instantiate a
Crew
:agents
: A list of allAgent
objects that are part of this crew.tasks
: A list of allTask
objects that the crew needs to complete.verbose
: Controls the level of logging.2
provides the most detailed output, useful for understanding the execution flow and debugging.
kickoff()
the crew: This method starts the execution of all tasks in the crew. The agents will collaborate and execute their assigned tasks until all are completed.Print the result: The
result
variable will contain the final output from the crew's execution.
4. Debugging and Logging Crew Behavior
Effective debugging is crucial for fine-tuning your multi-agent systems. CrewAI offers built-in logging features to help you understand what your agents are doing.
Using verbose
The verbose
parameter is your primary tool for debugging.
agent.verbose = True
: When set on an individualAgent
, it shows the thought process, tool usage, and execution details for that specific agent.crew.verbose = 1
orcrew.verbose = 2
: When set on theCrew
object, it controls the overall logging level of the entire crew execution.1
: Provides a moderate level of detail, showing task progression and agent reasoning.2
: Provides extensive detail, including every step of agent reasoning, tool calls, and responses.
Interpreting Logs
When verbose
is enabled, you'll typically see:
Agent's Role and Goal: Confirmation of what the agent is supposed to do.
Thought Process: The agent's internal monologue about how it plans to approach the task, what information it needs, and which tools it might use.
Tool Usage: When an agent decides to use a tool, the logs will show which tool is being called and with what arguments.
Tool Response: The output received from the tool.
Final Answer/Output: The agent's concluded answer or the result of its task.
Common Debugging Scenarios
Agent Stuck: If an agent seems to be looping or not making progress, increase the
verbose
level to understand its thought process. It might be waiting for information it can't get or stuck in a reasoning loop.Incorrect Tool Usage: Check the logs to see if the agent is calling the correct tool with the right parameters. If not, you may need to refine its
backstory
,goal
, or thedescription
of the task.Misinterpretation of Task: If the agent is not performing the task as expected, re-examine the
description
andexpected_output
of the task. Ensure they are clear, specific, and unambiguous.
5. Creating a Crew: Assigning Agents to Tasks
A crew acts as the orchestrator, bringing together agents to work on a defined set of tasks. The core of a crew is the mapping of tasks to specific agents.
Crew Configuration
When creating a Crew
object, you provide lists of agents
and tasks
. The order of tasks in the tasks
list can influence the execution flow, especially if tasks depend on each other (though CrewAI also supports more complex dependency management).
Assigning Tasks
Each Task
object has an agent
parameter, explicitly linking it to the Agent
instance responsible for executing it.
## Assume you have defined multiple agents:
## researcher = Agent(...)
## writer = Agent(...)
## editor = Agent(...)
## Define tasks and assign them to specific agents
research_task = Task(
description="Gather the latest statistics on renewable energy adoption in Europe.",
expected_output="A summary of key statistics and trends.",
agent=researcher
)
writing_task = Task(
description="Write a report based on the research findings.",
expected_output="A detailed report in markdown format.",
agent=writer
)
editing_task = Task(
description="Review the report for clarity, grammar, and factual accuracy.",
expected_output="A polished and edited report.",
agent=editor
)
## Create the crew with the agents and their assigned tasks
report_crew = Crew(
agents=[researcher, writer, editor],
tasks=[research_task, writing_task, editing_task],
verbose=2
)
## Execute the crew
result = report_crew.kickoff()
print(result)
In this example, research_task
is explicitly assigned to the researcher
agent, writing_task
to the writer
agent, and editing_task
to the editor
agent. The Crew
then manages the execution order (by default, sequentially) and data flow between these tasks.