Skip to main content
# pip install "upsonic[safety-engine]"
uv pip install "upsonic[safety-engine]"

What is Phone Number Policy?

Phone number policies specifically detect and anonymize phone numbers in various formats (US, international, etc.).

Why its Important?

Phone number policies are essential for protecting user privacy and preventing phone numbers from being exposed through AI interactions. These policies prevent phone numbers from being sent to LLMs, which helps maintain user privacy and protects against spam, harassment, and identity theft.
  • Prevents sending phone numbers to LLM: Blocks phone numbers from being processed by language models, protecting user contact information
  • Protects user privacy: Prevents phone numbers from being exposed to third-party LLM providers or stored in training data
  • Reduces spam and harassment risk: Anonymizes phone numbers to prevent them from being used for unwanted communications

Anonymize (Unique Random Placeholders)

AnonymizePhoneNumbersPolicy replaces detected phone numbers with unique random placeholders. Original values are restored in the agent’s response:
from upsonic import Agent, Task
from upsonic.safety_engine.policies.phone_policies import AnonymizePhoneNumbersPolicy

agent = Agent(
    "anthropic/claude-sonnet-4-6",
    user_policy=AnonymizePhoneNumbersPolicy,
    debug=True
)

task = Task(
    description="My Number is: +1-555-123-4567. Tell me what is my number."
)

result = agent.print_do(task)
print(result)  # "Your number is +1-555-123-4567"

Streaming

Phone number policies work with streaming — anonymized phone numbers are de-anonymized token-by-token in real-time:
import asyncio
from upsonic import Agent, Task
from upsonic.safety_engine.policies.phone_policies import AnonymizePhoneNumbersPolicy

async def main():
    agent = Agent(
        "anthropic/claude-sonnet-4-6",
        user_policy=AnonymizePhoneNumbersPolicy,
        debug=True,
    )

    task = Task(
        description="My Number is: +1-555-123-4567. Tell me what is my number."
    )

    async for text in agent.astream(task):
        print(text, end="", flush=True)
    print()

asyncio.run(main())

Async

import asyncio
from upsonic import Agent, Task
from upsonic.safety_engine.policies.phone_policies import AnonymizePhoneNumbersPolicy

async def main():
    agent = Agent(
        "anthropic/claude-sonnet-4-6",
        user_policy=AnonymizePhoneNumbersPolicy,
        debug=True,
    )

    task = Task(
        description="My Number is: +1-555-123-4567. Tell me what is my number."
    )

    result = await agent.print_do_async(task)
    print(result)  # "Your number is +1-555-123-4567"

asyncio.run(main())

Available Variants

  • AnonymizePhoneNumbersPolicy: Pattern-based detection and anonymization with unique random replacements
  • AnonymizePhoneNumbersPolicy_LLM_Finder: LLM-powered detection for better accuracy