Semantic Request Routing Between Models
Summary
Not every request requires the most powerful model. A semantic router directs simple questions to a cheap model, complex ones to an expensive model, and specialized ones to dedicated handlers. Here’s how it works.
Why Route Requests
Using a single powerful model for everything is expensive and slow. But most requests are simple: "where are my orders," "what is a token." The idea is to route requests by meaning:
flowchart LR
A[Request] --> B[Router]
B -->|simple| C["Cheap Model"]
B -->|complex| D["Powerful Model"]
B -->|support| E["Specialized Handler"]
How the Router Works
The classic approach is comparison by embeddings. For each route, a set of examples is defined, the request is compared to them, and the closest match is chosen.
import numpy as np
def route(query_embedding, routes):
best_route = None
best_score = -1
for name, examples in routes.items():
score = max(np.dot(query_embedding, e) for e in examples)
if score > best_score:
best_score = score
best_route = name
return best_route
routes is a dictionary where each route corresponds to a list of example embeddings.
How to Build Routes
- Collect real requests — from logs or support.
- Break into groups — by task type.
- Define examples — 5–10 per route.
- Check thresholds — for low similarity, route to a general model.
Route Examples
| Route | Example Requests | Where to Route |
|---|---|---|
| Support | "where is my order," "refund money" | Specialized bot |
| Simple Facts | "what is a token" | Cheap model |
| Code Analysis | "find bug in function" | Powerful model |
| General | everything else | Basic model |
Confidence Threshold
The router should be able to "not choose." If no route reaches a sufficient similarity score, the request goes to the general route — this is safer than confidently sending it to the wrong place.
Considerations
- cache example embeddings — don’t recalculate with each request;
- log router decisions — to see where requests are going;
- periodically review routes — user requests change.
Conclusion
Semantic routing is a simple way to reduce costs and improve quality: the right request goes to the right handler. Start with two routes and gradually add more.