Principle:CrewAIInc CrewAI Flow Execution And Visualization
Overview
Flow Execution and Visualization encompasses the runtime execution phase for event-driven flows combined with structural visualization tools that render the flow's execution graph for debugging, documentation, and team communication.
Description
This principle covers two tightly coupled capabilities:
Flow Execution
When flow.kickoff() is called, the runtime engine:
- Initializes state -- Creates or restores the state object, applies any
inputsprovided tokickoff() - Restores checkpoints -- If a UUID is provided and persistence is configured, loads the last saved state and marks completed methods
- Executes start methods -- All methods decorated with
@start()(that have no trigger conditions) are executed concurrently viaasyncio - Propagates results -- When a start method completes, its return value is passed to all downstream
@listenand@routermethods whose conditions are satisfied - Evaluates conditions -- OR conditions fire immediately on the first trigger; AND conditions accumulate completed triggers and fire when all are met
- Routes execution --
@routermethods return string values that select which downstream listeners fire - Continues recursively -- Each listener/router completion triggers its own downstream listeners, forming a cascade through the DAG
- Collects output -- The return value of the last executed method becomes the flow's output
The execution supports both synchronous (kickoff()) and asynchronous (kickoff_async()) entry points. The synchronous version wraps the async implementation with asyncio.run(). Streaming is also supported via FlowStreamingOutput when stream=True.
Flow Visualization
The visualization system generates an interactive HTML page that renders the flow's execution graph:
- Structure extraction --
build_flow_structure()inspects the flow instance's_methods,_listeners,_routers, and_router_pathsto build a graph of nodes and edges - Node classification -- Methods are classified as "start", "listen", or "router" nodes, each with distinct visual styling
- Edge generation -- Decorator relationships become directed edges with labels indicating condition types (OR, AND) and router path labels
- HTML rendering --
render_interactive()generates a self-contained HTML file with an interactive graph visualization - Browser display -- By default, the HTML file is opened in the user's browser for immediate inspection
The visualization captures:
- Method names and their decorator types (start, listen, router)
- Method signatures and source code
- Trigger conditions and condition types
- Router paths and branching logic
- Source file locations and line numbers
Theoretical Basis
Execution Model
Flow execution implements a Graph Execution Engine that traverses the decorator-defined DAG:
| Graph Concept | Flow Mapping |
|---|---|
| Node | Decorated method (@start, @listen, @router)
|
| Edge | Decorator parameter relationship (method reference, string trigger) |
| Root nodes | @start() methods without trigger conditions
|
| Leaf nodes | Methods with no downstream listeners |
| Traversal | Event-driven: method completion triggers downstream evaluation |
| Parallelism | Multiple listeners of the same source execute concurrently |
| Branching | @router return value selects which edges to traverse
|
The execution is non-blocking within the async runtime: when a method triggers multiple listeners, they are launched as concurrent asyncio tasks. This differs from a simple topological sort traversal because the actual execution order depends on runtime performance and I/O timing.
Visualization Model
The visualization follows Graph Visualization principles:
- Nodes are positioned to reflect execution order (start nodes at top, terminal nodes at bottom)
- Edge styles distinguish between normal triggers, AND conditions, and router paths
- Interactive features allow expanding method source code and inspecting signatures
- The HTML output is self-contained with no external dependencies, suitable for embedding in documentation or sharing
Usage
Execution
- Call
flow.kickoff(inputs={...})for synchronous execution - Call
await flow.kickoff_async(inputs={...})for async execution - Set
stream=Trueon the flow class for streaming output - Provide
inputs={"id": "existing-uuid"}to resume from a persistence checkpoint
Visualization
- Call
flow.plot()to generate and open an interactive HTML visualization - Call
flow.plot(filename="my_flow.html", show=False)to generate without opening - Use
build_flow_structure(flow)to get the raw graph structure for programmatic analysis - Use
calculate_execution_paths(structure)to count possible execution paths through the flow
Constraints
kickoff()cannot be called from within a running async event loop; usekickoff_async()instead- Visualization requires the flow to be instantiated (it inspects instance-level method registrations)
- Router paths are only visible in visualization if the router has a
LiteralorEnumreturn type annotation - The HTML visualization is a snapshot at generation time; it does not update during execution
Related Pages
- Implementation:CrewAIInc_CrewAI_Flow_Kickoff_And_Plot
- Principle:CrewAIInc_CrewAI_Flow_Class_Definition -- The decorator-defined graph that execution traverses
- Principle:CrewAIInc_CrewAI_Conditional_Routing -- Routing logic evaluated during execution
- Principle:CrewAIInc_CrewAI_State_Persistence -- Persistence integrated with the execution lifecycle
- Principle:CrewAIInc_CrewAI_State_Model_Design -- State initialized and managed during execution