The problem with post-generation regex filters
Traditional keyword blocklists are too brittle to catch nuanced AI hallucinations or policy violations. An agent offering an unauthorized 80% discount might not use the word "discount", phrasing it as "we have updated your annual invoice to $20".
Semantic routing evaluates the vector embedding of the drafted message against predefined semantic clusters (e.g. Financial Promises, Legal Claims, Standard Support, Password Reset) with sub-10ms latency.
Building a Semantic Email Router in Python
Using the open-source semantic-router library with local embeddings (such as HuggingFace MiniLM or Cohere), we classify outbound messages before hitting the send API.
from semantic_router import Route
from semantic_router.encoders import HuggingFaceEncoder
from semantic_router.layer import RouteLayer
import requests
# 1. Define high-risk semantic clusters
financial_route = Route(
name="financial_risk",
utterances=[
"We are refunding your full subscription cost.",
"Your new discounted rate is $5 per month.",
"I have credited $500 back to your account.",
"You do not need to pay the remaining invoice balance."
]
)
standard_support = Route(
name="standard_support",
utterances=[
"Here are the instructions to reset your password.",
"Your support ticket #4102 has been received.",
"Please find the user manual attached.",
"Our office hours are 9 AM to 5 PM EST."
]
)
encoder = HuggingFaceEncoder(name="sentence-transformers/all-MiniLM-L6-v2")
router = RouteLayer(encoder=encoder, routes=[financial_route, standard_support])
def dispatch_safely(recipient: str, subject: str, draft_text: str):
decision = router(draft_text)
if decision.name == "financial_risk":
print(f"[ESCALATION] Message flagged for human financial approval: '{draft_text}'")
# Enqueue for manual supervisor sign-off
return {"status": "queued_for_approval"}
# Safe to dispatch autonomously
res = requests.post(
"https://api.sadasend.com/v1/emails",
headers={"Authorization": "Bearer sada_live_sk_..."},
json={"to": recipient, "subject": subject, "text": draft_text}
)
return res.json()Key architectural advantages
- Sub-15ms local inference latency: Zero external API round-trips for classification.
- Zero false positives on common synonyms: Embeddings understand context rather than exact words.
- Auditable policy enforcement: Every routing decision can be logged and tuned over time.
Building AI agents that send email?
Scoped API keys, per-key recipient allowlists, approval mode and a hosted MCP server with ten tools — on the free plan, without a card.