Workflow.classifier()
The .classifier() method adds a configured Classifier as a declarative workflow step. It returns a JSON-safe { answers, usage } object that can be consumed by later steps and existing control-flow methods such as .branch().
Usage exampleDirect link to Usage example
const workflow = createWorkflow({
id: 'ticket-triage',
inputSchema: z.object({ message: z.string() }),
outputSchema: z.any(),
})
.map({ message: { initData: true, path: 'message' } })
.classifier(router)
.branch([
[async ({ inputData }) => inputData.answers.route.choice === 'billing', billingStep],
[async ({ inputData }) => inputData.answers.route.choice === 'support', supportStep],
[async () => true, fallbackStep],
])
.commit()
When a classifier instance is passed, question keys and choice literals are inferred in downstream steps. The classifier must contain constructor-configured questions.
ParametersDirect link to Parameters
classifierOrId:
options?:
stepOptions?:
ReturnsDirect link to Returns
workflow:
Input mappingDirect link to Input mapping
The classifier evaluates the complete previous step output. Insert .map() before .classifier() to select or reshape its input.
workflow
.map({
message: { initData: true, path: 'message' },
locale: { initData: true, path: 'locale' },
})
.classifier(router)
Mappings use the same validated path grammar as other workflow steps and persist in serialized workflow graphs.
OutputDirect link to Output
- Choice answers expose
answers.<question>.choiceand may includeprobabilities. - Score answers expose
answers.<question>.scoreand may includeprobabilities. - Boolean answers expose the raw
P(true)value asanswers.<question>.probability. Apply an explicit threshold when routing. usagecontains normalized token usage.
Retries and errorsDirect link to Retries and errors
maxRetries controls retries of the classifier's model call. retries controls retries of the workflow step. The workflow abort signal is forwarded to the classifier. Classifier errors fail the step. .classifier() doesn't apply fail-open behavior.
Referencing a classifier by IDDirect link to Referencing a classifier by ID
Register the classifier on the same Mastra instance that runs the workflow:
export const mastra = new Mastra({
classifiers: { router },
workflows: { workflow },
})
For a typed string reference, supply the configured question type explicitly:
workflow.classifier<typeof router.questions>('router')