Quick Use
npm create mastra@latest(scaffolds project + installs deps)- Define Steps with
inputSchema,outputSchema,execute(Zod-typed) - Chain with
new Workflow().step().then().commit(), thencreateRun().start()
Intro
Mastra Workflows is Mastra's typed orchestration primitive — chain .step().then().branch().parallel().while() to build multi-step agent pipelines where TypeScript inferring carries type-safety from the first input to the last output. Best for: production TypeScript agents that need readable, testable, observable workflows. Works with: Mastra 0.5+, Node 20+. Setup time: 5 minutes.
Define a workflow
import { Workflow, Step } from "@mastra/core";
import { z } from "zod";
const fetchData = new Step({
id: "fetch",
inputSchema: z.object({ url: z.string().url() }),
outputSchema: z.object({ markdown: z.string() }),
execute: async ({ context }) => {
const res = await fetch(context.inputData.url);
return { markdown: await res.text() };
},
});
const summarize = new Step({
id: "summarize",
inputSchema: z.object({ markdown: z.string() }),
outputSchema: z.object({ summary: z.string() }),
execute: async ({ context, mastra }) => {
const agent = mastra.getAgent("summarizer");
const result = await agent.generate(`Summarize: ${context.inputData.markdown}`);
return { summary: result.text };
},
});
const workflow = new Workflow({
name: "fetch-and-summarize",
triggerSchema: z.object({ url: z.string().url() }),
}).step(fetchData).then(summarize).commit();Branching
.then(classify)
.branch([
[async ({ context }) => context.results.classify.kind === "bug",
fixerStep],
[async ({ context }) => context.results.classify.kind === "feature",
plannerStep],
])Run and stream
const { runId, start } = workflow.createRun();
start({ triggerData: { url: "https://example.com/blog" } });
// Subscribe to step events
workflow.watch(runId, (event) => {
console.log(event.type, event.payload);
});Each step's input/output is checkpointed — restart from the last successful step if a downstream step fails.
FAQ
Q: Mastra Workflows vs LangGraph? A: LangGraph is Python-first with a graph DSL. Mastra Workflows is TypeScript-first with a fluent chain API. Pick by language preference + ecosystem; both handle the same shape of problems.
Q: Is Mastra free? A: Yes — Mastra is open-source under Elastic License 2.0. Self-hostable. Mastra Cloud (managed deployment + observability) is paid.
Q: Can workflows call other workflows?
A: Yes — step(otherWorkflow) embeds a workflow as a sub-step. Useful for shared sub-flows (e.g. validateAndCleanData reused across pipelines).
Source & Thanks
Built by Mastra. Licensed under Elastic License 2.0.
mastra-ai/mastra — ⭐ 13,000+