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
Creating Crews
There are two ways to create crews in CrewAI: using YAML configuration (recommended) or defining them directly in code.YAML Configuration (Recommended)
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 fromCrewBase and uses decorators to define agents, tasks, and the crew itself.
Example Crew Class with Decorators
code
code
Tasks will be executed in the order they are defined.
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 anAgentobject.@task: Denotes a method that returns aTaskobject.@crew: Denotes the method that returns theCrewobject.@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.
Direct Code Definition (Alternative)
Alternatively, you can define the crew directly in code without using YAML configuration files.code
code
- 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 theCrewOutput 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 theoutput 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 settingoutput_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 theusage_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_llmormanager_agentis 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 thekickoff() 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
Streaming Crew Execution
For real-time visibility into crew execution, you can enable streaming to receive output as it’s generated:Code
Replaying from a Specific Task
You can now replay from a specific task using our CLI commandreplay.
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:- Open your terminal or command prompt.
- Navigate to the directory where your CrewAI project is located.
- Run the following command:
