Routing

Direct requests to specialized agents

Agents can route user requests to the most appropriate specialist automatically.

Diagram

User

Router

Specialist Agent

Quick Start

  1. 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
  2. With Configuration
    typescript
    const router = new Router({
      routes: [
        { agent: salesAgent, keywords: ['buy', 'price', 'purchase'] },
        { agent: techAgent, keywords: ['error', 'bug', 'help'] }
      ]
    });

User Interaction Flow

Diagram
AgentRouterUserAgentRouterUser“I want to buy”Match keywordsRoute to Sales“Happy to help…”

Configuration Levels

typescript
// 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

OptionDescription
agentsList of agents to route to
routesRoute definitions with conditions
defaultAgentFallback when no match
priorityRoute priority (lower = first)

API Reference

Conditions Module

Routing 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.

Diagram

Which routing option?

Linear next step

Yes / no branch

Multiple outcomes

nextTasks

when + thenTask / elseTask

taskType: ‘decision’ + routing

Follow a fixed path

isStart picks the entry point; nextTasks picks what runs after.

typescript
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 → three

Branch on a condition

when gates thenTask and elseTask. {{previous_output}} holds the last result.

typescript
// 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.

typescript
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.


Related