CrewAI Collaboration

Last Updated : 14 Apr, 2026

CrewAI is a framework that enables multiple autonomous agents to collaborate and achieve complex tasks together. By defining agents, tasks and processes we can design workflows that mimic real-world teamwork. CrewAI supports different types of collaboration, primarily include:

  • Sequential Collaboration: Tasks are executed in a defined order, where one agent’s output becomes another’s input.
  • Hierarchical Collaboration: A manager agent who oversees the process, delegating tasks dynamically to other agents.

Installing CrewAI

Before using CrewAI, install it in our environment. We will install CrewAI using pip:

pip install crewai

After installation, we can set up the API key and import the needed classes:

Python
import os
os.environ["OPENAI_API_KEY"] = "Your_API_Key"

from crewai import Agent, Task, Crew, Process

This gives us access to the Agent, Task, Crew and Process classes required to define and run multi-agent workflows.

1. Sequential Process

In a Sequential Process, tasks are executed in a strict order, where each task depends on the output of the previous one. This ensures a structured workflow, making it suitable for step-by-step tasks like cooking or reporting.

This type of process works best in situations like cooking, research or reporting where each stage builds directly on the last. We will implement an example of a Chef and a Food Critic.

Step 1: Defining Agents

We will define our two agents , the Chef and the Food Critic.

  • Agent: Represents a role with a goal and backstory.
  • Verbose: Enables detailed logs of the process.
Python
chef = Agent(
    role='Chef',
    goal='Prepare a delicious meal using the best ingredients.',
    verbose=True,
    backstory="The chef has years of experience in the kitchen, specializing in a variety of cuisines."
)

food_critic = Agent(
    role='Food Critic',
    goal='Evaluate the quality of the meal based on taste, presentation, and creativity.',
    verbose=True,
    backstory="The food critic has a refined palate and has reviewed top restaurants worldwide."
)

Step 2: Defining Tasks

We will now define the tasks for the chef and the food critic.

  • Task: Defines what needs to be done and by whom.
  • Context: Ensures that the critic waits for the chef’s task to finish.
Python
prepare_meal_task = Task(
    description='Prepare a meal using the finest ingredients.',
    agent=chef,
    expected_output='A well-prepared and tasty meal.'
)

evaluate_meal_task = Task(
    description='Evaluate the meal based on taste and presentation.',
    agent=food_critic,
    expected_output='A detailed review of the meal.',
    context=[prepare_meal_task]
)

Step 3: Defining Crew

We will bring the agents and tasks together into a crew and set the process to sequential.

  • Crew: Brings agents and tasks together.
  • Process.sequential: Runs tasks one after another.
  • kickoff(): Starts the crew, activating all agents and executing tasks according to the selected process.
Python
cooking_crew = Crew(
    agents=[chef, food_critic],
    tasks=[prepare_meal_task, evaluate_meal_task],
    process=Process.sequential,
    verbose=True
)

cooking_crew.kickoff()

Output:

sequential
Sequential Process

2. Hierarchical Process

Hierarchical Process introduces a manager agent who oversees the process, assigning tasks to the right agents. Instead of tasks being locked in a fixed sequence, the manager looks at the situation and assigns tasks to the right agents. This makes the workflow more flexible and allows the crew to adjust as needed.

It works best in larger or more complex projects where many different roles are involved and coordination is important. We will implement an example of a Construction Crew:

Step 1: Defining Agents

We will define three specialized agents: Foundation Builder, Wall Builder and Roof Builder.

  • Agent: Represents a role with a goal and backstory.
  • Verbose: Enables detailed logs of the process.
Python
construction_worker_1 = Agent(
    role='Foundation Builder',
    goal='Build the foundation of the house.',
    verbose=True,
    backstory="Experienced in laying strong foundations."
)

construction_worker_2 = Agent(
    role='Wall Builder',
    goal='Construct the walls of the house.',
    verbose=True,
    backstory="Specializes in framing and structural integrity."
)

construction_worker_3 = Agent(
    role='Roof Builder',
    goal='Install the roof of the house.',
    verbose=True,
    backstory="Skilled in waterproof roofing."
)

Step 2: Defining Tasks

We will now define the tasks for constructing the foundation, walls and roof.

  • Task: Defines what needs to be done and by whom.
  • Context: Ensures that the critic waits for the chef’s task to finish.
Python
build_foundation_task = Task(
    description='Build the foundation of the house.',
    expected_output='A solid foundation built for the house.'
)

build_walls_task = Task(
    description='Construct the walls of the house.',
    expected_output='The walls are up, ready for the roof.'
)

build_roof_task = Task(
    description='Install the roof of the house.',
    expected_output='The roof is securely in place.'
)

Note: Tasks do not specify agents directly, the manager assigns them.

Step 3: Defining Crew with Manager

We will now create the crew, set the process to hierarchical and assign a manager LLM.

  • Process.hierarchical: Allows a manager to delegate tasks dynamically.
  • manager_llm: Specifies the language model that acts as the manager.
  • kickoff(): Starts the crew, activating all agents and executing tasks according to the selected process.
Python
construction_crew = Crew(
    agents=[construction_worker_1, construction_worker_2, construction_worker_3],
    tasks=[build_foundation_task, build_walls_task, build_roof_task],
    process=Process.hierarchical,
    manager_llm="gpt-4o",
    verbose=True
)

construction_crew.kickoff()

Output:

hirerchial
Hierarchical Process

Difference Between Sequential and Hierarchical Processess

This table highlights the main differences between the two processes and when each is most suitable.

FeatureSequential ProcessHierarchical Process
Task FlowLinear, tasks run in a fixed orderManager decides order dynamically
DependenciesEach task may depend on previous task outputManager oversees dependencies
FlexibilityLess flexible, strictly orderedMore flexible, allows dynamic decision making
Manager RoleNot requiredRequired (via manager agent or LLM)
Best Use CaseWhen tasks strictly depend on each otherWhen tasks need coordination and oversight
Comment

Explore