Routing
Direct requests to specialized agents
Agents can route user requests to the most appropriate specialist automatically.
Quick Start
- Simple Usage
typescript import { Agent, Router } from 'praisonai'; const salesAgent = new Agent({ instructions: 'You handle sales inquiries' }); const techAgent = new Agent({ instructions: 'You provide tech support' }); const router = new Router({ agents: [salesAgent, techAgent], defaultAgent: techAgent }); // Automatically routes to the right agent const response = await router.route("I'd like to buy your product"); // → Sales agent responds - With Configuration
typescript const router = new Router({ routes: [ { agent: salesAgent, keywords: ['buy', 'price', 'purchase'] }, { agent: techAgent, keywords: ['error', 'bug', 'help'] } ] });
User Interaction Flow
Configuration Levels
// Level 1: Array - Simple agent list
const router = new Router({
agents: [salesAgent, techAgent]
});
// Level 2: Dict - With keywords
const router = new Router({
routes: [
{ agent: salesAgent, keywords: ['buy', 'price'] }
]
});
// Level 3: Instance - Full control
const router = new Router({
routes: [
{
agent: salesAgent,
condition: (input) => input.includes('purchase'),
priority: 1
}
],
defaultAgent: generalAgent
});Routing Options
| Option | Description |
|---|---|
agents | List of agents to route to |
routes | Route definitions with conditions |
defaultAgent | Fallback when no match |
priority | Route priority (lower = first) |
API Reference
Conditions ModuleRouting conditions module
Task-Level Routing
A team of tasks routes itself: set isStart and nextTasks (or a decision table) and the graph, not the list order, decides what runs.
process: 'workflow' turns on the graph. Without it, a team walks the task list in order.
Follow a fixed path
isStart picks the entry point; nextTasks picks what runs after.
import { Agent, Team } from 'praisonai';
const team = new Team({
agents: [new Agent({ instructions: 'help' })],
tasks: [
{ name: 'one', description: 'step one', isStart: true, nextTasks: ['three'] },
{ name: 'two', description: 'step two' }, // never runs — the graph skips it
{ name: 'three', description: 'step three' }
],
process: 'workflow'
});
await team.start();
// Runs: one → threeBranch on a condition
when gates thenTask and elseTask. {{previous_output}} holds the last result.
// a task object inside team.tasks
{
name: 'triage',
description: 'triage the ticket',
isStart: true,
when: '{{previous_output}} contains urgent',
thenTask: 'escalate',
elseTask: 'archive'
}Route on a decision
A taskType: 'decision' task reads a decision field from its answer and follows the routing table. The target 'exit' ends the run.
const team = new Team({
agents: [reviewer],
tasks: [
{
name: 'gate',
description: 'approve or reject',
isStart: true,
taskType: 'decision',
routing: { approve: ['ship'], reject: ['exit'] }
},
{ name: 'ship', description: 'ship it' }
],
process: 'workflow'
});
await team.start();Best Practices
Set a default agent
Always have a fallback for unmatched requests.
Use clear keywords
Distinct keywords for each route prevent confusion.
Keep routes simple
Start with keyword matching, add complexity only if needed.
Pick the smallest routing tool
nextTasks for a fixed path, when for a yes/no branch, a decision table for many outcomes.