Skip to main content

Overview

A crew in crewAI represents a collaborative group of agents working together to achieve a set of tasks. Each crew defines the strategy for task execution, agent collaboration, and the overall workflow.

Crew Attributes

Crew Max RPM: The max_rpm attribute sets the maximum number of requests per minute the crew can perform to avoid rate limits and will override individual agents’ max_rpm settings if you set it.

Creating Crews

There are two ways to create crews in CrewAI: using YAML configuration (recommended) or defining them directly in code. Using YAML configuration provides a cleaner, more maintainable way to define crews and is consistent with how agents and tasks are defined in CrewAI projects. After creating your CrewAI project as outlined in the Installation section, you can define your crew in a class that inherits from CrewBase and uses decorators to define agents, tasks, and the crew itself.

Example Crew Class with Decorators

code
How to run the above code:
code
Tasks will be executed in the order they are defined.
The CrewBase class, along with these decorators, automates the collection of agents and tasks, reducing the need for manual management.

Decorators overview from annotations.py

CrewAI provides several decorators in the annotations.py file that are used to mark methods within your crew class for special handling:
  • @CrewBase: Marks the class as a crew base class.
  • @agent: Denotes a method that returns an Agent object.
  • @task: Denotes a method that returns a Task object.
  • @crew: Denotes the method that returns the Crew object.
  • @before_kickoff: (Optional) Marks a method to be executed before the crew starts.
  • @after_kickoff: (Optional) Marks a method to be executed after the crew finishes.
These decorators help in organizing your crew’s structure and automatically collecting agents and tasks without manually listing them.

Direct Code Definition (Alternative)

Alternatively, you can define the crew directly in code without using YAML configuration files.
code
How to run the above code:
code
In this example:
  • Agents and tasks are defined directly within the class without decorators.
  • We manually create and manage the list of agents and tasks.
  • This approach provides more control but can be less maintainable for larger projects.

Crew Output

The output of a crew in the CrewAI framework is encapsulated within the CrewOutput class. This class provides a structured way to access results of the crew’s execution, including various formats such as raw strings, JSON, and Pydantic models. The CrewOutput includes the results from the final task output, token usage, and individual task outputs.

Crew Output Attributes

Crew Output Methods and Properties

Accessing Crew Outputs

Once a crew has been executed, its output can be accessed through the output attribute of the Crew object. The CrewOutput class provides various ways to interact with and present this output.

Example

Code

Accessing Crew Logs

You can see real time log of the crew execution, by setting output_log_file as a True(Boolean) or a file_name(str). Supports logging of events as both file_name.txt and file_name.json. In case of True(Boolean) will save as logs.txt. In case of output_log_file is set as False(Boolean) or None, the logs will not be populated.
Code

Memory Utilization

Crews can utilize memory (short-term, long-term, and entity memory) to enhance their execution and learning over time. This feature allows crews to store and recall execution memories, aiding in decision-making and task execution strategies.

Cache Utilization

Caches can be employed to store the results of tools’ execution, making the process more efficient by reducing the need to re-execute identical tasks.

Crew Usage Metrics

After the crew execution, you can access the usage_metrics attribute to view the language model (LLM) usage metrics for all tasks executed by the crew. This provides insights into operational efficiency and areas for improvement.
Code

Crew Execution Process

  • Sequential Process: Tasks are executed one after another, allowing for a linear flow of work.
  • Hierarchical Process: A manager agent coordinates the crew, delegating tasks and validating outcomes before proceeding. Note: A manager_llm or manager_agent is required for this process and it’s essential for validating the process flow.

Kicking Off a Crew

Once your crew is assembled, initiate the workflow with the kickoff() method. This starts the execution process according to the defined process flow.
Code

Different Ways to Kick Off a Crew

Once your crew is assembled, initiate the workflow with the appropriate kickoff method. CrewAI provides several methods for better control over the kickoff process.

Synchronous Methods

  • kickoff(): Starts the execution process according to the defined process flow.
  • kickoff_for_each(): Executes tasks sequentially for each provided input event or item in the collection.

Asynchronous Methods

CrewAI offers two approaches for async execution:
For high-concurrency workloads, akickoff() and akickoff_for_each() are recommended as they use native async for task execution, memory operations, and knowledge retrieval.
Code
These methods provide flexibility in how you manage and execute tasks within your crew, allowing for both synchronous and asynchronous workflows tailored to your needs. For detailed async examples, see the Kickoff Crew Asynchronously guide.

Streaming Crew Execution

For real-time visibility into crew execution, you can enable streaming to receive output as it’s generated:
Code
Learn more about streaming in the Streaming Crew Execution guide.

Replaying from a Specific Task

You can now replay from a specific task using our CLI command replay. The replay feature in CrewAI allows you to replay from a specific task using the command-line interface (CLI). By running the command crewai replay -t <task_id>, you can specify the task_id for the replay process. Kickoffs will now save the latest kickoffs returned task outputs locally for you to be able to replay from.

Replaying from a Specific Task Using the CLI

To use the replay feature, follow these steps:
  1. Open your terminal or command prompt.
  2. Navigate to the directory where your CrewAI project is located.
  3. Run the following command:
To view the latest kickoff task IDs, use:
Then, to replay from a specific task, use:
These commands let you replay from your latest kickoff tasks, still retaining context from previously executed tasks.