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:
| Code | Category | Description |
|---|---|---|
| S1 | Violent Crimes | Content promoting or enabling violent criminal activity |
| S2 | Non-Violent Crimes | Content promoting fraud, theft, or other non-violent crimes |
| S3 | Sex-Related Crimes | Content promoting sexual crimes |
| S4 | Child Sexual Exploitation | Any content involving CSAM |
| S5 | Defamation | False statements damaging reputation |
| S6 | Specialized Advice | Unqualified medical, legal, or financial advice |
| S7 | Privacy | Content violating personal privacy |
| S8 | Intellectual Property | Content infringing intellectual property rights |
| S9 | Indiscriminate Weapons | Content about creating weapons of mass destruction |
| S10 | Hate | Content promoting hatred based on protected characteristics |
| S11 | Suicide & Self-Harm | Content promoting or instructing self-harm |
| S12 | Sexual Content | Explicit sexual content |
| S13 | Elections | Content designed to interfere with elections |
| S14 | Code Interpreter Abuse | Content attempting to abuse code execution capabilities |
Using the Safety API
/api/v1/safety/checkCheck 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
| Field | Type | Description |
|---|---|---|
safe | boolean | Whether the content passed safety checks |
categories | string[] | Human-readable names of violated safety categories |
confidence | float | Confidence score (1.0 for safe, 0.9 for unsafe detections) |
raw_response | string | Raw 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:
infofor safe content,warningfor 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
| Variable | Description | Default |
|---|---|---|
GROQ_API_KEY | Groq API key | None (checks skipped) |
GROQ_GUARD_MODEL | Model identifier | meta-llama/llama-guard-4-12b |
GROQ_BASE_URL | Groq API base URL | https://api.groq.com/openai/v1 |
The safety scanner uses temperature=0.0 and max_tokens=100 for deterministic, low-latency classification.