mastra-classifier
Skill2 starsChanged 29 days agoApache-2.0View on GitHub
Make typed runtime decisions with the Mastra Classifier: send JSON state and fixed choice, score, or boolean questions to a decision model and get one typed answer per question, then route workflows, gate tool approval, guard agent input and output with ClassifierProcessor, route cheap versus capable models with ModelSelectionProcessor, or score outputs with createClassifierScorer. Use this skill whenever the user wants to classify, route, triage, label, gate on a threshold, detect unsafe content, or pick a branch from a small known set of outcomes without parsing model text. Triggers on Classifier, @mastra/core/classifier, decide(), decision model, decisionModel, typeSafeAi, @ai-sdk/typesafe-ai, choice question, score question, boolean question, probability, refusal, ClassifierProcessor, ModelSelectionProcessor, createClassifierScorer, workflow .classifier(), MastraDecisionModel, getClassifierById. Also triggers for intent detection, ticket triage, and topic scoping.
- Reads credentials
In this repository46 other files it gives its agents.
- mastra-a2a-acpSkill
- mastra-agent-approval-hitlSkill
- mastra-agentic-uiSkill
- mastra-agent-skillsSkill
- mastra-agentsSkill
- mastra-authSkill
- mastra-browserSkill
- mastra-build-playbookSkill
- mastra-channelsSkill
- mastra-cli-apiSkill
- mastra-client-sdkSkill
- mastra-datasets-experimentsSkill
- mastra-deploymentSkill
- mastra-evalsSkill
- mastra-file-based-agentsSkill
- mastra-framework-integrationSkill
- mastra-guideSkill
- mastra-harnessSkill
- mastra-knowledgeSkill
- mastra-mcpSkill
- mastra-memorySkill
- mastra-migrationsSkill
- mastra-modelsSkill
- mastra-multi-agentSkill
- mastra-observabilitySkill
- mastra-observational-memorySkill
- mastra-platformSkill
- mastra-processors-guardrailsSkill
- mastra-project-setupSkill
- mastra-ragSkill
- mastra-schedulesSkill
- mastra-semantic-recallSkill
- mastra-serverSkill
- mastra-signalsSkill
- mastra-storageSkill
- mastra-streamingSkill
- mastra-structured-outputSkill
- mastra-studio-editorSkill
- mastra-tool-integrationsSkill
- mastra-toolsSkill
- mastra-troubleshootingSkill
- mastra-vector-storesSkill
- mastra-voiceSkill
- mastra-workflowsSkill
- mastra-workflow-suspend-resumeSkill
- mastra-workspace-sandboxSkill
The file
We split this skill into the rules it gives an agent
6 rules. About 0 are common, 1 is distinctive, and 5 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
5
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 14
Ask a decision model fixed, typed questions about JSON state and act on the typed answers in code.
- Very specific: line 96
Every answer can instead be { type: 'refusal' } with no choice, score, or probability.
- Very specific: line 115
Verify the API: If node_modules/@mastra/ exists, read node_modules/@mastra/core/dist/docs/ and the installed Classifier types.
- Very specific: line 169
Every .branch() condition that is true runs.
- Very specific: line 180
Closed list with no escape: Every input is forced into a listed team.
- Very specific: line 183
Unconditional fallback branch: after .classifier(). It runs alongside the match.
≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Mastra Classifier
Ask a decision model fixed, typed questions about JSON state and act on the typed answers in code.
When to use
- Routing a request to a team, handler, or workflow branch
- Gating an action on a threshold (unsafe content, needs human review)
- Scoring one dimension of an output (tone, quality)
- Scoping an agent to allowed topics
- Downgrading easy requests to a cheaper model
- Turning a classifier answer into an eval score
The decision has a small, known set of outcomes. The model supplies the judgment; your code supplies the policy.
When NOT to use
- Extracting a data object with many interdependent fields (use
mastra-structured-output) - LLM-based moderation, PII, or injection detection with built-in prompts (use
mastra-processors-guardrails) - Measuring output quality over time with datasets (use
mastra-evalsandmastra-datasets-experiments) - Workflow branching mechanics beyond the classifier step (use
mastra-workflows) - Picking a language model or provider string (use
mastra-models) - The approval flow that a classifier gates (use
mastra-agent-approval-hitl)
Required inputs
- The decision: what outcome set, and what your code does with each outcome
- Question types:
choice(pick a key),score(position on an ordered list),boolean(probability of true) - The state fields the decision needs, and which fields are sensitive
- A decision model provider and its API key (for example
TYPESAFE_AI_API_KEY) - Thresholds and the policy for a refused question
The framework: state plus questions in, typed answers out
import { Classifier } from '@mastra/core/classifier'
import { typeSafeAi } from '@ai-sdk/typesafe-ai'
const classifier = new Classifier({
id: 'request-classifier',
model: typeSafeAi.decisionModel('jev-latest'),
questions: {
route: {
type: 'choice',
instructions: 'Choose the team that should handle this request.',
criteria: {
billing: 'Questions about invoices, charges, or refunds',
support: 'Questions about using or troubleshooting the product',
},
},
urgent: {
type: 'boolean',
instructions: 'Does this request require an immediate response?',
},
},
})
const result = await classifier.decide({
state: {
subject: 'Duplicate charge',
message: 'I was charged twice for the same subscription.',
},
})
result.answers.route.choice // 'billing' | 'support' | undefined
result.answers.urgent.probability // P(true), 0 to 1, or undefinedAdded in @mastra/core@1.69.0. The model is a decision model from a provider's decisionModel() factory. It is not a language model and not a 'provider/model' string. Install @ai-sdk/typesafe-ai and set TYPESAFE_AI_API_KEY for this example. OpenAI, Anthropic, and Google AI SDK providers also expose decisionModel().
Question types
| Type | criteria | Answer field | Notes |
choice | Record of key to description | choice, optional probabilities per key | Choice keys are inferred as a union type |
score | Ordered array, at least 2 levels | score (index range, may be fractional), optional probabilities | 3 levels give scores 0 to 2 |
boolean | Optional { true?, false? } | probability of true | Not "confidence in the likelier outcome" |
Every answer can instead be { type: 'refusal' } with no choice, score, or probability. Check type first.
One decide() call sends the state once with all questions, so extra questions add no round trip.
Where classifiers run
| Place | API |
| Your code | classifier.decide({ state }) |
| Workflow routing | .classifier(classifier) then .branch() |
| Agent guardrail | new ClassifierProcessor({ classifier, onResult }) |
| Model routing | new ModelSelectionProcessor({ model, choices }) |
| Tool approval gate | requireToolApproval: async ({ toolName, args }) => ... |
| Evals | createClassifierScorer({ classifier, question, ... }) |
Workflow
- Verify the API. If
node_modules/@mastra/exists, readnode_modules/@mastra/core/dist/docs/and the installedClassifiertypes. Otherwise fetchhttps://mastra.ai/docs/classifier.mdandhttps://mastra.ai/reference/classifier/classifier.md. Trust these over memory. - Install a decision model provider and set its key. Confirm the model supports every question type you use.
- Write one judgment per question. Split multi-factor questions and combine the answers in code.
- Make criteria exclusive and add an
otherkey or level when the set is open. - Put facts in
state, notinstructions. Send only what the decision needs. Redact sensitive fields. - Keep
questionsas an object literal in the constructor so answer keys and choice unions are inferred. Processors, workflow steps, and scorers require constructor questions. - Handle refusals and thresholds explicitly in code.
- Register shared classifiers on
new Mastra({ classifiers })and reference them by ID where useful. - Wire it in: workflow
.classifier(),ClassifierProcessor,ModelSelectionProcessor, or a scorer. - Test each question by changing one state field and confirming one answer moves. Review
CLASSIFIER_EVALUATIONspans in Studio.
Deep dive: guardrails and routing
ClassifierProcessor runs the classifier over message text. onResult owns the policy: abort(reason) stops with a tripwire, filter() drops the message or chunk, doing nothing lets it pass.
import { Agent } from '@mastra/core/agent'
import { ClassifierProcessor } from '@mastra/core/processors'
export const agent = new Agent({
id: 'support-agent',
name: 'support-agent',
instructions: 'You help customers with billing and account questions.',
model: 'openai/gpt-6.1-sol',
inputProcessors: [
new ClassifierProcessor({
classifier: topic, // a Classifier with a 'topic' choice question
lastMessageOnly: true,
onResult: (answers, { abort }) => {
if (answers.topic.type === 'refusal' || answers.topic.choice === 'other') {
abort('This assistant only handles billing and account questions')
}
},
}),
],
})Workflow routing:
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 ({ inputData }) => inputData.answers.route.choice === 'other', fallbackStep],
])
.commit()Every .branch() condition that is true runs. Keep conditions mutually exclusive and do not end with async () => true unless the fallback should also run.
Failure patterns
- Passing
'openai/gpt-6.1-sol'asmodel. The classifier needs a decision model, for exampletypeSafeAi.decisionModel('jev-latest'). - Reading
probabilityon a refusal. It isundefined. Checkanswer.type === 'refusal'and apply a fallback (often fail closed with?? 1). - Treating boolean
probabilityas confidence. It is P(true).0.1means likely false. - Multi-factor questions. "Is this urgent and from a VIP?" moves on either factor. Ask two questions and combine in code.
- Overlapping criteria ("good fit" vs "maybe a fit"). Probability leaks between them. Make them exclusive.
- Closed list with no escape. Every input is forced into a listed team. Add
other. - Questions in
decide()when the constructor has them. They are mutually exclusive. Constructor questions are required for processors, workflow steps, and scorers. - Lost types with a string reference. Pass the question type:
.classifier<typeof router.questions>('router')ornew ClassifierProcessor<SafetyQuestions>({ classifier: 'safety', ... }). - Unconditional fallback branch after
.classifier(). It runs alongside the match. Add an explicitotherorrefusalcondition. - Expecting partial answers on failure.
decide()rejects on provider failure or invalid output. Only retryableAPICallErrors retry (maxRetriesdefault 2). - Two
ClassifierProcessors with the sameid. They share per-run state. Give each a uniqueid. - Slow
onDecisionin streams. It runs per chunk and delays output. Queue the write. - Sensitive data in
state. It goes to the provider and into trace spans. Redact, or sethideInput/hideOutputin tracing options. - Calling
evaluate(). Deprecated alias. Usedecide().
Output format
A finished classifier delivery contains:
- The classifier in
src/mastra/classifiers/<id>.tswith constructor questions as an object literal - Registration in
new Mastra({ classifiers })when shared or referenced by ID - The decision provider package installed and its env var in
.env.example - Code that handles every choice, the threshold, and refusals
- Wiring into the workflow, processor, model selection, approval gate, or scorer
- Tests that change one state field per question and check the answer moves
Reference files
references/api.md- Constructor, question shapes,decide()options, results, validation,MastraDecisionModel, registration, processor and workflow options.references/examples.md- Ticket triage workflow, safety guardrail with audit, output quality gate, model routing, classifier scorer.references/checklist.md- Classifier design and production checklist.
Live docs
- https://mastra.ai/docs/classifier
- https://mastra.ai/reference/classifier/classifier
- https://mastra.ai/reference/processors/classifier-processor
- https://mastra.ai/reference/processors/model-selection-processor
- https://mastra.ai/reference/workflows/workflow-methods/classifier
- https://mastra.ai/reference/evals/create-classifier-scorer
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

