Build Your First Flow
Learn how to create structured, event-driven workflows with precise control over execution.
Build Your First Flow
Taking Control of AI Workflows with Flows
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.
What Makes Flows Powerful
Flows enable you to:
- Combine different AI interaction patterns - Use crews for complex collaborative tasks, direct LLM calls for simpler operations, and regular code for procedural logic
- Build event-driven systems - Define how components respond to specific events and data changes
- Maintain state across components - Share and transform data between different parts of your application
- Integrate with external systems - Seamlessly connect your AI workflow with databases, APIs, and user interfaces
- Create complex execution paths - Design conditional branches, parallel processing, and dynamic workflows
What You’ll Build and Learn
By the end of this guide, you’ll have:
- Created a sophisticated content generation system that combines user input, AI planning, and multi-agent content creation
- Orchestrated the flow of information between different components of your system
- Implemented event-driven architecture where each step responds to the completion of previous steps
- Built a foundation for more complex AI applications that you can expand and customize
This guide creator flow demonstrates fundamental patterns that can be applied to create much more advanced applications, such as:
- Interactive AI assistants that combine multiple specialized subsystems
- Complex data processing pipelines with AI-enhanced transformations
- Autonomous agents that integrate with external services and APIs
- Multi-stage decision-making systems with human-in-the-loop processes
Let’s dive in and build your first flow!
Prerequisites
Before starting, make sure you have:
- Installed CrewAI following the installation guide
- Set up your OpenAI API key in your environment variables
- Basic understanding of Python
Step 1: Create a New CrewAI Flow Project
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
Step 2: Understanding the Project Structure
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:
- The main flow logic in the
main.py
file - Specialized crews in the
crews
directory - Custom tools in the
tools
directory
We’ll modify this structure to create our guide creator flow, which will orchestrate the process of generating comprehensive learning guides.
Step 3: Add a Content Writer Crew
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.
Step 4: Configure the Content Writer Crew
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:
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.
- Next, update the tasks configuration file to define the specific writing and reviewing tasks:
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.
- Now, update the crew implementation file to define how our agents and tasks work together:
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.
Step 5: Create the Flow
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:
- Get user input for a topic and audience level
- Make a direct LLM call to create a structured guide outline
- Process each section sequentially using the content writer crew
- Combine everything into a final comprehensive document
Let’s create our flow in the main.py
file:
Let’s analyze what’s happening in this flow:
- We define Pydantic models for structured data, ensuring type safety and clear data representation
- We create a state class to maintain data across different steps of the flow
- We implement three main flow steps:
- Getting user input with the
@start()
decorator - Creating a guide outline with a direct LLM call
- Processing sections with our content crew
- Getting user input with the
- We use the
@listen()
decorator to establish event-driven relationships between steps
This is the power of flows - combining different types of processing (user interaction, direct LLM calls, crew-based tasks) into a coherent, event-driven system.
Step 6: Set Up Your Environment Variables
Create a .env
file in your project root with your API keys:
Step 7: Install Dependencies
Install the required dependencies:
Step 8: Run Your Flow
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:
- It will prompt you for a topic and audience level
- It will create a structured outline for your guide
- It will process each section, with the content writer and reviewer collaborating on each
- Finally, it will compile everything into a comprehensive guide
This demonstrates the power of flows to orchestrate complex processes involving multiple components, both AI and non-AI.
Step 9: Visualize Your Flow
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.
Step 10: Review the Output
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 sections
Take 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.
The Art of the Possible: Beyond Your First Flow
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:
Enhancing User Interaction
You could create more interactive flows with:
- Web interfaces for input and output
- Real-time progress updates
- Interactive feedback and refinement loops
- Multi-stage user interactions
Adding More Processing Steps
You could expand your flow with additional steps for:
- Research before outline creation
- Image generation for illustrations
- Code snippet generation for technical guides
- Final quality assurance and fact-checking
Creating More Complex Flows
You could implement more sophisticated flow patterns:
- Conditional branching based on user preferences or content type
- Parallel processing of independent sections
- Iterative refinement loops with feedback
- Integration with external APIs and services
Applying to Different Domains
The same patterns can be applied to create flows for:
- Interactive storytelling: Create personalized stories based on user input
- Business intelligence: Process data, generate insights, and create reports
- Product development: Facilitate ideation, design, and planning
- Educational systems: Create personalized learning experiences
Key Features Demonstrated
This guide creator flow demonstrates several powerful features of CrewAI:
- User interaction: The flow collects input directly from the user
- Direct LLM calls: Uses the LLM class for efficient, single-purpose AI interactions
- Structured data with Pydantic: Uses Pydantic models to ensure type safety
- Sequential processing with context: Writes sections in order, providing previous sections for context
- Multi-agent crews: Leverages specialized agents (writer and reviewer) for content creation
- State management: Maintains state across different steps of the process
- Event-driven architecture: Uses the
@listen
decorator to respond to events
Understanding the Flow Structure
Let’s break down the key components of flows to help you understand how to build your own:
1. Direct LLM Calls
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.
2. Event-Driven Architecture
Flows use decorators to establish relationships between components:
This creates a clear, declarative structure for your application.
3. State Management
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.
4. Crew Integration
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.
Next Steps
Now that you’ve built your first flow, you can:
- Experiment with more complex flow structures and patterns
- Try using
@router()
to create conditional branches in your flows - Explore the
and_
andor_
functions for more complex parallel execution - Connect your flow to external APIs, databases, or user interfaces
- Combine multiple specialized crews in a single flow
Congratulations! 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.
Was this page helpful?