Skip to main content

What is Safety Engine?

The Safety Engine is a powerful content filtering and policy enforcement system that helps you maintain safe, appropriate, and compliant AI interactions. Just like humans need guidelines and rules to ensure appropriate behavior, AI agents need safety policies to filter content, block inappropriate material, and protect sensitive information. The key benefits of the Safety Engine are:
  • Content Filtering: Automatically detect and block inappropriate content like adult material, hate speech, or sensitive topics
  • Privacy Protection: Anonymize or redact sensitive information like phone numbers, emails, or personal data
  • Compliance: Ensure your AI applications meet regulatory requirements and platform policies
  • Customizable: Create your own policies or use pre-built ones for common use cases
  • Dual Protection: Apply policies to both user input (user_policy) and agent output (agent_policy)

Quick Example: Banking Assistant with Safety Engine

Here’s a simple example of a banking assistant that blocks cryptocurrency content:
uv pip install upsonic
# pip install upsonic
# Upsonic Docs: Add a Safety Engine
# https://docs.upsonic.ai/guides/3-add-a-safety-engine

# Imports
from upsonic import Agent, Task
from upsonic.safety_engine import CryptoBlockPolicy, AnonymizePhoneNumbersPolicy, SensitiveSocialBlockPolicy

# Banking Assistant with Multiple Safety Policies
banking_assistant = Agent(
    model="openai/gpt-5-mini",
    name="Banking Assistant V1",
    role="Certified banking assistant providing financial guidance",
    goal="Help customers with banking services while maintaining regulatory compliance and data protection",
    instructions="""
    You are a banking assistant. Provide information about traditional banking products 
    like savings accounts, checking accounts, loans, and investment products.
    Always comply with banking regulations and protect customer privacy.
    """,
    user_policy=CryptoBlockPolicy,  # Block cryptocurrency content per banking regulations
)

# Test Task with Crypto Content (Should be Blocked)
crypto_task = Task(
    description="I want to invest in Bitcoin and Ethereum through my bank account. Can you help me set up crypto trading?",
)

# Test Task with Safe Banking Content (Should Pass)
safe_task = Task(
    description="I'm 25 years old and want to open a high-yield savings account. What are the best options available?",
)


# Run the tasks
print("=== Testing Crypto Content (Should be Blocked) ===")
banking_assistant.print_do(crypto_task)

print("\n=== Testing Safe Banking Content (Should Pass) ===")
banking_assistant.print_do(safe_task)

print("Crypto Task Result:", crypto_task.response)
print("Safe Task Result:", safe_task.response[:100] + "...")

Using Safety Policies

The Safety Engine offers flexibility in how you protect your AI applications. You can use pre-built policies for common use cases or create your own custom policies tailored to your specific needs. Pre-built Policies: Ready-to-use policies for common scenarios like blocking crypto content, anonymizing phone numbers, or filtering adult content. Custom Policies: Build your own policies with custom rules and actions to match your specific compliance requirements. Both approaches work the same way - simply pass them to your Agent’s user_policy or agent_policy parameters.

Need more advanced features?

The Safety Engine supports many powerful configuration options to meet your compliance and security needs:
  • Custom Policy Creation: Build your own policies with custom rules and actions tailored to your specific compliance requirements.
  • Multiple Policy Types: Combine blocking, anonymization, and exception policies for comprehensive regulatory compliance.
  • LLM-Enhanced Detection: Use AI-powered content detection for better accuracy in identifying risks and compliance violations.
  • Privacy Protection: Automatically anonymize sensitive information like SSNs, account numbers, and personal data.
  • Dual Protection: Apply different policies to user input (user_policy) and agent responses (agent_policy) for complete coverage.
  • Multi-Language Support: Automatic language detection and localized responses for global applications.
  • Audit Trail: Monitor policy triggers, confidence scores, and compliance actions for regulatory reporting and risk management.
For detailed examples and advanced patterns, see our comprehensive Safety Engine Concept Documentation.