Skip to main content

RouterAgent

RouterAgent routes requests to the most appropriate specialized agent.

Quick Start

import { RouterAgent, createRouter } from 'praisonai';

const router = createRouter({
  routes: [
    { agent: codeAgent, condition: (input) => input.includes('code') },
    { agent: mathAgent, condition: (input) => input.includes('calculate') },
    { agent: generalAgent, condition: () => true }
  ]
});

const result = await router.route('Help me debug this code');

Configuration

interface RouterConfig {
  name?: string;
  routes: RouteConfig[];
  defaultAgent?: EnhancedAgent;
  verbose?: boolean;
}

interface RouteConfig {
  agent: EnhancedAgent;
  condition: (input: string, context?: RouteContext) => boolean | Promise<boolean>;
  priority?: number;
}

Example

import { RouterAgent, Agent } from 'praisonai';

// Create specialized agents
const codeAgent = new Agent({
  name: 'CodeExpert',
  instructions: 'You are a coding expert.'
});

const mathAgent = new Agent({
  name: 'MathExpert',
  instructions: 'You are a math expert.'
});

const generalAgent = new Agent({
  name: 'GeneralAssistant',
  instructions: 'You are a helpful assistant.'
});

// Create router
const router = new RouterAgent({
  routes: [
    { 
      agent: codeAgent, 
      condition: (input) => /code|debug|function|class/i.test(input),
      priority: 2
    },
    { 
      agent: mathAgent, 
      condition: (input) => /calculate|math|equation/i.test(input),
      priority: 2
    },
    { 
      agent: generalAgent, 
      condition: () => true,
      priority: 0
    }
  ]
});

// Route a request
const result = await router.route('Help me debug this function');
console.log(`Routed to: ${result.agent.name}`);
console.log(`Response: ${result.response}`);

CLI Usage

praisonai-ts router analyze "Help me debug this code"
praisonai-ts router analyze "What is 2+2?" --json
The CLI provides routing analysis without executing agents.