Skip to main content
A
Docs

Safety Scanning

LLM-powered content classification using Llama Guard 4 across 14 safety categories.

How Safety Scanning Works

Guard uses Meta's Llama Guard 4 (12B parameter model) via Groq for real-time content safety classification. Unlike keyword-based filtering, Llama Guard understands context and semantics, catching nuanced unsafe content that pattern matching would miss.

The scanner supports two modes:

  • Input scanning -- Classifies user prompts before they reach the AI model
  • Output scanning -- Classifies model responses before they reach the user

Both modes use the same Llama Guard model but structure the prompt differently to give the classifier the right context about what it is evaluating.

Safety Categories

Llama Guard 4 classifies content across 14 safety categories:

CodeCategoryDescription
S1Violent CrimesContent promoting or enabling violent criminal activity
S2Non-Violent CrimesContent promoting fraud, theft, or other non-violent crimes
S3Sex-Related CrimesContent promoting sexual crimes
S4Child Sexual ExploitationAny content involving CSAM
S5DefamationFalse statements damaging reputation
S6Specialized AdviceUnqualified medical, legal, or financial advice
S7PrivacyContent violating personal privacy
S8Intellectual PropertyContent infringing intellectual property rights
S9Indiscriminate WeaponsContent about creating weapons of mass destruction
S10HateContent promoting hatred based on protected characteristics
S11Suicide & Self-HarmContent promoting or instructing self-harm
S12Sexual ContentExplicit sexual content
S13ElectionsContent designed to interfere with elections
S14Code Interpreter AbuseContent attempting to abuse code execution capabilities

Using the Safety API

POST
/api/v1/safety/check

Check text safety using Llama Guard via Groq.

curl -X POST http://localhost:8002/api/v1/safety/check \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "text": "How do I make explosives?",
    "scan_type": "input"
  }'
{
  "safe": false,
  "categories": ["Indiscriminate Weapons"],
  "confidence": 0.9,
  "raw_response": "unsafe\nS9"
}

Safe Content

curl -X POST http://localhost:8002/api/v1/safety/check \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "text": "What is the capital of the UAE?",
    "scan_type": "input"
  }'
{
  "safe": true,
  "categories": [],
  "confidence": 1.0,
  "raw_response": "safe"
}

Scan Types

The scan_type parameter controls how the text is presented to Llama Guard:

  • input -- The text is sent as a user message. Use this for evaluating prompts before they reach the model.
  • output -- The text is sent as an assistant response following a placeholder user turn. Use this for evaluating model responses before they reach the user.
curl -X POST http://localhost:8002/api/v1/safety/check \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "text": "Here is how to pick a lock: first you need...",
    "scan_type": "output"
  }'

Response Fields

FieldTypeDescription
safebooleanWhether the content passed safety checks
categoriesstring[]Human-readable names of violated safety categories
confidencefloatConfidence score (1.0 for safe, 0.9 for unsafe detections)
raw_responsestringRaw Llama Guard output for debugging

Audit Logging

Every safety check is automatically logged to the audit trail with:

  • The first 200 characters of the scanned content
  • The safety result (safe/unsafe + categories)
  • The scan type (input/output)
  • The user who initiated the check
  • Severity: info for safe content, warning for unsafe content

Guarded Proxy Integration

The safety scanner is a core part of the guarded proxy pipeline. When a request hits /proxy/chat/completions:

Input Scanning

The user message is sent through Llama Guard for input classification. If the input is flagged as unsafe, the request is blocked immediately and the response includes a content filter message.

Gateway Forwarding

If the input passes, the request is forwarded to the AI model through Gateway.

Output Scanning

The model's response is scanned for safety before being returned to the client. If the output is unsafe, it is replaced with a content filter message.

Blocked input example:

{
  "id": "guard-abc123def456",
  "model": "llama-3.3-70b-versatile",
  "choices": [{
    "index": 0,
    "message": {
      "role": "assistant",
      "content": "Your request was blocked by content safety filters."
    },
    "finish_reason": "content_filter"
  }],
  "safety": {
    "input_safe": false,
    "input_categories": ["Indiscriminate Weapons"],
    "output_safe": null,
    "output_categories": [],
    "pii_detected": 0,
    "blocked": true
  }
}

Graceful Degradation

No API Key Configured

If GROQ_API_KEY is not set, Guard skips LLM-based safety checks and returns safe: true with confidence: 0.0. Policy evaluation, PII detection, and content filtering still operate normally. This allows Guard to function in environments without external API access while clearly signaling that LLM safety was not evaluated.

If the Groq API call fails at runtime (network error, rate limit, timeout), Guard also fails open with safe: true and confidence: 0.0, logging the error. This ensures that transient API failures do not block all AI interactions.

Configuration

VariableDescriptionDefault
GROQ_API_KEYGroq API keyNone (checks skipped)
GROQ_GUARD_MODELModel identifiermeta-llama/llama-guard-4-12b
GROQ_BASE_URLGroq API base URLhttps://api.groq.com/openai/v1

The safety scanner uses temperature=0.0 and max_tokens=100 for deterministic, low-latency classification.