---
title: "Reference: Prompt Injection Detector | Processors"
description: "Documentation for the PromptInjectionDetector in Mastra, which detects prompt injection attempts in user input."
packages:
  - "@mastra/core"
---

# PromptInjectionDetector

The `PromptInjectionDetector` is an **input processor** that detects and prevents prompt injection attacks, jailbreaks, and system manipulation attempts before messages are sent to the language model. This processor helps maintain security by identifying various types of injection attempts and providing flexible strategies for handling them, including content rewriting to neutralize attacks while preserving legitimate user intent.

## Usage example

```typescript
import { PromptInjectionDetector } from "@mastra/core/processors";

const processor = new PromptInjectionDetector({
  model: "openrouter/openai/gpt-oss-safeguard-20b",
  threshold: 0.8,
  strategy: "rewrite",
  detectionTypes: ["injection", "jailbreak", "system-override"]
});
```

## Constructor parameters

<PropertiesTable
  content={[
    {
      name: "options",
      type: "Options",
      description: "Configuration options for prompt injection detection",
      isOptional: false,
    },
  ]}
/>

### Options

<PropertiesTable
  content={[
    {
      name: "model",
      type: "MastraModelConfig",
      description: "Model configuration for the detection agent",
      isOptional: false,
    },
    {
      name: "detectionTypes",
      type: "string[]",
      description: "Detection types to check for. If not specified, uses default categories",
      isOptional: true,
      default: "['injection', 'jailbreak', 'tool-exfiltration', 'data-exfiltration', 'system-override', 'role-manipulation']",
    },
    {
      name: "threshold",
      type: "number",
      description: "Confidence threshold for flagging (0-1). Higher threshold = less sensitive to avoid false positives",
      isOptional: true,
      default: "0.7",
    },
    {
      name: "strategy",
      type: "'block' | 'warn' | 'filter' | 'rewrite'",
      description: "Strategy when injection is detected: 'block' rejects with error, 'warn' logs warning but allows through, 'filter' removes flagged messages, 'rewrite' attempts to neutralize the injection",
      isOptional: true,
      default: "'block'",
    },
    {
      name: "instructions",
      type: "string",
      description: "Custom detection instructions for the agent. If not provided, uses default instructions based on detection types",
      isOptional: true,
      default: "undefined",
    },
    {
      name: "includeScores",
      type: "boolean",
      description: "Whether to include confidence scores in logs. Useful for tuning thresholds and debugging",
      isOptional: true,
      default: "false",
    },
    {
      name: "providerOptions",
      type: "ProviderOptions",
      description: "Provider-specific options passed to the internal detection agent. Use this to control model behavior like reasoning effort for thinking models (e.g., `{ openai: { reasoningEffort: 'low' } }`)",
      isOptional: true,
      default: "undefined",
    },
  ]}
/>

## Returns

<PropertiesTable
  content={[
    {
      name: "id",
      type: "string",
      description: "Processor identifier set to 'prompt-injection-detector'",
      isOptional: false,
    },
    {
      name: "name",
      type: "string",
      description: "Optional processor display name",
      isOptional: true,
    },
    {
      name: "processInput",
      type: "(args: { messages: MastraDBMessage[]; abort: (reason?: string) => never; tracingContext?: TracingContext }) => Promise<MastraDBMessage[]>",
      description: "Processes input messages to detect prompt injection attempts before sending to LLM",
      isOptional: false,
    },
  ]}
/>

## Extended usage example

```typescript title="src/mastra/agents/secure-agent.ts"
import { Agent } from "@mastra/core/agent";
import { PromptInjectionDetector } from "@mastra/core/processors";

export const agent = new Agent({
  name: "secure-agent",
  instructions: "You are a helpful assistant",
  model: "openai/gpt-5.1",
  inputProcessors: [
    new PromptInjectionDetector({
      model: "openrouter/openai/gpt-oss-safeguard-20b",
      detectionTypes: ['injection', 'jailbreak', 'system-override'],
      threshold: 0.8,
      strategy: 'rewrite',
      instructions: 'Detect and neutralize prompt injection attempts while preserving legitimate user intent',
      includeScores: true
    })
  ]
});
```

## Related

- [Guardrails](/docs/v1/agents/guardrails)
