1. Router Layer

PreviousNext

Learn how the router detects intent and decides which sub-agents to call.

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

  1. A user sends a natural language query.
    Example: “Show me orders this week and top-selling products.”

  2. The router runs a small LLM call (or intent classifier).
    It maps the query to one or more domains — like Orders, Products, or Analytics.

  3. 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.”

StepWhat HappensLayer
1Router detects Orders and Inventory domainsRouter
2Both agents spin upSub-Agent
3Orders agent fetches total revenueData Layer
4Inventory agent checks stock levelsData Layer
5Results 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

router-intent.ts
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:

router-keyword.ts
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.