CrewAI Collaboration (original) (raw)

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:

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.

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.

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.

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.

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.

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.

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.

Feature Sequential Process Hierarchical Process
Task Flow Linear, tasks run in a fixed order Manager decides order dynamically
Dependencies Each task may depend on previous task output Manager oversees dependencies
Flexibility Less flexible, strictly ordered More flexible, allows dynamic decision making
Manager Role Not required Required (via manager agent or LLM)
Best Use Case When tasks strictly depend on each other When tasks need coordination and oversight