Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Principle:CrewAIInc CrewAI Flow Execution And Visualization

From Leeroopedia

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:

  1. Initializes state -- Creates or restores the state object, applies any inputs provided to kickoff()
  2. Restores checkpoints -- If a UUID is provided and persistence is configured, loads the last saved state and marks completed methods
  3. Executes start methods -- All methods decorated with @start() (that have no trigger conditions) are executed concurrently via asyncio
  4. Propagates results -- When a start method completes, its return value is passed to all downstream @listen and @router methods whose conditions are satisfied
  5. Evaluates conditions -- OR conditions fire immediately on the first trigger; AND conditions accumulate completed triggers and fire when all are met
  6. Routes execution -- @router methods return string values that select which downstream listeners fire
  7. Continues recursively -- Each listener/router completion triggers its own downstream listeners, forming a cascade through the DAG
  8. 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:

  1. Structure extraction -- build_flow_structure() inspects the flow instance's _methods, _listeners, _routers, and _router_paths to build a graph of nodes and edges
  2. Node classification -- Methods are classified as "start", "listen", or "router" nodes, each with distinct visual styling
  3. Edge generation -- Decorator relationships become directed edges with labels indicating condition types (OR, AND) and router path labels
  4. HTML rendering -- render_interactive() generates a self-contained HTML file with an interactive graph visualization
  5. 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=True on 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; use kickoff_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 Literal or Enum return type annotation
  • The HTML visualization is a snapshot at generation time; it does not update during execution

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment