The Router Layer
The router is the brain of your multi-agent system.
It listens to the user, figures out what they mean, and decides which agent (or agents) should handle the task.
You can think of it as a traffic controller for AI — lightweight, fast, and domain-aware.
How It Works
-
A user sends a natural language query.
Example: “Show me orders this week and top-selling products.” -
The router runs a small LLM call (or intent classifier).
It maps the query to one or more domains — likeOrders,Products, orAnalytics. -
Depending on the result:
- If there’s one domain, it runs a single agent.
- If there are multiple, it launches agents in parallel.
Example
Let’s walk through an ecommerce example:
User: “Show me total revenue and inventory for this week.”
| Step | What Happens | Layer |
|---|---|---|
| 1 | Router detects Orders and Inventory domains | Router |
| 2 | Both agents spin up | Sub-Agent |
| 3 | Orders agent fetches total revenue | Data Layer |
| 4 | Inventory agent checks stock levels | Data Layer |
| 5 | Results stream back together (chart + table) | Output |
The router itself doesn’t know how to get the data.
It just knows who knows how.
Intent Detection
The router’s core job is intent detection.
You can implement it in several ways:
1. Using a Light LLM Call
const intent = await generateText({
model: "openai/gpt-4o-mini",
prompt: `
You are a router. Given a query, decide which domains apply.
Domains: Orders, Inventory, Products, Analytics.
Query: "Show me revenue and stock this week."
`,
})Output:
{
"domains": ["Orders", "Inventory"]
}2. Using Rules or Keywords
For simpler cases, you can skip the LLM entirely:
function detectDomains(query: string) {
const domains = []
if (query.includes("revenue") || query.includes("sales"))
domains.push("Orders")
if (query.includes("inventory") || query.includes("stock"))
domains.push("Inventory")
if (query.includes("products")) domains.push("Products")
return domains
}Multi-Intent Handling
When a query touches more than one domain (like “sales and stock”), the router can split the query into sub-tasks.
Each sub-agent then works independently, like teammates on parallel threads.
This is where routers shine — they turn messy natural language into structured, parallel execution.
Optional Plan Path
For more complex requests (“Compare last month’s orders to this month and summarize top categories”), the router can also return an execution plan.
Example output:
{
"domains": ["Orders", "Analytics"],
"plan": [
"Fetch last month's orders",
"Fetch this month's orders",
"Compare totals and summarize by category"
]
}This lets you run multi-step reasoning with clear boundaries.
Key Design Idea
Keep your router simple and deterministic.
- It should only decide who acts, never how to act.
- It’s the conductor — not the orchestra.
That simplicity makes it fast, predictable, and easy to extend.
ai sdk patterns.