Workflow State: Share Data Across Steps

ยท

Oct 22, 2025

Workflow state lets you share values across steps without threading them through every inputSchema and outputSchema.

Use it when you need data in Step 5 that was created in Step 1, but Steps 2-4 don't care about it. Or when multiple steps need to access shared configuration, user context, or counters. Instead of passing these values through every intermediate step, put them in state.

How It Works

Define all state values in the workflow's stateSchema:

 1const workflow = createWorkflow({
 2  id: "process-items",
 3  stateSchema: z.object({
 4    processedItems: z.array(z.string()),
 5    userId: z.string()
 6  })
 7});

Each step declares only what it uses:

 1const step1 = createStep({
 2  stateSchema: z.object({ processedItems: z.array(z.string()) }),
 3  execute: async ({ state, setState }) => {
 4    setState({ ...state, processedItems: [...state.processedItems, "item-1"] });
 5    return { done: true };
 6  }
 7});
 8
 9const step3 = createStep({
10  stateSchema: z.object({ userId: z.string() }),
11  execute: async ({ state }) => {
12    return { dashboard: createDashboard(state.userId) };
13  }
14});

Step 2 doesn't declare any state if it doesn't need it. Step 1 and Step 3 only see what they declared. And remember to set initial values using initialState!

Workflow state is available starting in @mastra/core@0.20.1. Happy building! ๐Ÿš€

Stay up to date