Key Concepts
Workflows in Mastra are composed of:- Steps: Individual units of work with defined inputs and outputs
- Control Flow: Patterns like sequential (
.then()), parallel (.parallel()), and conditional (.branch()) execution - State Management: Shared state across all steps in a workflow
- Suspend/Resume: Human-in-the-loop capabilities for pausing and resuming execution
- Type Safety: Full TypeScript support with schema validation using Zod
Basic Workflow Structure
All workflows must call
.commit() before they can be executed. This finalizes the workflow graph.Graph-Based Execution
Unlike simple linear pipelines, Mastra workflows use a graph-based execution engine that supports:- Non-linear flows: Branch based on conditions, execute steps in parallel
- Dynamic routing: Choose execution paths at runtime
- State persistence: Save and restore workflow state at any point
- Time travel: Resume from any previous step in the workflow
Workflow Lifecycle
- Define: Create workflow with input/output schemas and steps
- Commit: Finalize the workflow graph structure
- Execute: Run the workflow with input data
- Monitor: Track step-by-step execution progress
- Resume: Continue suspended workflows with additional data
Features
Type-Safe Schema Validation
All workflow inputs, outputs, and step data are validated using Zod schemas, providing compile-time and runtime type safety.Execution Context
Every step receives an execution context with:inputData: Data passed to this stepstate: Shared workflow statesetState: Update workflow stategetStepResult(): Access outputs from previous stepsmastra: Access to the Mastra instancerequestContext: Request-scoped data
Observability
Workflows are instrumented with:- Distributed tracing support
- Step-level logging
- Error tracking
- Performance metrics
Next Steps
Creating Workflows
Learn how to define and configure workflows
Workflow Steps
Define steps with schemas and execute functions
Control Flow
Use .then(), .branch(), and .parallel() patterns
Suspend & Resume
Implement human-in-the-loop workflows