A regex router is only as good as the patterns you anticipated — it fails silently the moment a user phrases a known intent in an unanticipated way. Semantic routing sidesteps this by matching on meaning rather than literal string structure: it encodes the incoming query into an embedding and checks which route's example utterances it's closest to in vector space, so "I need a refund," "cancel this," and "please stop my order" can all correctly hit the same route without anyone writing a pattern for each phrasing.
Choosing between the two approaches
1. Use regex when the input format is genuinely fixed. Order numbers, specific command syntaxes, or anything with a rigid literal structure is a better fit for a pattern match — it's instant and has no model dependency.
2. Use semantic routing when intent varies in phrasing. Natural-language requests that express the same underlying intent in many different ways are exactly what embedding-based routing was built to handle.
3. Tune the similarity threshold, and keep a fallback. Semantic routers need a confidence threshold below which no route is confidently matched — route those cases to a fallback (a clarifying question, or a full LLM call) rather than guessing.
# pip install semantic-router
from semantic_router import Route
from semantic_router.encoders import OpenAIEncoder
from semantic_router.routers import SemanticRouter
cancel_order = Route(
name="cancel_order",
utterances=[
"I want to cancel my order",
"please refund this purchase",
"stop my order, I changed my mind",
],
)
router = SemanticRouter(encoder=OpenAIEncoder(), routes=[cancel_order])
result = router("can you cancel this for me?")
print(result.name) # -> "cancel_order", despite no exact phrase match
| Router Type | Handles Paraphrasing? | Latency / Cost |
|---|---|---|
| Regex pattern router | No — only exact patterns you anticipated | Near-zero; no model call |
| Semantic (embedding) router | Yes — matches by meaning, not literal text | Low — one embedding lookup, far cheaper than an LLM call |
The two approaches aren't mutually exclusive — many production systems use regex for a handful of rigid, high-confidence patterns (a specific command prefix) and fall back to semantic routing for everything else. Treat regex as the fast path for known-fixed cases and semantic routing as the flexible net that catches genuine natural-language variety.
Practical Challenge
Build a semantic-router route with 3-4 utterances for one intent from your own project, then test it against 10 differently-phrased queries expressing that same intent and check how many are matched correctly.
Concept Check
Sources & Further Reading
- semantic-router (GitHub) — the open-source embedding-based routing library referenced throughout this article.
- Aurelio AI: Semantic Router — background and use cases for embedding-based decision layers.
AI