Workflow.agent()
The .agent() method adds an agent as a declarative step. The step accepts { prompt: string } as input and returns { text: string } by default. Use .map() before the agent to build the prompt from workflow data.
Unlike wrapping an agent with createStep(), .agent() records a declarative entry in the workflow graph. This makes the workflow portable: the same graph can be serialized and persisted as a stored workflow.
Usage exampleDirect link to Usage example
workflow
.map({ prompt: mapVariable({ initData: workflow, path: "topic" }) })
.agent(testAgent)
.commit();
ParametersDirect link to Parameters
agentOrId:
options?:
maxSteps, modelSettings, memory, and providerOptions, plus step-level retries, scorers, and metadata. Per-request fields such as requestContext, resourceId, threadId, and onStepFinish are managed by the workflow engine and excluded.stepOptions?:
ReturnsDirect link to Returns
workflow:
Structured outputDirect link to Structured output
By default the step's output is { text: string }. Pass structuredOutput.schema to make the step return that shape instead. The schema becomes the step's output schema, so later steps chain against it with full type safety:
workflow
.agent(testAgent, {
structuredOutput: {
schema: z.object({
subtopics: z.array(z.string()),
}),
},
})
.commit();
Referencing an agent by IDDirect link to Referencing an agent by ID
Pass a string to reference a registered agent without importing it. The agent must be registered on the Mastra instance when the workflow runs:
workflow.agent("test-agent", { maxSteps: 3 }).commit();
Persisting agent stepsDirect link to Persisting agent steps
Workflows built with .agent() serialize to the same declarative entries that stored workflows use. Only retries and metadata round-trip through storage. Options that hold functions, such as onFinish or a function-valued toolChoice, throw an error when the workflow is stored.