Skip to main content
Mastra workflows provide a powerful, type-safe way to orchestrate complex multi-step processes in your AI applications. Built on a graph-based execution model, workflows enable you to compose steps with sophisticated control flow patterns.

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

import { createWorkflow, createStep } from '@mastra/core';
import { z } from 'zod';

const myWorkflow = createWorkflow({
  id: 'my-workflow',
  inputSchema: z.object({
    input: z.string(),
  }),
  outputSchema: z.object({
    result: z.string(),
  }),
})
  .then(step1)
  .then(step2)
  .commit();
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

  1. Define: Create workflow with input/output schemas and steps
  2. Commit: Finalize the workflow graph structure
  3. Execute: Run the workflow with input data
  4. Monitor: Track step-by-step execution progress
  5. Resume: Continue suspended workflows with additional data
// Define
const workflow = createWorkflow({ ... })
  .then(step1)
  .commit();

// Execute
const run = await workflow.execute({
  inputData: { input: 'Hello' },
});

// Monitor
const result = await run.result();
console.log(result.status); // 'success' | 'failed' | 'suspended'

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 step
  • state: Shared workflow state
  • setState: Update workflow state
  • getStepResult(): Access outputs from previous steps
  • mastra: Access to the Mastra instance
  • requestContext: 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

Build docs developers (and LLMs) love