Processors
A file-based agent discovers processors from its processors/ directory. Use this page for the file-based convention; use the processors guide for built-in processors, custom processors, streaming behavior, and advanced patterns.
Files under processors/input/ run before messages reach the model. Files under processors/output/ run after the model responds. Each file default-exports one Processor, and the filename becomes its discovery key.
QuickstartDirect link to Quickstart
Add an input processor by placing a processor file under processors/input/:
import { PIIDetector } from '@mastra/core/processors'
export default new PIIDetector({
strategy: 'block',
})
This processor runs before user messages reach the model.
Add an output processorDirect link to Add an output processor
Output processors are good for filtering or transforming model responses before they're returned. This example redacts PII from the final response.
import { PIIDetector } from '@mastra/core/processors'
export default new PIIDetector({
strategy: 'redact',
})
OrderingDirect link to Ordering
Discovered processors are added after any processors defined in config.ts. That means config processors run first.
config.inputProcessors → processors/input/* → model
model → config.outputProcessors → processors/output/* → response
If config.inputProcessors or config.outputProcessors is a function, discovered processors of that type are ignored with a warning because function-valued processors can't be statically merged.
Precedence with configDirect link to Precedence with config
Use config.ts for processors that need to be created dynamically or shared across agents. Use file-based processors for static, per-agent processors that should be reviewed alongside the agent.
Precedence is:
- Function-valued
config.inputProcessorsorconfig.outputProcessors - Array-valued
config.inputProcessorsorconfig.outputProcessors - Discovered files under
processors/input/orprocessors/output/
Visit the Processor reference for the full interface.