Skip to content
agentgateway has joined the Agentic AI FoundationLearn more

For the complete documentation index, see llms.txt. Markdown versions of all docs pages are available by appending .md to any docs URL.

Page as Markdown

OpenAI moderation

Detects potentially harmful content across categories including hate, harassment, self-harm, sexual content, and violence with the OpenAI moderation API.

The OpenAI Moderation API detects potentially harmful content across categories including hate, harassment, self-harm, sexual content, and violence.

Before you begin

  1. Set up an agentgateway proxy.
  2. Set up access to the OpenAI LLM provider.

Block harmful content

  1. Configure the prompt guard to use OpenAI Moderation:

    kubectl apply -f - <<EOF
    apiVersion: agentgateway.dev/v1alpha1
    kind: AgentgatewayPolicy
    metadata:
      name: openai-prompt-guard
      namespace: agentgateway-system
    spec:
      targetRefs:
      - group: gateway.networking.k8s.io
        kind: HTTPRoute
        name: openai
      backend:
        ai:
          promptGuard:
            request:
            - openAIModeration:
                policies:
                  auth:
                    secretRef:
                      name: openai-secret
                model: omni-moderation-latest
              response:
                message: "Content blocked by moderation policy"
    EOF

    The policies field supports more than the secretRef authentication shown here. You can choose a different authentication method or tune the connection that agentgateway opens to the Moderation API, such as setting a request timeout or custom TLS. For all the options, see Backend connection and authentication policies.

  2. Test with content that triggers moderation.

    curl -i "$INGRESS_GW_ADDRESS/openai" \
      -H "content-type: application/json" \
      -d '{
        "model": "gpt-4o-mini",
        "messages": [
          {
            "role": "user",
            "content": "I want to harm myself"
          }
        ]
      }'

    Expected response:

    HTTP/1.1 403 Forbidden
    Content blocked by moderation policy

Backend connection and authentication policies

The policies field configures how agentgateway connects and authenticates to the OpenAI Moderation API when it evaluates a request.

Authentication

Under policies.auth, set one credential source (secretRef or key). Optionally, set location to control where the credential is placed.

MethodDescription
secretRefRead the API key from a Kubernetes secret. By default, the key that matches the credential location is used, such as Authorization for the default header location. To use a different key, set secretRef.key.
keySend an inline API key in the Authorization header. This option is the least secure. Use a secret instead when possible.
locationWhere to place the credential. Defaults to the Authorization header with a Bearer prefix. To change it, set a header, queryParameter, or cookie.

Backend connection settings

You can also tune the connection that agentgateway opens to the OpenAI Moderation backend by setting the following BackendConnectionPolicy fields under policies.

SettingDescription
tlsTLS settings for the connection, such as a custom CA certificate or SNI.
httpHTTP settings, such as the requestTimeout and HTTP protocol version.
tcpTCP connection settings.
tunnelTunnel settings, such as an HTTPS_PROXY, used to reach the backend.

For example, the following prompt guard authenticates with a secret and sets a request timeout for the calls to the Moderation API.

- openAIModeration:
    model: omni-moderation-latest
    policies:
      auth:
        secretRef:
          name: openai-secret
      http:
        requestTimeout: 5s

For the full set of fields, see the API reference.

Cleanup

You can remove the resources that you created in this guide.
kubectl delete AgentgatewayPolicy openai-prompt-guard -n agentgateway-system 
Was this page helpful?
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/.