AI Prompt Guard

Agentgateway can inspect and filter LLM requests to prevent prompt injection attacks and block sensitive data like PII from being sent to AI models.

What you’ll build

In this tutorial, you configure the following.

  1. Configure prompt guard policies for LLM requests
  2. Block sensitive data like SSNs and email addresses from reaching the LLM
  3. Use both custom regex patterns and built-in patterns for filtering
  4. Test the prompt guard to see requests blocked in real-time

Before you begin

Step 1: Set up your environment

Create a working directory and set your API key.

mkdir prompt-guard-test && cd prompt-guard-test
export OPENAI_API_KEY=your-api-key-here

Step 2: Create the configuration

Create a config.yaml file with prompt guard policies.

cat > config.yaml << 'EOF'
# yaml-language-server: $schema=https://agentgateway.dev/schema/config
binds:
- port: 3000
  listeners:
  - routes:
    - backends:
      - ai:
          name: openai
          provider:
            openAI:
              model: gpt-4o-mini
      policies:
        ai:
          promptGuard:
            request:
            # Block Social Security Numbers
            - regex:
                action: reject
                rules:
                - pattern: SSN
                - pattern: Social Security
              rejection:
                status: 400
                headers:
                  set:
                    content-type: "application/json"
                body: |
                  {
                    "error": {
                      "message": "Request rejected: Content contains sensitive information",
                      "type": "invalid_request_error",
                      "code": "content_policy_violation"
                    }
                  }
            # Block email addresses
            - regex:
                action: reject
                rules:
                - builtin: email
              rejection:
                status: 400
                headers:
                  set:
                    content-type: "application/json"
                body: |
                  {
                    "error": {
                      "message": "Request blocked: Contains email address",
                      "type": "invalid_request_error",
                      "code": "pii_detected"
                    }
                  }
        backendAuth:
          key: "$OPENAI_API_KEY"
EOF

Configuration explained

SettingDescription
policies.ai.promptGuardThe prompt guard policy that inspects LLM requests
requestRules applied to incoming requests before they reach the LLM
regex.action: rejectBlock requests that match the patterns
regex.rulesList of patterns to match against
patternCustom regex pattern to match
builtinUse a built-in pattern (like email)
rejectionCustom response returned when a request is blocked

Step 3: Start agentgateway

agentgateway -f config.yaml

You should see output indicating the gateway is running on port 3000.

Step 4: Test normal requests

In a new terminal, send a normal request.

curl http://localhost:3000/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{
    "model": "gpt-4o-mini",
    "messages": [{"role": "user", "content": "Hello, how are you?"}]
  }'

You should receive a normal response from the LLM.

Step 5: Test blocked requests

Block SSN mentions

curl http://localhost:3000/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{
    "model": "gpt-4o-mini",
    "messages": [{"role": "user", "content": "My SSN is 123-45-6789"}]
  }'

Expected response (request blocked by prompt guard):

{
  "error": {
    "message": "Request rejected: Content contains sensitive information",
    "type": "invalid_request_error",
    "code": "content_policy_violation"
  }
}

Block email addresses

curl http://localhost:3000/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{
    "model": "gpt-4o-mini",
    "messages": [{"role": "user", "content": "Contact me at test@example.com"}]
  }'

Expected response (request blocked by prompt guard):

{
  "error": {
    "message": "Request blocked: Contains email address",
    "type": "invalid_request_error",
    "code": "pii_detected"
  }
}

Built-in patterns

Agentgateway includes built-in patterns for common PII types.

PatternDescription
emailEmail addresses
phonePhone numbers
ssnSocial Security Numbers
credit_cardCredit card numbers
ip_addressIP addresses

Example using built-in SSN pattern:

- regex:
    action: reject
    rules:
    - builtin: ssn

Custom regex patterns

Add your own regex patterns to catch credentials, secrets, or custom data.

policies:
  ai:
    promptGuard:
      request:
      - regex:
          action: reject
          rules:
          - pattern: "password[=:]\\s*\\S+"
          - pattern: "api[_-]?key[=:]\\s*\\S+"
          - pattern: "secret[=:]\\s*\\S+"
        rejection:
          status: 400
          headers:
            set:
              content-type: "application/json"
          body: |
            {
              "error": {
                "message": "Request contains credentials",
                "type": "invalid_request_error",
                "code": "credentials_detected"
              }
            }

Response filtering

You can also filter LLM responses to mask sensitive data before it reaches the client:

policies:
  ai:
    promptGuard:
      response:
      - regex:
          action: mask
          rules:
          - builtin: credit_card
          replacement: "[REDACTED]"

Cleanup

Stop the agentgateway with Ctrl+C and remove the test directory:

cd .. && rm -rf prompt-guard-test

Learn more

Agentgateway assistant

Ask me anything about agentgateway configuration, features, or usage.

Note: AI-generated content might contain errors; please verify and test all returned information.

Tip: one topic per conversation gives the best results. Use the + button in the chat header to start a new conversation.

Switching topics? Starting a new conversation improves accuracy.
↑↓ navigate select esc dismiss

What could be improved?

Your feedback helps us improve assistant answers and identify docs gaps we should fix.

Need more help? Join us on Discord: https://discord.gg/y9efgEmppm

Want to use your own agent? Add the Solo MCP server to query our docs directly. Get started here: https://search.solo.io/.