Skip to main content

Workflows

Mastra discovers project-level workflows from .ts and .js files under src/mastra/workflows/. This convention isn't scoped to one agent: every file with a default export becomes a registered workflow, and the filename becomes the workflow key.

Use this page for the file-based convention. For steps, schemas, control flow, streaming, state, and workflow execution, see Workflows overview.

Quickstart
Direct link to Quickstart

Create a file under src/mastra/workflows/, build the workflow with createWorkflow(), and default-export it.

src/mastra/workflows/daily-report.ts
import { createStep, createWorkflow } from '@mastra/core/workflows'
import { z } from 'zod'

const collectInputs = createStep({
id: 'collect-inputs',
inputSchema: z.object({ topic: z.string() }),
outputSchema: z.object({ topic: z.string(), audience: z.string() }),
execute: async ({ inputData }) => ({
topic: inputData.topic,
audience: 'engineering',
}),
})

const writeSummary = createStep({
id: 'write-summary',
inputSchema: z.object({ topic: z.string(), audience: z.string() }),
outputSchema: z.object({ report: z.string() }),
execute: async ({ inputData }) => ({
report: `Daily report about ${inputData.topic} for ${inputData.audience}.`,
}),
})

const dailyReport = createWorkflow({
id: 'daily-report',
inputSchema: z.object({ topic: z.string() }),
outputSchema: z.object({ report: z.string() }),
})
.then(collectInputs)
.then(writeSummary)
.commit()

export default dailyReport

Mastra registers this workflow under the daily-report key.

Precedence with code
Direct link to Precedence with code

File-based workflows merge with workflows registered in new Mastra(). On a key collision, the code-registered workflow wins.

On this page