Output Filtering (Llama Guard, NeMo)
Run a smaller guard model over outputs to catch toxicity, PII leaks, and policy violations before they ship.
Output Filtering (Llama Guard, NeMo) is a free AI Agents lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the AI Agents learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Filter Outputs?
Even with safe inputs, models can output:
- Personal data leaks
- Hate speech / harassment
- Self-harm content
- Tool calls that violate user intent
An output filter is your last line of defense before the user sees anything.
Llama Guard
Meta's safety classifier — open weights, very fast:
from transformers import AutoTokenizer, AutoModelForCausalLM
model = AutoModelForCausalLM.from_pretrained('meta-llama/Llama-Guard-3-8B')
# Outputs 'safe' or 'unsafe' with category.NeMo Guardrails
NVIDIA NeMo Guardrails — Python framework that wraps LLMs with checks:
# pip install nemoguardrails
from nemoguardrails import RailsConfig, LLMRails
config = RailsConfig.from_path('./config')
rails = LLMRails(config)
response = rails.generate(messages=[{'role': 'user', 'content': '...'}])Guardrails AI
guardrails-ai is the Python library focused on validation/guards. Supports XML-based "rails" and reAsk repair loops.
Anthropic Constitutional Classifier
Anthropic released a classifier (in addition to the safety training in Claude). Useful for post-hoc filtering of any model output.
Custom LLM-Based Filter
Cheapest path: a smaller LLM that screens outputs:
FILTER_PROMPT = '''
Analyse the output. Return JSON: {safe: true/false, reason: str, categories: list}
Unsafe categories: PII leak, harmful instructions, copyright violation, prompt-injection echo.
Output:
{output}
'''
result = guard_llm.invoke(FILTER_PROMPT.format(output=text))Regex / Rule Filters
For specific patterns, regex is fast and reliable:
import re
EMAIL_RE = re.compile(r'[\w\.-]+@[\w\.-]+')
SSN_RE = re.compile(r'\b\d{3}-\d{2}-\d{4}\b')
def has_pii(text):
return bool(EMAIL_RE.search(text) or SSN_RE.search(text))
# --- demo ---
samples = ['Contact me at jane@example.com', 'My SSN is 123-45-6789', 'No PII in this sentence']
for s in samples:
print(f'{s!r} -> has_pii={has_pii(s)}')
PII Redaction
Don't just block — sometimes redact and let the rest through:
def redact(text):
text = EMAIL_RE.sub('[email]', text)
text = SSN_RE.sub('[ssn]', text)
return textTwo-Layer Filter
Fast rules first, expensive LLM second:
def filter_output(text):
if has_obvious_pii(text):
return BLOCKED
result = guard_llm.invoke(...)
return resultStreaming Outputs
For streamed outputs, filter SENTENCE-BY-SENTENCE:
buf = ''
for token in stream:
buf += token
if buf.endswith(('. ', '! ', '? ', '\n')):
if not safe(buf):
stream.close()
emit_to_client('[content filtered]')
break
emit_to_client(buf)
buf = ''False Positives vs False Negatives
Tune for the right balance:
- High-stakes (medical, financial): err toward false positives (block more)
- Creative/entertainment: err toward false negatives (block less)
Filter Logs Are Sensitive
Filter logs contain the blocked content. Store securely with short TTLs:
log.warning('Blocked output', extra={'reason': r, 'category': c}, sanitize_payload=True)Educate the User
When you block, tell the user why so they don't spam retries:
def handle_privacy_request():
return 'I cannot share that information for privacy reasons.'
print(handle_privacy_request())Multi-Layer Filter
Why combine regex with LLM-based filtering?
Recap
Llama Guard / NeMo / custom LLM filter + regex rules. Two layers. Redact when possible. Always tell the user when content is blocked.
Frequently asked questions
Is the “Output Filtering (Llama Guard, NeMo)” lesson free?
Yes — the full text of “Output Filtering (Llama Guard, NeMo)” is free to read here on the web, and the AI Agents course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the AI Agents course, upgrade to CoddyKit PRO.
What will I learn in “Output Filtering (Llama Guard, NeMo)”?
Run a smaller guard model over outputs to catch toxicity, PII leaks, and policy violations before they ship. You practise AI Agents with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start AI Agents?
No prior experience is required. AI Agents on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Output Filtering (Llama Guard, NeMo)” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this AI Agents lesson?
Yes. Every AI Agents lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Prompt Injection Defences
- Output Filtering (Llama Guard, NeMo)
- Sandbox Execution for Code Agents
- Access Control on Tools