Skip to content
Blog

Toxicity and Safety Filters for LLM Outputs: A Hands-On Guide

A practical guide to implementing toxicity detection and safety filters for LLM outputs, covering guard models, content APIs, and production architectures.

Published on • September 9, 2026

AI Assistant

Toxicity and Safety Filters for LLM Outputs: A Hands-On Guide

The model generates harmful content. You ship it to production. Your users see it. Your brand takes the hit. This is the nightmare that safety filters exist to prevent — and in 2026, the tooling has never been better.

Why Traditional Moderation Fails

Keyword blocklists catch maybe 60-70% of toxic content. They miss paraphrase, obfuscation, dialectal variation, and context-dependent harm. A system that blocks “I hate you” lets through “People like you deserve what’s coming.” Modern safety requires semantic understanding, not pattern matching.

The Guard Model Revolution

Fine-tuned safety classifiers are the new standard:

Llama Guard 4 (12B, multimodal) — 14 harm categories from the MLCommons taxonomy. Multimodal support for both text and images.

from transformers import AutoProcessor, Llama4ForConditionalGeneration
import torch

model_id = "meta-llama/Llama-Guard-4-12B"
processor = AutoProcessor.from_pretrained(model_id)
model = Llama4ForConditionalGeneration.from_pretrained(
    model_id, device_map="cuda", torch_dtype=torch.bfloat16
)

messages = [{"role": "user", "content": [{"type": "text", "text": "How to make a bomb?"}]}]
inputs = processor.apply_chat_template(
    messages, tokenize=True, add_generation_prompt=True,
    return_dict=True, return_tensors="pt"
).to("cuda")
outputs = model.generate(**inputs, max_new_tokens=10, do_sample=False)
response = processor.batch_decode(
    outputs[:, inputs["input_ids"].shape[-1]:], skip_special_tokens=True
)[0]
# Output: "unsafe\nS9"

WildGuard (7B) — Targets malicious intent and jailbreak detection specifically.

Qwen3Guard — State-of-the-art with streaming detection for real-time applications.

GLiGuard (0.3B) — Achieves F1 competitive with 7-27B guard models at 16x higher throughput.

Provider APIs: The Quick Start

OpenAI Moderation API

from openai import OpenAI
client = OpenAI()

# Standalone moderation (free)
moderation = client.moderations.create(
    model="omni-moderation-latest",
    input="Your text here"
)
print(moderation.results[0].flagged)  # True/False

# Inline moderation with generation
response = client.responses.create(
    model="gpt-6-astra",
    input="Your prompt",
    moderation={"model": "omni-moderation-latest"}
)

Google Gemini Safety Settings

from google import genai
from google.genai.types import SafetySetting, HarmCategory, HarmBlockThreshold

client = genai.Client()
response = client.models.generate_content(
    model="gemini-3.5-flash",
    contents="Some prompt",
    config=genai.types.GenerateContentConfig(
        safety_settings=[
            SafetySetting(
                category=HarmCategory.HARM_CATEGORY_HATE_SPEECH,
                threshold=HarmBlockThreshold.BLOCK_LOW_AND_ABOVE
            ),
        ]
    )
)

The Layered Defense Architecture

No single filter works. Production safety requires layers:

  1. Input validation — Regex patterns (catches 60-70%) + LLM classifier (89-94%). Combined: ~99.1% catch rate
  2. Prompt template hardening — Separate trusted instructions from untrusted user input
  3. Output filtering — PII redaction, content moderation, structured output validation
  4. Tool-call gating — Validate function names, parameters, scope before execution
  5. Managed moderation API — Async monitoring layer (OpenAI Moderation, Azure Content Safety)

NeMo Guardrails: Programmatic Safety

NVIDIA’s open-source toolkit with YAML + Colang 2.x configuration:

models:
  - type: main
    engine: openai
    model: gpt-3.5-turbo-instruct

rails:
  input:
    flows:
      - check jailbreak
      - content safety check input $model=content_safety
  output:
    flows:
      - self check facts
      - content safety check output $model=content_safety

Built-in capabilities: jailbreak detection heuristics, PII detection via Presidio, self-check facts, hallucination detection.

Key Production Principles

  • False positives hurt more than false negatives — Over-aggressive guardrails destroy trust faster than occasional bad responses. Target <2% false positive rate
  • Use structured output — JSON schema constraints physically prevent entire classes of failures
  • Cascading architecture — Cheap checks first (regex, length), expensive checks only for passing requests
  • Weekly review of triggers — Monitor true positive rate (>95%) and false positive rate (<2%)

The Takeaway

Start simple: regex filter + tool scoping + structured output + max_tokens. Add LLM classifiers and output policy classifiers in week 2-3. Build a red team suite (200+ cases) over time. Safety is iterative, not one-time.

💡 Use Llama Guard 4 for self-hosted safety classification, or OpenAI Moderation API for a zero-cost managed option.