Learn how to create structured, event-driven workflows with precise control over execution.
CrewAI Flows represent the next level in AI orchestration - combining the collaborative power of AI agent crews with the precision and flexibility of procedural programming. While crews excel at agent collaboration, flows give you fine-grained control over exactly how and when different components of your AI system interact.
In this guide, we’ll walk through creating a powerful CrewAI Flow that generates a comprehensive learning guide on any topic. This tutorial will demonstrate how Flows provide structured, event-driven control over your AI workflows by combining regular code, direct LLM calls, and crew-based processing.
Flows enable you to:
By the end of this guide, you’ll have:
This guide creator flow demonstrates fundamental patterns that can be applied to create much more advanced applications, such as:
Let’s dive in and build your first flow!
Before starting, make sure you have:
First, let’s create a new CrewAI Flow project using the CLI. This command sets up a scaffolded project with all the necessary directories and template files for your flow.
This will generate a project with the basic structure needed for your flow.
CrewAI Framework Overview
The generated project has the following structure. Take a moment to familiarize yourself with it, as understanding this structure will help you create more complex flows in the future.
This structure provides a clear separation between different components of your flow:
main.py
filecrews
directorytools
directoryWe’ll modify this structure to create our guide creator flow, which will orchestrate the process of generating comprehensive learning guides.
Our flow will need a specialized crew to handle the content creation process. Let’s use the CrewAI CLI to add a content writer crew:
This command automatically creates the necessary directories and template files for your crew. The content writer crew will be responsible for writing and reviewing sections of our guide, working within the overall flow orchestrated by our main application.
Now, let’s modify the generated files for the content writer crew. We’ll set up two specialized agents - a writer and a reviewer - that will collaborate to create high-quality content for our guide.
First, update the agents configuration file to define our content creation team:
Remember to set llm
to the provider you are using.
These agent definitions establish the specialized roles and perspectives that will shape how our AI agents approach content creation. Notice how each agent has a distinct purpose and expertise.
These task definitions provide detailed instructions to our agents, ensuring they produce content that meets our quality standards. Note how the context
parameter in the review task creates a workflow where the reviewer has access to the writer’s output.
This crew definition establishes the relationship between our agents and tasks, setting up a sequential process where the content writer creates a draft and then the reviewer improves it. While this crew can function independently, in our flow it will be orchestrated as part of a larger system.
Now comes the exciting part - creating the flow that will orchestrate the entire guide creation process. This is where we’ll combine regular Python code, direct LLM calls, and our content creation crew into a cohesive system.
Our flow will:
Let’s create our flow in the main.py
file:
Let’s analyze what’s happening in this flow:
@start()
decorator@listen()
decorator to establish event-driven relationships between stepsThis is the power of flows - combining different types of processing (user interaction, direct LLM calls, crew-based tasks) into a coherent, event-driven system.
Create a .env
file in your project root with your API keys. See the LLM setup
guide for details on configuring a provider.
Install the required dependencies:
Now it’s time to see your flow in action! Run it using the CrewAI CLI:
When you run this command, you’ll see your flow spring to life:
This demonstrates the power of flows to orchestrate complex processes involving multiple components, both AI and non-AI.
One of the powerful features of flows is the ability to visualize their structure:
This will create an HTML file that shows the structure of your flow, including the relationships between different steps and the data that flows between them. This visualization can be invaluable for understanding and debugging complex flows.
Once the flow completes, you’ll find two files in the output
directory:
guide_outline.json
: Contains the structured outline of the guidecomplete_guide.md
: The comprehensive guide with all sectionsTake a moment to review these files and appreciate what you’ve built - a system that combines user input, direct AI interactions, and collaborative agent work to produce a complex, high-quality output.
What you’ve learned in this guide provides a foundation for creating much more sophisticated AI systems. Here are some ways you could extend this basic flow:
You could create more interactive flows with:
You could expand your flow with additional steps for:
You could implement more sophisticated flow patterns:
The same patterns can be applied to create flows for:
This guide creator flow demonstrates several powerful features of CrewAI:
@listen
decorator to respond to eventsLet’s break down the key components of flows to help you understand how to build your own:
Flows allow you to make direct calls to language models when you need simple, structured responses:
This is more efficient than using a crew when you need a specific, structured output.
Flows use decorators to establish relationships between components:
This creates a clear, declarative structure for your application.
Flows maintain state across steps, making it easy to share data:
This provides a type-safe way to track and transform data throughout your flow.
Flows can seamlessly integrate with crews for complex collaborative tasks:
This allows you to use the right tool for each part of your application - direct LLM calls for simple tasks and crews for complex collaboration.
Now that you’ve built your first flow, you can:
@router()
to create conditional branches in your flowsand_
and or_
functions for more complex parallel executionCongratulations! You’ve successfully built your first CrewAI Flow that combines regular code, direct LLM calls, and crew-based processing to create a comprehensive guide. These foundational skills enable you to create increasingly sophisticated AI applications that can tackle complex, multi-stage problems through a combination of procedural control and collaborative intelligence.
Learn how to create structured, event-driven workflows with precise control over execution.
CrewAI Flows represent the next level in AI orchestration - combining the collaborative power of AI agent crews with the precision and flexibility of procedural programming. While crews excel at agent collaboration, flows give you fine-grained control over exactly how and when different components of your AI system interact.
In this guide, we’ll walk through creating a powerful CrewAI Flow that generates a comprehensive learning guide on any topic. This tutorial will demonstrate how Flows provide structured, event-driven control over your AI workflows by combining regular code, direct LLM calls, and crew-based processing.
Flows enable you to:
By the end of this guide, you’ll have:
This guide creator flow demonstrates fundamental patterns that can be applied to create much more advanced applications, such as:
Let’s dive in and build your first flow!
Before starting, make sure you have:
First, let’s create a new CrewAI Flow project using the CLI. This command sets up a scaffolded project with all the necessary directories and template files for your flow.
This will generate a project with the basic structure needed for your flow.
CrewAI Framework Overview
The generated project has the following structure. Take a moment to familiarize yourself with it, as understanding this structure will help you create more complex flows in the future.
This structure provides a clear separation between different components of your flow:
main.py
filecrews
directorytools
directoryWe’ll modify this structure to create our guide creator flow, which will orchestrate the process of generating comprehensive learning guides.
Our flow will need a specialized crew to handle the content creation process. Let’s use the CrewAI CLI to add a content writer crew:
This command automatically creates the necessary directories and template files for your crew. The content writer crew will be responsible for writing and reviewing sections of our guide, working within the overall flow orchestrated by our main application.
Now, let’s modify the generated files for the content writer crew. We’ll set up two specialized agents - a writer and a reviewer - that will collaborate to create high-quality content for our guide.
First, update the agents configuration file to define our content creation team:
Remember to set llm
to the provider you are using.
These agent definitions establish the specialized roles and perspectives that will shape how our AI agents approach content creation. Notice how each agent has a distinct purpose and expertise.
These task definitions provide detailed instructions to our agents, ensuring they produce content that meets our quality standards. Note how the context
parameter in the review task creates a workflow where the reviewer has access to the writer’s output.
This crew definition establishes the relationship between our agents and tasks, setting up a sequential process where the content writer creates a draft and then the reviewer improves it. While this crew can function independently, in our flow it will be orchestrated as part of a larger system.
Now comes the exciting part - creating the flow that will orchestrate the entire guide creation process. This is where we’ll combine regular Python code, direct LLM calls, and our content creation crew into a cohesive system.
Our flow will:
Let’s create our flow in the main.py
file:
Let’s analyze what’s happening in this flow:
@start()
decorator@listen()
decorator to establish event-driven relationships between stepsThis is the power of flows - combining different types of processing (user interaction, direct LLM calls, crew-based tasks) into a coherent, event-driven system.
Create a .env
file in your project root with your API keys. See the LLM setup
guide for details on configuring a provider.
Install the required dependencies:
Now it’s time to see your flow in action! Run it using the CrewAI CLI:
When you run this command, you’ll see your flow spring to life:
This demonstrates the power of flows to orchestrate complex processes involving multiple components, both AI and non-AI.
One of the powerful features of flows is the ability to visualize their structure:
This will create an HTML file that shows the structure of your flow, including the relationships between different steps and the data that flows between them. This visualization can be invaluable for understanding and debugging complex flows.
Once the flow completes, you’ll find two files in the output
directory:
guide_outline.json
: Contains the structured outline of the guidecomplete_guide.md
: The comprehensive guide with all sectionsTake a moment to review these files and appreciate what you’ve built - a system that combines user input, direct AI interactions, and collaborative agent work to produce a complex, high-quality output.
What you’ve learned in this guide provides a foundation for creating much more sophisticated AI systems. Here are some ways you could extend this basic flow:
You could create more interactive flows with:
You could expand your flow with additional steps for:
You could implement more sophisticated flow patterns:
The same patterns can be applied to create flows for:
This guide creator flow demonstrates several powerful features of CrewAI:
@listen
decorator to respond to eventsLet’s break down the key components of flows to help you understand how to build your own:
Flows allow you to make direct calls to language models when you need simple, structured responses:
This is more efficient than using a crew when you need a specific, structured output.
Flows use decorators to establish relationships between components:
This creates a clear, declarative structure for your application.
Flows maintain state across steps, making it easy to share data:
This provides a type-safe way to track and transform data throughout your flow.
Flows can seamlessly integrate with crews for complex collaborative tasks:
This allows you to use the right tool for each part of your application - direct LLM calls for simple tasks and crews for complex collaboration.
Now that you’ve built your first flow, you can:
@router()
to create conditional branches in your flowsand_
and or_
functions for more complex parallel executionCongratulations! You’ve successfully built your first CrewAI Flow that combines regular code, direct LLM calls, and crew-based processing to create a comprehensive guide. These foundational skills enable you to create increasingly sophisticated AI applications that can tackle complex, multi-stage problems through a combination of procedural control and collaborative intelligence.