Skip to main content

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:
  1. Combine different AI interaction patterns - Use crews for complex collaborative tasks, direct LLM calls for simpler operations, and regular code for procedural logic
  2. Build event-driven systems - Define how components respond to specific events and data changes
  3. Maintain state across components - Share and transform data between different parts of your application
  4. Integrate with external systems - Seamlessly connect your AI workflow with databases, APIs, and user interfaces
  5. 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:
  1. Created a sophisticated content generation system that combines user input, AI planning, and multi-agent content creation
  2. Orchestrated the flow of information between different components of your system
  3. Implemented event-driven architecture where each step responds to the completion of previous steps
  4. 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:
  1. Installed CrewAI following the installation guide
  2. Set up your LLM API key in your environment, following the LLM setup guide
  3. 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. The starter embedded crew uses the classic Python/YAML layout, and in Step 4 we will replace the content crew with a JSONC crew.
This structure provides a clear separation between different components of your flow:
  • The main flow logic in the src/guide_creator_flow/main.py file
  • Specialized crews in the src/guide_creator_flow/crews directory
  • Custom tools in the src/guide_creator_flow/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 configure the content writer crew with JSONC. We’ll set up two specialized agents - a writer and a reviewer - that collaborate to create high-quality content for our guide.
  1. Create src/guide_creator_flow/crews/content_crew/agents/content_writer.jsonc:
  1. Create src/guide_creator_flow/crews/content_crew/agents/content_reviewer.jsonc:
Replace provider/model-id with the model you use, for example openai/gpt-4o, gemini/gemini-2.0-flash-001, or anthropic/claude-sonnet-4-6.
  1. Create src/guide_creator_flow/crews/content_crew/crew.jsonc:
The context field lets the reviewer use the writer’s output.
  1. Replace src/guide_creator_flow/crews/content_crew/content_crew.py with a small loader:
This loader turns crew.jsonc into a Crew at runtime. 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:
  1. Get user input for a topic and audience level
  2. Make a direct LLM call to create a structured guide outline
  3. Process each section sequentially using the content writer crew
  4. 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:
  1. We define Pydantic models for structured data, ensuring type safety and clear data representation
  2. We create a state class to maintain data across different steps of the flow
  3. 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
  4. 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. See the LLM setup guide for details on configuring a provider.
.env

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:
  1. It will prompt you for a topic and audience level
  2. It will create a structured outline for your guide
  3. It will process each section, with the content writer and reviewer collaborating on each
  4. 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:
  1. guide_outline.json: Contains the structured outline of the guide
  2. complete_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:
  1. User interaction: The flow collects input directly from the user
  2. Direct LLM calls: Uses the LLM class for efficient, single-purpose AI interactions
  3. Structured data with Pydantic: Uses Pydantic models to ensure type safety
  4. Sequential processing with context: Writes sections in order, providing previous sections for context
  5. Multi-agent crews: Leverages specialized agents (writer and reviewer) for content creation
  6. State management: Maintains state across different steps of the process
  7. 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:
  1. Experiment with more complex flow structures and patterns
  2. Try using @router() to create conditional branches in your flows
  3. Explore the and_ and or_ functions for more complex parallel execution
  4. Connect your flow to external APIs, databases, or user interfaces
  5. Combine multiple specialized crews in a single flow
  6. Build multi-turn chat apps with Conversational Flows (kickoff per message, ChatSession, deferred tracing)
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.