mastra-processors-guardrails
Skill2 starsChanged 29 days agoApache-2.0View on GitHub
Add input, output, and error processors to Mastra agents: built-in guardrails for prompt injection, PII, moderation, system prompt leaks, regex filters, language detection, token and cost limits, response caching, and custom processors with processInput, processInputStep, processOutputStream, processOutputStep, processToolResult, and processAPIError. Use this skill whenever the user wants to block, redact, rewrite, moderate, validate, or limit what goes into or comes out of an agent, retry a bad response, cap spend, cache responses, or run guardrails in parallel. Triggers on inputProcessors, outputProcessors, errorProcessors, @mastra/core/processors, Processor interface, PromptInjectionDetector, PIIDetector, ModerationProcessor, SystemPromptScrubber, RegexFilterProcessor, TokenLimiter, TokenCostControl, ToolCallFilter, BatchPartsProcessor, ResponseCache, tripwire, onViolation, maxProcessorRetries. Also triggers for jailbreak defense, content safety, and redaction.
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-classifierSkill
- 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-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
4 rules. About 0 are common, 0 are distinctive, and 4 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈0
Distinctive · estimate
This author's own choices. Worth a look.
4
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 89
Every array also accepts a function of { requestContext }.
- Very specific: line 122
Verify the API: If node_modules/@mastra/ exists, read node_modules/@mastra/core/dist/docs/ and the installed processor types.
- Very specific: line 131
Set
maxProcessorRetries: if any processor uses abort(..., { retry: true }). - Very specific: line 172
Guardrail model outage lets content through: Default errorStrategy is 'warn' (fail-open).
≈ “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 Processors and Guardrails
Control what enters and leaves an agent with processors: built-in guardrails, custom processors, tripwires, and retries.
When to use
- Blocking prompt injection, jailbreaks, or system prompt overrides
- Redacting PII or secrets on input, output, or tool results
- Moderating harmful content in either direction
- Limiting tokens, trimming context, or capping monetary cost
- Validating a response and asking the model to retry
- Switching model or tool choice per step
- Recovering from provider API errors
- Caching identical LLM requests
- Running several guardrails in parallel to cut latency
When NOT to use
- Shaping one tool's output for the model or UI (use
mastra-tools:toModelOutput,transform) - Blocking a specific tool call before it runs (use
mastra-tools:hooks.beforeToolCall) - Human approval of tool calls (use
mastra-agent-approval-hitl) - Typed objects from the model (use
mastra-structured-output) - Labeling text with typed answers outside a pipeline (use
mastra-classifier) - Memory processors for history, semantic recall, and working memory (use
mastra-memory) - Large tool catalogs through
ToolSearchProcessor(usemastra-tools) - Scoring response quality offline or live (use
mastra-evals)
Required inputs
- The threat or rule: what must be blocked, redacted, rewritten, or limited
- Direction: input, output, tool results, or provider errors
- Strategy per rule:
block,warn,filter,redact,rewrite,detect, ortranslate - Fail mode if a guardrail model fails: fail-open (
warn) or fail-closed (strict) - Latency budget for LLM-based guardrails
- How the caller handles a blocked request (tripwire)
The framework: three arrays, one interface
import { Agent } from '@mastra/core/agent'
import {
UnicodeNormalizer,
PromptInjectionDetector,
PIIDetector,
ModerationProcessor,
} from '@mastra/core/processors'
const GUARDRAIL_MODEL = 'openai/gpt-6-luna'
export const secureAgent = new Agent({
id: 'secure-agent',
name: 'Secure Agent',
instructions: 'You are a helpful assistant',
model: 'openai/gpt-6.1-sol',
inputProcessors: [
new UnicodeNormalizer({ stripControlChars: true, collapseWhitespace: true }),
new PromptInjectionDetector({ model: GUARDRAIL_MODEL, threshold: 0.8, strategy: 'block' }),
new PIIDetector({ model: GUARDRAIL_MODEL, strategy: 'redact', redactionMethod: 'mask' }),
],
outputProcessors: [new ModerationProcessor({ model: GUARDRAIL_MODEL, strategy: 'block' })],
})| Array | Runs | Typical use |
inputProcessors | Before the LLM, and per step | Normalize, detect injection, redact, trim tokens, switch models |
outputProcessors | During and after the response | Moderate, scrub, redact, batch chunks, validate and retry |
errorProcessors | When the provider call throws | Repair the request and retry |
Order matters. Processors run in array order. With memory: [memory] -> [your inputProcessors] and [your outputProcessors] -> [memory]. An output guardrail that aborts skips memory, so the message is not saved.
Every array also accepts a function of { requestContext }. Passing an array to generate()/stream() replaces the agent's array for that call.
Processor methods
| Method | Runs | Use |
processInput | Once before the loop | Rewrite or reject user input |
processInputStep | Before every model step | Swap model, tools, toolChoice, send signals |
processLLMRequest | Right before each provider call | Transient prompt rewrites, cache lookup |
processOutputStream | Each streamed chunk | Filter or edit chunks. Return null to drop |
processLLMResponse | After each provider stream | Cache writes, side effects |
processOutputStep | After each model step | Validate, abort(reason, { retry: true }) |
processToolResult | Each tool result before it enters history | Redact or block tool output |
processOutputResult | Once at the end | Final transforms, usage tracking, metadata |
processAPIError | On provider API errors | Fix the request, return { retry: true } |
Blocking and tripwires
abort(reason, { retry?, metadata? }) throws a TripWire. Detect it at the call site:
const result = await agent.generate('Is this credit card number valid?: 4543 1374 5089 4332')
if (result.tripwire) {
console.error('Blocked:', result.tripwire.reason, result.tripwire.processorId)
}On streams, look for chunk.type === 'tripwire' with chunk.payload.reason and chunk.payload.processorId. generate() also sets finishReason: 'other'.
Workflow
- Verify the API. If
node_modules/@mastra/exists, readnode_modules/@mastra/core/dist/docs/and the installed processor types. Otherwise fetchhttps://mastra.ai/docs/agents/processors.md,https://mastra.ai/docs/agents/guardrails.md, and the processor reference page. Trust these over memory. - List the rules. For each, pick direction, built-in processor, and strategy (see
references/built-in-processors.md). - Prefer cheap checks first.
UnicodeNormalizerandRegexFilterProcessormake no LLM calls. Put them before LLM-based guardrails. - Use a small guardrail model. Guardrails do not need the primary model. Set
lastMessageOnly: trueon long threads. - Choose fail mode. Model-backed guardrails default to
errorStrategy: 'warn'(fail-open). Use'strict'when unchecked content must never pass. - Order the arrays. Normalize, then detect, then redact. For output, put
BatchPartsProcessorbefore heavier stream processors. - Parallelize independent checks with a processor workflow (see Deep dive).
- Handle tripwires in every caller: check
result.tripwireor thetripwirechunk and show a safe message. - Add
onViolationfor alerting and audit logs. - Set
maxProcessorRetriesif any processor usesabort(..., { retry: true }). Without it, step retries do not run. - Test with benign and adversarial inputs in Studio. Processor workflows appear there as workflows.
Deep dive: parallel guardrails
Processors run sequentially by default. Run independent checks in parallel with a workflow. Map to the redact branch so its rewritten messages carry forward.
import { createWorkflow, createStep } from '@mastra/core/workflows'
import {
ProcessorStepSchema,
PIIDetector,
PromptInjectionDetector,
ModerationProcessor,
} from '@mastra/core/processors'
export const inputGuardrails = createWorkflow({
id: 'input-guardrails',
inputSchema: ProcessorStepSchema,
outputSchema: ProcessorStepSchema,
})
.parallel([
createStep(new PIIDetector({ model: 'openai/gpt-6-luna', strategy: 'redact' })),
createStep(new PromptInjectionDetector({ model: 'openai/gpt-6-luna', strategy: 'block' })),
createStep(new ModerationProcessor({ model: 'openai/gpt-6-luna', strategy: 'block' })),
])
.map(async ({ inputData }) => inputData['processor:pii-detector'])
.commit()
// new Agent({ ..., inputProcessors: [inputGuardrails] })Branch results are keyed processor:<id>. If every branch only blocks, map to any branch.
Failure patterns
- Retry never happens.
abort(..., { retry: true })needsmaxProcessorRetrieson the agent or call. It is unset by default. - Guardrail model outage lets content through. Default
errorStrategyis'warn'(fail-open). Set'strict'for must-block rules. - Caller ignores tripwires. The response looks empty. Check
result.tripwireor thetripwirechunk. - Redaction lost after a parallel step.
.map()picked a block-only branch. Map to theredactbranch. - Slow streams. LLM guardrails run per chunk. Add
BatchPartsProcessorfirst, or setbufferSizeonModerationProcessor/SystemPromptScrubber. - One LLM call per old message. Set
lastMessageOnly: trueon detectors in long threads. - Reading
message.contentas a string in a custom processor. Text lives inmessage.content.parts. Filterpart.type === 'text'. processOutputStreamreturns nothing.undefineddrops the chunk. Returnpartto pass it through.- Custom
data-*chunks never reach the processor. SetprocessDataParts = true. - Per-call
errorProcessorsdrops defaults. A per-call list replaces the resolved list, defaults included. Pass a same-idinstance on the agent to customize one default instead. - Expecting
onViolationfor every block. It does not fire forprocessToolResultaborts or for processor workflows while streaming output. - Caching tools with side effects.
ResponseCachereplays tool calls without running them. Skip it for side-effecting agents. TokenCostControlovershoots. Metrics persist asynchronously. Treat the limit as approximate and setwarnAtPercent.
Output format
A finished processor delivery contains:
- Custom processors in
src/mastra/processors/<id>.ts, each with a stableid - Processor workflows (if any) in
src/mastra/processors/withProcessorStepSchema - The agent's
inputProcessors,outputProcessors, anderrorProcessorsin a deliberate order maxProcessorRetriesset when retries are used- Tripwire handling at each call site, and
onViolationwiring for alerts - Tests or Studio runs with benign and adversarial inputs, plus a latency check
Reference files
references/api.md-Processorinterface, method arguments and returns, abort and tripwire shapes, agent options.references/built-in-processors.md- Built-in guardrail and utility processors with their options.references/examples.md- Layered guardrails, custom input and output processors, quality retry, tool result redaction, cost cap, response cache.references/checklist.md- Guardrail production checklist.
Live docs
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.

