Skip to Content
ExamplesWorkflowsSequential Steps

Sequential Execution

Many workflows involve executing steps one after another in a defined order. This example demonstrates how to use .then() to build a simple sequential workflow where the output of one step becomes the input of the next.

Sequential execution using steps

In this example, the workflow runs step1 and step2 in sequence, passing the input through each step and returning the final result from step2.

src/mastra/workflows/example-sequential-steps.ts
import { createWorkflow, createStep } from "@mastra/core/workflows"; import { z } from "zod"; const step1 = createStep({ id: "step-1", description: "passes value from input to output", inputSchema: z.object({ value: z.number() }), outputSchema: z.object({ value: z.number() }), execute: async ({ inputData }) => { const { value } = inputData; return { value }; } }); const step2 = createStep({ id: "step-2", description: "passes value from input to output", inputSchema: z.object({ value: z.number() }), outputSchema: z.object({ value: z.number() }), execute: async ({ inputData }) => { const { value } = inputData; return { value }; } }); export const sequentialSteps = createWorkflow({ id: "sequential-workflow", inputSchema: z.object({ value: z.number() }), outputSchema: z.object({ value: z.number() }) }) .then(step1) .then(step2) .commit();