Learn how to create and manage AI workflows using CrewAI Flows.
generate_city
and generate_fun_fact
. The generate_city
task is the starting point of the Flow, and the generate_fun_fact
task listens for the output of the generate_city
task.
Each Flow instance automatically receives a unique identifier (UUID) in its state, which helps track and manage flow executions. The state can also store additional data (like the generated city and fun fact) that persists throughout the flow’s execution.
When you run the Flow, it will:
.env
file to store your OPENAI_API_KEY
. This key is necessary for authenticating requests to the OpenAI API.
@start()
decorator is used to mark a method as the starting point of a Flow. When a Flow is started, all the methods decorated with @start()
are executed in parallel. You can have multiple start methods in a Flow, and they will all be executed when the Flow is started.
@listen()
decorator is used to mark a method as a listener for the output of another task in the Flow. The method decorated with @listen()
will be executed when the specified task emits an output. The method can access the output of the task it is listening to as an argument.
@listen()
decorator can be used in several ways:
kickoff()
method returns the output of this final method.
Here’s how you can access the final output:
second_method
is the last method to complete, so its output will be the final output of the Flow.
The kickoff()
method will return the final output, which is then printed to the console. The plot()
method will generate the HTML file, which will help you understand the flow.
first_method
and second_method
.
After the Flow has run, you can access the final state to see the updates made by these methods.
By ensuring that the final method’s output is returned and providing access to the state, CrewAI Flows make it easy to integrate the results of your AI workflows into larger applications or systems,
while also maintaining and accessing the state throughout the Flow’s execution.
state
attribute of the Flow
class.
This approach offers flexibility, enabling developers to add or modify state attributes on the fly without defining a strict schema.
Even with unstructured states, CrewAI Flows automatically generates and maintains a unique identifier (UUID) for each state instance.
id
field is automatically generated and preserved throughout the flow’s execution. You don’t need to manage or set it manually, and it will be maintained even when updating the state with new data.
Key Points:
self.state
without predefined constraints.BaseModel
, developers can define the exact shape of the state, enabling better validation and auto-completion in development environments.
Each state in CrewAI Flows automatically receives a unique identifier (UUID) to help track and manage state instances. This ID is automatically generated and managed by the Flow system.
ExampleState
clearly outlines the state structure, enhancing code readability and maintainability.id
field is automatically added if not presentor
or_
function in Flows allows you to listen to multiple methods and trigger the listener method when any of the specified methods emit an output.
logger
method will be triggered by the output of either the start_method
or the second_method
.
The or_
function is used to listen to multiple methods and trigger the listener method when any of the specified methods emit an output.
and
and_
function in Flows allows you to listen to multiple methods and trigger the listener method only when all the specified methods emit an output.
logger
method will be triggered only when both the start_method
and the second_method
emit an output.
The and_
function is used to listen to multiple methods and trigger the listener method only when all the specified methods emit an output.
@router()
decorator in Flows allows you to define conditional routing logic based on the output of a method.
You can specify different routes based on the output of the method, allowing you to control the flow of execution dynamically.
start_method
generates a random boolean value and sets it in the state.
The second_method
uses the @router()
decorator to define conditional routing logic based on the value of the boolean.
If the boolean is True
, the method returns "success"
, and if it is False
, the method returns "failed"
.
The third_method
and fourth_method
listen to the output of the second_method
and execute based on the returned value.
When you run this Flow, the output will change based on the random boolean value generated by the start_method
.
MarketAnalysis
) ensures type safety and structured data throughout the flow.
MarketResearchState
) maintains context between steps and stores both inputs and outputs.
WebsiteSearchTool
) to enhance their capabilities.
poem_crew
that is already working. You can use this crew as a template by copying, pasting, and editing it to create other crews.
crewai create flow name_of_flow
command, you will see a folder structure similar to the following:
Directory/File | Description |
---|---|
name_of_flow/ | Root directory for the flow. |
├── crews/ | Contains directories for specific crews. |
│ └── poem_crew/ | Directory for the “poem_crew” with its configurations and scripts. |
│ ├── config/ | Configuration files directory for the “poem_crew”. |
│ │ ├── agents.yaml | YAML file defining the agents for “poem_crew”. |
│ │ └── tasks.yaml | YAML file defining the tasks for “poem_crew”. |
│ ├── poem_crew.py | Script for “poem_crew” functionality. |
├── tools/ | Directory for additional tools used in the flow. |
│ └── custom_tool.py | Custom tool implementation. |
├── main.py | Main script for running the flow. |
├── README.md | Project description and instructions. |
├── pyproject.toml | Configuration file for project dependencies and settings. |
└── .gitignore | Specifies files and directories to ignore in version control. |
crews
folder, you can define multiple crews. Each crew will have its own folder containing configuration files and the crew definition file. For example, the poem_crew
folder contains:
config/agents.yaml
: Defines the agents for the crew.config/tasks.yaml
: Defines the tasks for the crew.poem_crew.py
: Contains the crew definition, including agents, tasks, and the crew itself.poem_crew
to create other crews.
main.py
main.py
file is where you create your flow and connect the crews together. You can define your flow by using the Flow
class and the decorators @start
and @listen
to specify the flow of execution.
Here’s an example of how you can connect the poem_crew
in the main.py
file:
PoemFlow
class defines a flow that generates a sentence count, uses the PoemCrew
to generate a poem, and then saves the poem to a file. The flow is kicked off by calling the kickoff()
method. The PoemFlowPlot will be generated by plot()
method.
plot()
Methodplot()
method on your flow object. This method will create an HTML file containing the interactive plot of your flow.
my_flow_plot.html
in your current directory. You can open this file in a web browser to view the interactive plot.
plot()
method. The file will be saved in your project directory, and you can open it in a web browser to explore the flow.
plot()
method or the command line, generating plots will provide you with a visual representation of your workflows, aiding in both development and presentation.
kickoff()
method:
crewai run
command:
type = "flow"
setting in your pyproject.toml) and runs it accordingly. This is the recommended way to run flows from the command line.
For backward compatibility, you can also use:
crewai run
command is now the preferred method as it works for both crews and flows.