Safety Engine
Protect your AI agents with built-in content safety policies and guardrails.Overview
The Safety Engine lets you control what goes into your agents (user input) and what comes out (agent responses). Add policies to automatically detect and handle sensitive content like PII, prohibited topics, or custom safety rules—all with just a few lines of code.How It Works
Safety Engine operates at two key points:- Before: Filter user input with
user_policy
- After: Sanitize agent output with
agent_policy
Quick Start
Your First Safe Agent
Understanding Policies
Every policy has two parts that work together:Part | What It Does | Example |
---|---|---|
Rule | Finds specific content | ”Does this text contain credit card numbers?” |
Action | Decides what to do | ”Yes → anonymize them” |
When Policies Run
Using Policies with Your Agents
Protect Both Input and Output
Input Filtering Only
Output Sanitization Only
Ready-to-Use Policies
Upsonic comes with policies you can use right away. Just import and add to your agent.Cryptocurrency Content
Perfect for financial services that need to avoid crypto discussions:CryptoBlockPolicy_LLM_Block
- Smarter detection, better error messagesCryptoBlockPolicy_LLM_Finder
- AI-powered detection for edge casesCryptoReplacePolicy
- Replace crypto terms instead of blockingCryptoRaiseExceptionPolicy
- Stop execution immediately (strict mode)
Personal Information (PII)
Protect emails, phone numbers, SSNs, addresses, and more:PIIBlockPolicy
- Block any content with PIIPIIBlockPolicy_LLM
- Smarter PII detection with AIPIIReplacePolicy
- Replace PII with[REDACTED]
PIIRaiseExceptionPolicy
- Stop execution when PII detected
Phone Numbers
Specifically for phone number protection:AnonymizePhoneNumbersPolicy_LLM_Finder
for better detection
What Actions Can Do
When a policy detects something, it can handle it in different ways:Action | What Happens | When to Use |
---|---|---|
BLOCK | Shows a message, stops the task | ”Sorry, I can’t help with that” |
ALLOW | Lets it through | Content is safe |
REPLACE | Swaps keywords with placeholder text | Remove but keep working |
ANONYMIZE | Randomizes but keeps format | Hide data, preserve structure |
RAISE_EXCEPTION | Throws error, stops everything | Critical violations only |
See Actions in Practice
Build Your Own Policies
Need something specific? Create custom policies for your unique requirements.Simple Keyword Blocker
Smarter Detection with AI
Use LLM-powered policies for better accuracy and context understanding.Why Use LLM Detection?
- Catches edge cases: Understands context, not just keywords
- Better messages: Generates natural explanations
- Handles variations: Detects intent, not just exact matches
Smart Detection Example
Works in Any Language
Policies automatically adapt to the user’s language.language="tr"
for Turkish, language="es"
for Spanish, etc.
Advanced Options
Use Different Models for Different Tasks
Add Custom Keywords
Works with Async
Exception Handling
Real-World Examples
Financial Advisor Bot
Customer Support Bot
Healthcare Bot (HIPAA Compliant)
Tips & Best Practices
When to Use Which Policy
- user_policy: Filter what comes IN (block bad user input)
- agent_policy: Clean what goes OUT (protect sensitive data in responses)
- Both: Maximum protection for critical applications
Choosing Actions
- BLOCK: Say “no” clearly → best for prohibited content
- ANONYMIZE: Hide but keep working → best for PII in customer support
- REPLACE: Swap keywords → best for sanitizing specific terms
- RAISE_EXCEPTION: Stop immediately → best for compliance violations
Performance Tips
- Static rules are fast: Keyword matching is instant
- LLM rules are smart: Better accuracy but slower
- Mix both: Use static rules first, LLM for edge cases
- Use async:
do_async()
works great with policies
Security Recommendations
- Layer your defenses: Use multiple policies for critical systems
- Test thoroughly: Make sure policies catch what you want without false positives
- Monitor triggers: Log when policies activate to improve them
- Keep updated: Add new keywords and patterns as threats evolve