@mastra/core 1.15.0-alpha.0 → 1.15.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (161) hide show
  1. package/CHANGELOG.md +33 -0
  2. package/dist/agent/index.cjs +8 -8
  3. package/dist/agent/index.js +1 -1
  4. package/dist/{chunk-JBYJDZT5.js → chunk-4YKZNIK6.js} +4 -4
  5. package/dist/{chunk-JBYJDZT5.js.map → chunk-4YKZNIK6.js.map} +1 -1
  6. package/dist/{chunk-I5ON7TPA.cjs → chunk-7BM5LQHF.cjs} +82 -82
  7. package/dist/{chunk-I5ON7TPA.cjs.map → chunk-7BM5LQHF.cjs.map} +1 -1
  8. package/dist/{chunk-IST54Q37.js → chunk-ASVFCNLS.js} +7 -7
  9. package/dist/{chunk-IST54Q37.js.map → chunk-ASVFCNLS.js.map} +1 -1
  10. package/dist/{chunk-DGSXFZGZ.js → chunk-CTJLKJMO.js} +10 -6
  11. package/dist/chunk-CTJLKJMO.js.map +1 -0
  12. package/dist/{chunk-4JBWS3Y6.js → chunk-EAWVRIHS.js} +3 -3
  13. package/dist/{chunk-4JBWS3Y6.js.map → chunk-EAWVRIHS.js.map} +1 -1
  14. package/dist/{chunk-7GXY5CDK.js → chunk-ECAVJWAH.js} +4 -4
  15. package/dist/{chunk-7GXY5CDK.js.map → chunk-ECAVJWAH.js.map} +1 -1
  16. package/dist/{chunk-EG3QZTQQ.cjs → chunk-EFYYAFWD.cjs} +19 -15
  17. package/dist/chunk-EFYYAFWD.cjs.map +1 -0
  18. package/dist/{chunk-ORYC6WMY.js → chunk-EYLPPZIF.js} +4 -2
  19. package/dist/chunk-EYLPPZIF.js.map +1 -0
  20. package/dist/{chunk-FIOAZZAM.js → chunk-FNOAF2SZ.js} +3 -3
  21. package/dist/{chunk-FIOAZZAM.js.map → chunk-FNOAF2SZ.js.map} +1 -1
  22. package/dist/{chunk-GMUEV4ML.cjs → chunk-FYQXIWRH.cjs} +4 -2
  23. package/dist/chunk-FYQXIWRH.cjs.map +1 -0
  24. package/dist/{chunk-KZLBDSIF.js → chunk-HIZDAENF.js} +3 -3
  25. package/dist/{chunk-KZLBDSIF.js.map → chunk-HIZDAENF.js.map} +1 -1
  26. package/dist/{chunk-7XPMIQIK.js → chunk-JIBMK2QP.js} +8 -8
  27. package/dist/{chunk-7XPMIQIK.js.map → chunk-JIBMK2QP.js.map} +1 -1
  28. package/dist/{chunk-CBLM3UY3.js → chunk-KCZ3R5SF.js} +3 -3
  29. package/dist/{chunk-CBLM3UY3.js.map → chunk-KCZ3R5SF.js.map} +1 -1
  30. package/dist/{chunk-Y2I3C7FR.cjs → chunk-M5BDH7B4.cjs} +6 -6
  31. package/dist/{chunk-Y2I3C7FR.cjs.map → chunk-M5BDH7B4.cjs.map} +1 -1
  32. package/dist/{chunk-4CC2ZV3B.js → chunk-MHTWFVXK.js} +3 -3
  33. package/dist/{chunk-4CC2ZV3B.js.map → chunk-MHTWFVXK.js.map} +1 -1
  34. package/dist/{chunk-REVBDBHI.cjs → chunk-NWPRZZ2K.cjs} +48 -48
  35. package/dist/{chunk-REVBDBHI.cjs.map → chunk-NWPRZZ2K.cjs.map} +1 -1
  36. package/dist/{chunk-PEKFBFE2.cjs → chunk-ORHPD25N.cjs} +7 -7
  37. package/dist/{chunk-PEKFBFE2.cjs.map → chunk-ORHPD25N.cjs.map} +1 -1
  38. package/dist/{chunk-SV6VG3XO.cjs → chunk-OX63O3QG.cjs} +5 -5
  39. package/dist/{chunk-SV6VG3XO.cjs.map → chunk-OX63O3QG.cjs.map} +1 -1
  40. package/dist/{chunk-NSJS72DA.cjs → chunk-Q64Z437G.cjs} +15 -15
  41. package/dist/{chunk-NSJS72DA.cjs.map → chunk-Q64Z437G.cjs.map} +1 -1
  42. package/dist/{chunk-W4R4TA4Z.cjs → chunk-RFZB2PQE.cjs} +9 -9
  43. package/dist/{chunk-W4R4TA4Z.cjs.map → chunk-RFZB2PQE.cjs.map} +1 -1
  44. package/dist/{chunk-J7UJLVIQ.cjs → chunk-TJB7IK7N.cjs} +19 -11
  45. package/dist/chunk-TJB7IK7N.cjs.map +1 -0
  46. package/dist/{chunk-OT7UVM2Z.cjs → chunk-X4RVX77L.cjs} +185 -185
  47. package/dist/{chunk-OT7UVM2Z.cjs.map → chunk-X4RVX77L.cjs.map} +1 -1
  48. package/dist/{chunk-XVOLOB5X.cjs → chunk-YV5UMIRV.cjs} +3 -3
  49. package/dist/{chunk-XVOLOB5X.cjs.map → chunk-YV5UMIRV.cjs.map} +1 -1
  50. package/dist/{chunk-SCTBRRU3.js → chunk-Z76WT6W3.js} +18 -10
  51. package/dist/chunk-Z76WT6W3.js.map +1 -0
  52. package/dist/datasets/index.cjs +11 -11
  53. package/dist/datasets/index.js +1 -1
  54. package/dist/docs/SKILL.md +10 -11
  55. package/dist/docs/assets/SOURCE_MAP.json +154 -154
  56. package/dist/docs/references/docs-agents-agent-approval.md +114 -193
  57. package/dist/docs/references/docs-agents-guardrails.md +120 -167
  58. package/dist/docs/references/docs-agents-networks.md +88 -205
  59. package/dist/docs/references/docs-agents-overview.md +47 -256
  60. package/dist/docs/references/docs-agents-processors.md +201 -297
  61. package/dist/docs/references/docs-agents-structured-output.md +13 -22
  62. package/dist/docs/references/docs-agents-supervisor-agents.md +24 -18
  63. package/dist/docs/references/docs-agents-using-tools.md +81 -104
  64. package/dist/docs/references/docs-memory-observational-memory.md +4 -2
  65. package/dist/docs/references/docs-memory-overview.md +219 -24
  66. package/dist/docs/references/docs-memory-semantic-recall.md +1 -1
  67. package/dist/docs/references/docs-memory-storage.md +4 -4
  68. package/dist/docs/references/docs-memory-working-memory.md +1 -1
  69. package/dist/docs/references/docs-observability-overview.md +1 -1
  70. package/dist/docs/references/docs-observability-tracing-exporters-arize.md +1 -1
  71. package/dist/docs/references/docs-server-request-context.md +1 -1
  72. package/dist/docs/references/docs-workflows-overview.md +1 -1
  73. package/dist/docs/references/docs-workspace-overview.md +1 -1
  74. package/dist/docs/references/guides-concepts-multi-agent-systems.md +75 -0
  75. package/dist/docs/references/reference-agents-agent.md +6 -8
  76. package/dist/docs/references/reference-agents-generate.md +74 -23
  77. package/dist/docs/references/reference-agents-getMemory.md +1 -1
  78. package/dist/docs/references/reference-agents-network.md +2 -2
  79. package/dist/docs/references/reference-ai-sdk-network-route.md +1 -1
  80. package/dist/docs/references/reference-ai-sdk-with-mastra.md +1 -1
  81. package/dist/docs/references/reference-core-getMemory.md +1 -2
  82. package/dist/docs/references/reference-core-listMemory.md +1 -2
  83. package/dist/docs/references/reference-harness-harness-class.md +2 -2
  84. package/dist/docs/references/reference-memory-observational-memory.md +3 -1
  85. package/dist/docs/references/reference-processors-processor-interface.md +2 -0
  86. package/dist/docs/references/reference-storage-overview.md +1 -1
  87. package/dist/docs/references/reference-templates-overview.md +1 -1
  88. package/dist/docs/references/reference-tools-create-tool.md +16 -4
  89. package/dist/evals/index.cjs +5 -5
  90. package/dist/evals/index.js +2 -2
  91. package/dist/evals/scoreTraces/index.cjs +3 -3
  92. package/dist/evals/scoreTraces/index.js +1 -1
  93. package/dist/harness/harness.d.ts +3 -0
  94. package/dist/harness/harness.d.ts.map +1 -1
  95. package/dist/harness/index.cjs +48 -13
  96. package/dist/harness/index.cjs.map +1 -1
  97. package/dist/harness/index.js +46 -11
  98. package/dist/harness/index.js.map +1 -1
  99. package/dist/harness/types.d.ts +11 -0
  100. package/dist/harness/types.d.ts.map +1 -1
  101. package/dist/index.cjs +2 -2
  102. package/dist/index.js +1 -1
  103. package/dist/llm/index.cjs +16 -16
  104. package/dist/llm/index.js +5 -5
  105. package/dist/llm/model/model.d.ts.map +1 -1
  106. package/dist/llm/model/provider-types.generated.d.ts +6 -2
  107. package/dist/loop/index.cjs +14 -14
  108. package/dist/loop/index.js +1 -1
  109. package/dist/mastra/index.cjs +2 -2
  110. package/dist/mastra/index.js +1 -1
  111. package/dist/memory/index.cjs +14 -14
  112. package/dist/memory/index.js +1 -1
  113. package/dist/memory/types.d.ts +7 -0
  114. package/dist/memory/types.d.ts.map +1 -1
  115. package/dist/models-dev-E6FRPGHV.js +3 -0
  116. package/dist/{models-dev-5BT32JYX.js.map → models-dev-E6FRPGHV.js.map} +1 -1
  117. package/dist/models-dev-WIROJ2IM.cjs +12 -0
  118. package/dist/{models-dev-OTMJW4WK.cjs.map → models-dev-WIROJ2IM.cjs.map} +1 -1
  119. package/dist/netlify-7IRBQ2BY.cjs +12 -0
  120. package/dist/{netlify-DT2P2NQD.cjs.map → netlify-7IRBQ2BY.cjs.map} +1 -1
  121. package/dist/netlify-OAGRP6WY.js +3 -0
  122. package/dist/{netlify-FJCQU3OY.js.map → netlify-OAGRP6WY.js.map} +1 -1
  123. package/dist/processor-provider/index.cjs +10 -10
  124. package/dist/processor-provider/index.js +1 -1
  125. package/dist/processors/index.cjs +42 -42
  126. package/dist/processors/index.js +1 -1
  127. package/dist/provider-registry-2MHU2NP6.js +3 -0
  128. package/dist/{provider-registry-BCSAL2IQ.js.map → provider-registry-2MHU2NP6.js.map} +1 -1
  129. package/dist/provider-registry-FINEGQHE.cjs +40 -0
  130. package/dist/{provider-registry-65WCBR2K.cjs.map → provider-registry-FINEGQHE.cjs.map} +1 -1
  131. package/dist/provider-registry.json +14 -6
  132. package/dist/relevance/index.cjs +3 -3
  133. package/dist/relevance/index.js +1 -1
  134. package/dist/stream/index.cjs +8 -8
  135. package/dist/stream/index.js +1 -1
  136. package/dist/test-utils/llm-mock.cjs +4 -4
  137. package/dist/test-utils/llm-mock.js +1 -1
  138. package/dist/tool-loop-agent/index.cjs +4 -4
  139. package/dist/tool-loop-agent/index.js +1 -1
  140. package/dist/workflows/default.d.ts +2 -2
  141. package/dist/workflows/default.d.ts.map +1 -1
  142. package/dist/workflows/evented/index.cjs +10 -10
  143. package/dist/workflows/evented/index.js +1 -1
  144. package/dist/workflows/index.cjs +24 -24
  145. package/dist/workflows/index.js +1 -1
  146. package/package.json +7 -7
  147. package/src/llm/model/provider-types.generated.d.ts +6 -2
  148. package/dist/chunk-DGSXFZGZ.js.map +0 -1
  149. package/dist/chunk-EG3QZTQQ.cjs.map +0 -1
  150. package/dist/chunk-GMUEV4ML.cjs.map +0 -1
  151. package/dist/chunk-J7UJLVIQ.cjs.map +0 -1
  152. package/dist/chunk-ORYC6WMY.js.map +0 -1
  153. package/dist/chunk-SCTBRRU3.js.map +0 -1
  154. package/dist/docs/references/docs-agents-agent-memory.md +0 -209
  155. package/dist/docs/references/docs-agents-network-approval.md +0 -278
  156. package/dist/models-dev-5BT32JYX.js +0 -3
  157. package/dist/models-dev-OTMJW4WK.cjs +0 -12
  158. package/dist/netlify-DT2P2NQD.cjs +0 -12
  159. package/dist/netlify-FJCQU3OY.js +0 -3
  160. package/dist/provider-registry-65WCBR2K.cjs +0 -40
  161. package/dist/provider-registry-BCSAL2IQ.js +0 -3
@@ -1,50 +1,16 @@
1
1
  # Guardrails
2
2
 
3
- Agents use processors to apply guardrails to inputs and outputs. They run before or after each interaction, giving you a way to review, transform, or block information as it passes between the user and the agent.
3
+ Mastra provides built-in processors that add security and safety controls to your agent. These processors detect, transform, or block harmful content before it reaches the language model or the user.
4
4
 
5
- Processors can be configured as:
6
-
7
- - **`inputProcessors`**: Applied before messages reach the language model.
8
- - **`outputProcessors`**: Applied to responses before they're returned to users.
9
-
10
- Some processors are _hybrid_, meaning they can be used with either `inputProcessors` or `outputProcessors`, depending on where the logic should be applied.
11
-
12
- ## When to use processors
13
-
14
- Use processors for content moderation, prompt injection prevention, response sanitization, message transformation, and other security-related controls. Mastra provides several built-in input and output processors for common use cases.
15
-
16
- ## Adding processors to an agent
17
-
18
- Import and instantiate the relevant processor class, and pass it to your agent’s configuration using either the `inputProcessors` or `outputProcessors` option:
19
-
20
- ```typescript
21
- import { Agent } from '@mastra/core/agent'
22
- import { ModerationProcessor } from '@mastra/core/processors'
23
-
24
- export const moderatedAgent = new Agent({
25
- id: 'moderated-agent',
26
- name: 'Moderated Agent',
27
- instructions: 'You are a helpful assistant',
28
- model: 'openai/gpt-5.4',
29
- inputProcessors: [
30
- new ModerationProcessor({
31
- model: 'openrouter/openai/gpt-oss-safeguard-20b',
32
- categories: ['hate', 'harassment', 'violence'],
33
- threshold: 0.7,
34
- strategy: 'block',
35
- instructions: 'Detect and flag inappropriate content in user messages',
36
- }),
37
- ],
38
- })
39
- ```
5
+ For an introduction to how processors work, how to add them to an agent, and how to create custom processors, see [Processors](https://mastra.ai/docs/agents/processors).
40
6
 
41
7
  ## Input processors
42
8
 
43
- Input processors are applied before user messages reach the language model. They're useful for normalization, validation, content moderation, prompt injection detection, and security checks.
9
+ Input processors run before user messages reach the language model. They handle normalization, validation, prompt injection detection, and security checks.
44
10
 
45
- ### Normalizing user messages
11
+ ### Normalize user messages
46
12
 
47
- The `UnicodeNormalizer` is an input processor that cleans and normalizes user input by unifying Unicode characters, standardizing whitespace, and removing problematic symbols, allowing the LLM to better understand user messages.
13
+ The `UnicodeNormalizer()` cleans and normalizes user input by unifying Unicode characters, standardizing whitespace, and removing problematic symbols.
48
14
 
49
15
  ```typescript
50
16
  import { UnicodeNormalizer } from '@mastra/core/processors'
@@ -61,11 +27,11 @@ export const normalizedAgent = new Agent({
61
27
  })
62
28
  ```
63
29
 
64
- > **Info:** Visit [UnicodeNormalizer](https://mastra.ai/reference/processors/unicode-normalizer) for a full list of configuration options.
30
+ > **Note:** Visit [`UnicodeNormalizer()`](https://mastra.ai/reference/processors/unicode-normalizer) reference for a full list of configuration options.
65
31
 
66
- ### Preventing prompt injection
32
+ ### Prevent prompt injection
67
33
 
68
- The `PromptInjectionDetector` is an input processor that scans user messages for prompt injection, jailbreak attempts, and system override patterns. It uses an LLM to classify risky input and can block or rewrite it before it reaches the model.
34
+ The `PromptInjectionDetector()` scans user messages for prompt injection, jailbreak attempts, and system override patterns. It uses an LLM to classify risky input and can block or rewrite it before it reaches the model.
69
35
 
70
36
  ```typescript
71
37
  import { PromptInjectionDetector } from '@mastra/core/processors'
@@ -84,11 +50,11 @@ export const secureAgent = new Agent({
84
50
  })
85
51
  ```
86
52
 
87
- > **Info:** Visit [PromptInjectionDetector](https://mastra.ai/reference/processors/prompt-injection-detector) for a full list of configuration options.
53
+ > **Note:** Visit [`PromptInjectionDetector()`](https://mastra.ai/reference/processors/prompt-injection-detector) reference for a full list of configuration options.
88
54
 
89
- ### Detecting and translating language
55
+ ### Detect and translate language
90
56
 
91
- The `LanguageDetector` is an input processor that detects and translates user messages into a target language, enabling multilingual support while maintaining consistent interaction. It uses an LLM to identify the language and perform the translation.
57
+ The `LanguageDetector()` detects and translates user messages into a target language, enabling multilingual support. It uses an LLM to identify the language and perform the translation.
92
58
 
93
59
  ```typescript
94
60
  import { LanguageDetector } from '@mastra/core/processors'
@@ -107,15 +73,15 @@ export const multilingualAgent = new Agent({
107
73
  })
108
74
  ```
109
75
 
110
- > **Info:** Visit [LanguageDetector](https://mastra.ai/reference/processors/language-detector) for a full list of configuration options.
76
+ > **Note:** Visit [`LanguageDetector()`](https://mastra.ai/reference/processors/language-detector) reference for a full list of configuration options.
111
77
 
112
78
  ## Output processors
113
79
 
114
- Output processors are applied after the language model generates a response, but before it's returned to the user. They're useful for response optimization, moderation, transformation, and applying safety controls.
80
+ Output processors run after the language model generates a response, but before it reaches the user. They handle response optimization, moderation, transformation, and safety controls.
115
81
 
116
- ### Batching streamed output
82
+ ### Batch streamed output
117
83
 
118
- The `BatchPartsProcessor` is an output processor that combines multiple stream parts before emitting them to the client. This reduces network overhead and improves the user experience by consolidating small chunks into larger batches.
84
+ The `BatchPartsProcessor()` combines multiple stream parts before emitting them to the client. This reduces network overhead by consolidating small chunks into larger batches.
119
85
 
120
86
  ```typescript
121
87
  import { BatchPartsProcessor } from '@mastra/core/processors'
@@ -133,33 +99,11 @@ export const batchedAgent = new Agent({
133
99
  })
134
100
  ```
135
101
 
136
- > **Info:** Visit [BatchPartsProcessor](https://mastra.ai/reference/processors/batch-parts-processor) for a full list of configuration options.
137
-
138
- ### Limiting token usage
139
-
140
- The `TokenLimiterProcessor` is an output processor that limits the number of tokens in model responses. It helps manage cost and performance by truncating or blocking messages when the limit is exceeded.
141
-
142
- ```typescript
143
- import { TokenLimiterProcessor } from '@mastra/core/processors'
144
-
145
- export const limitedAgent = new Agent({
146
- id: 'limited-agent',
147
- name: 'Limited Agent',
148
- outputProcessors: [
149
- new TokenLimiterProcessor({
150
- limit: 1000,
151
- strategy: 'truncate',
152
- countMode: 'cumulative',
153
- }),
154
- ],
155
- })
156
- ```
157
-
158
- > **Info:** Visit [TokenLimiterProcessor](https://mastra.ai/reference/processors/token-limiter-processor) for a full list of configuration options.
102
+ > **Note:** Visit [`BatchPartsProcessor()`](https://mastra.ai/reference/processors/batch-parts-processor) reference for a full list of configuration options.
159
103
 
160
- ### Scrubbing system prompts
104
+ ### Scrub system prompts
161
105
 
162
- The `SystemPromptScrubber` is an output processor that detects and redacts system prompts or other internal instructions from model responses. It helps prevent unintended disclosure of prompt content or configuration details that could introduce security risks. It uses an LLM to identify and redact sensitive content based on configured detection types.
106
+ The `SystemPromptScrubber()` detects and redacts system prompts or internal instructions from model responses. It prevents unintended disclosure of prompt content or configuration details. It uses an LLM to identify and redact sensitive content based on configured detection types.
163
107
 
164
108
  ```typescript
165
109
  import { SystemPromptScrubber } from '@mastra/core/processors'
@@ -182,17 +126,17 @@ const scrubbedAgent = new Agent({
182
126
  })
183
127
  ```
184
128
 
185
- > **Info:** Visit [SystemPromptScrubber](https://mastra.ai/reference/processors/system-prompt-scrubber) for a full list of configuration options.
129
+ > **Note:** Visit [`SystemPromptScrubber()`](https://mastra.ai/reference/processors/system-prompt-scrubber) reference for a full list of configuration options.
186
130
 
187
131
  > **Note:** When streaming responses over HTTP, Mastra redacts sensitive request data (system prompts, tool definitions, API keys) from stream chunks at the server level by default. See [Stream data redaction](https://mastra.ai/docs/server/mastra-server) for details.
188
132
 
189
133
  ## Hybrid processors
190
134
 
191
- Hybrid processors can be applied either before messages are sent to the language model or before responses are returned to the user. They're useful for tasks like content moderation and PII redaction.
135
+ Hybrid processors can run on either input or output. Place them in `inputProcessors`, `outputProcessors`, or both.
192
136
 
193
- ### Moderating input and output
137
+ ### Moderate input and output
194
138
 
195
- The `ModerationProcessor` is a hybrid processor that detects inappropriate or harmful content across categories like hate, harassment, and violence. It can be used to moderate either user input or model output, depending on where it's applied. It uses an LLM to classify the message and can block or rewrite it based on your configuration.
139
+ The `ModerationProcessor()` detects inappropriate or harmful content across categories like hate, harassment, and violence. It uses an LLM to classify the message and can block or rewrite it based on your configuration.
196
140
 
197
141
  ```typescript
198
142
  import { ModerationProcessor } from '@mastra/core/processors'
@@ -212,11 +156,11 @@ export const moderatedAgent = new Agent({
212
156
  })
213
157
  ```
214
158
 
215
- > **Info:** Visit [ModerationProcessor](https://mastra.ai/reference/processors/moderation-processor) for a full list of configuration options.
159
+ > **Note:** Visit [`ModerationProcessor()`](https://mastra.ai/reference/processors/moderation-processor) reference for a full list of configuration options.
216
160
 
217
- ### Detecting and redacting PII
161
+ ### Detect and redact PII
218
162
 
219
- The `PIIDetector` is a hybrid processor that detects and removes personally identifiable information such as emails, phone numbers, and credit cards. It can redact either user input or model output, depending on where it's applied. It uses an LLM to identify sensitive content based on configured detection types.
163
+ The `PIIDetector()` detects and removes personally identifiable information such as emails, phone numbers, and credit cards. It uses an LLM to identify sensitive content based on configured detection types.
220
164
 
221
165
  ```typescript
222
166
  import { PIIDetector } from '@mastra/core/processors'
@@ -238,67 +182,32 @@ export const privateAgent = new Agent({
238
182
  })
239
183
  ```
240
184
 
241
- > **Info:** Visit [PIIDetector](https://mastra.ai/reference/processors/pii-detector) for a full list of configuration options.
242
-
243
- ## Applying multiple processors
244
-
245
- You can apply multiple processors by listing them in the `inputProcessors` or `outputProcessors` array. They run in sequence, with each processor receiving the output of the one before it.
246
-
247
- A typical order might be:
248
-
249
- 1. **Normalization**: Standardize input format (`UnicodeNormalizer`).
250
- 2. **Security checks**: Detect threats or sensitive content (`PromptInjectionDetector`, `PIIDetector`).
251
- 3. **Filtering**: Block or transform messages (`ModerationProcessor`).
252
-
253
- The order affects behavior, so arrange processors to suit your goals.
254
-
255
- ```typescript
256
- import {
257
- UnicodeNormalizer,
258
- ModerationProcessor,
259
- PromptInjectionDetector,
260
- PIIDetector,
261
- } from '@mastra/core/processors'
262
-
263
- export const testAgent = new Agent({
264
- id: 'test-agent',
265
- name: 'Test Agent',
266
- inputProcessors: [
267
- new UnicodeNormalizer(),
268
- new PromptInjectionDetector(),
269
- new PIIDetector(),
270
- new ModerationProcessor(),
271
- ],
272
- })
273
- ```
185
+ > **Note:** Visit [`PIIDetector()`](https://mastra.ai/reference/processors/pii-detector) reference for a full list of configuration options.
274
186
 
275
187
  ## Processor strategies
276
188
 
277
- Many of the built-in processors support a `strategy` parameter that controls how they handle flagged input or output. Supported values may include: `block`, `warn`, `detect`, or `redact`.
189
+ Many built-in processors support a `strategy` parameter that controls how they handle flagged content. Supported values include: `block`, `warn`, `detect`, `redact`, `rewrite`, and `translate`.
278
190
 
279
- Most strategies allow the request to continue without interruption. When `block` is used, the processor calls its internal `abort()` function, which immediately stops the request and prevents any subsequent processors from running.
191
+ Most strategies allow the request to continue. When `block` is used, the processor calls `abort()`, which stops the request immediately and prevents subsequent processors from running.
280
192
 
281
193
  ```typescript
282
- import { PIIDetector } from '@mastra/core/processors'
283
-
284
- export const privateAgent = new Agent({
285
- id: 'private-agent',
286
- name: 'Private Agent',
287
- inputProcessors: [
288
- new PIIDetector({
289
- strategy: 'block',
290
- }),
291
- ],
292
- })
194
+ inputProcessors: [
195
+ new PIIDetector({
196
+ model: 'openrouter/openai/gpt-oss-safeguard-20b',
197
+ threshold: 0.6,
198
+ strategy: 'block',
199
+ detectionTypes: ['email', 'phone', 'credit-card'],
200
+ }),
201
+ ]
293
202
  ```
294
203
 
295
- ### Handling blocked requests
204
+ ## Handle blocked requests
296
205
 
297
- When a processor blocks a request, the agent will still return successfully without throwing an error. To handle blocked requests, check for `tripwire` in the response.
206
+ When a processor calls `abort()`, the agent stops processing. How you detect this depends on whether you use `generate()` or `stream()`.
298
207
 
299
- For example, if an agent uses the `PIIDetector` with `strategy: "block"` and the request includes a credit card number, it will be blocked and the response will include tripwire information.
208
+ ### With `generate()`
300
209
 
301
- #### `.generate()` example
210
+ Check the `tripwire` field on the result:
302
211
 
303
212
  ```typescript
304
213
  const result = await agent.generate('Is this credit card number valid?: 4543 1374 5089 4332')
@@ -306,14 +215,12 @@ const result = await agent.generate('Is this credit card number valid?: 4543 137
306
215
  if (result.tripwire) {
307
216
  console.error('Blocked:', result.tripwire.reason)
308
217
  console.error('Processor:', result.tripwire.processorId)
309
- // Optional: check if retry was requested
310
- console.error('Retry requested:', result.tripwire.retry)
311
- // Optional: access additional metadata
312
- console.error('Metadata:', result.tripwire.metadata)
313
218
  }
314
219
  ```
315
220
 
316
- #### `.stream()` example
221
+ ### With `stream()`
222
+
223
+ Listen for `tripwire` chunks in the stream:
317
224
 
318
225
  ```typescript
319
226
  const stream = await agent.stream('Is this credit card number valid?: 4543 1374 5089 4332')
@@ -326,49 +233,95 @@ for await (const chunk of stream.fullStream) {
326
233
  }
327
234
  ```
328
235
 
329
- In this case, the `reason` indicates that a credit card number was detected:
236
+ ## Speed up guardrails
330
237
 
331
- ```text
332
- PII detected. Types: credit-card
333
- ```
238
+ Guardrail processors that use an LLM (moderation, PII detection, prompt injection) add latency to every request. Three techniques reduce this overhead.
334
239
 
335
- ### Requesting retries
240
+ ### Run guardrails in parallel
336
241
 
337
- Processors can request that the LLM retry its response with feedback. This is useful for implementing quality checks:
242
+ By default, processors run sequentially. Guardrails that only `block` (and never mutate messages) are independent and can run at the same time using a [workflow processor](https://mastra.ai/docs/agents/processors).
338
243
 
339
- ```typescript
340
- export class QualityChecker implements Processor {
341
- id = 'quality-checker'
244
+ You can also mix `block` and `redact` strategies in a single parallel step. Map to the `redact` branch so its transformed messages carry forward.
342
245
 
343
- async processOutputStep({ text, abort, retryCount }) {
344
- const score = await evaluateQuality(text)
246
+ For output guardrails, run `TokenLimiterProcessor` and `BatchPartsProcessor` sequentially _before_ the parallel step, and any `redact` processors that depend on each other sequentially _after_ it:
345
247
 
346
- if (score < 0.7 && retryCount < 3) {
347
- // Request retry with feedback for the LLM
348
- abort('Response quality too low. Please be more specific.', {
349
- retry: true,
350
- metadata: { score },
351
- })
352
- }
248
+ ```typescript
249
+ import { createWorkflow, createStep } from '@mastra/core/workflows'
250
+ import {
251
+ ProcessorStepSchema,
252
+ PIIDetector,
253
+ ModerationProcessor,
254
+ SystemPromptScrubber,
255
+ TokenLimiterProcessor,
256
+ BatchPartsProcessor,
257
+ } from '@mastra/core/processors'
353
258
 
354
- return []
355
- }
356
- }
259
+ export const outputGuardrails = createWorkflow({
260
+ id: 'output-guardrails',
261
+ inputSchema: ProcessorStepSchema,
262
+ outputSchema: ProcessorStepSchema,
263
+ })
264
+ // Sequential: limit tokens first, then batch stream chunks
265
+ .then(createStep(new TokenLimiterProcessor({ limit: 1000 })))
266
+ .then(createStep(new BatchPartsProcessor()))
267
+ // Parallel: run independent checks at the same time
268
+ .parallel([
269
+ createStep(
270
+ new PIIDetector({
271
+ strategy: 'redact',
272
+ }),
273
+ ),
274
+ createStep(
275
+ new ModerationProcessor({
276
+ strategy: 'block',
277
+ }),
278
+ ),
279
+ ])
280
+ // Map to the redact branch to keep its transformed messages
281
+ .map(async ({ inputData }) => {
282
+ return inputData['processor:pii-detector']
283
+ })
284
+ // Sequential: scrubber depends on previous redaction output
285
+ .then(
286
+ createStep(
287
+ new SystemPromptScrubber({
288
+ strategy: 'redact',
289
+ placeholderText: '[REDACTED]',
290
+ }),
291
+ ),
292
+ )
293
+ .commit()
357
294
  ```
358
295
 
359
- The `abort()` function accepts an optional second parameter with:
296
+ See [workflows as processors](https://mastra.ai/docs/agents/processors) for more details on `.parallel()` and `.map()`.
360
297
 
361
- - `retry: true` - Request the LLM retry the step
362
- - `metadata: unknown` - Attach additional data for debugging/logging
298
+ ### Choose a fast model
363
299
 
364
- Use `retryCount` to track retry attempts and prevent infinite loops.
300
+ Guardrail processors don't need your primary model. Use a small, fast model for classification tasks:
365
301
 
366
- ## Custom processors
302
+ ```typescript
303
+ const GUARDRAIL_MODEL = 'openai/gpt-5-nano'
304
+
305
+ new ModerationProcessor({ model: GUARDRAIL_MODEL })
306
+ new PIIDetector({ model: GUARDRAIL_MODEL })
307
+ new PromptInjectionDetector({ model: GUARDRAIL_MODEL })
308
+ ```
367
309
 
368
- If the built-in processors don’t cover your needs, you can create your own by extending the `Processor` class.
310
+ ### Batch stream parts
311
+
312
+ Output guardrails that implement `processOutputStream` run on every streamed chunk. Use `BatchPartsProcessor` _before_ heavier processors to combine chunks and reduce the number of LLM classification calls:
313
+
314
+ ```typescript
315
+ outputProcessors: [
316
+ new BatchPartsProcessor({ batchSize: 10 }),
317
+ // Heavier processors now run on batched chunks instead of individual ones
318
+ new PIIDetector({ model: GUARDRAIL_MODEL, strategy: 'redact' }),
319
+ new ModerationProcessor({ model: GUARDRAIL_MODEL, strategy: 'block' }),
320
+ ]
321
+ ```
369
322
 
370
- Available examples:
323
+ ## Related
371
324
 
372
- - [Message Length Limiter](https://github.com/mastra-ai/mastra/tree/main/examples/processors-message-length-limiter)
373
- - [Response Length Limiter](https://github.com/mastra-ai/mastra/tree/main/examples/processors-response-length-limiter)
374
- - [Response Validator](https://github.com/mastra-ai/mastra/tree/main/examples/processors-response-validator)
325
+ - [Processors](https://mastra.ai/docs/agents/processors): How processors work, execution order, custom processors, and retry mechanism
326
+ - [`Processor` Interface](https://mastra.ai/reference/processors/processor-interface): API reference for the `Processor` interface
327
+ - [Memory Processors](https://mastra.ai/docs/memory/memory-processors): Processors for message history, semantic recall, and working memory