Skip to main content
PraisonAI TypeScript provides a Router Agent for directing requests to specialized agents.

Installation

npm install praisonai

Basic Router

import { RouterAgent, createRouter, routeConditions, EnhancedAgent } from 'praisonai';

const mathAgent = new EnhancedAgent({
  instructions: 'You are a math expert.'
});

const codeAgent = new EnhancedAgent({
  instructions: 'You are a coding expert.'
});

const router = createRouter({
  routes: [
    {
      agent: mathAgent,
      condition: routeConditions.keywords(['math', 'calculate', 'equation'])
    },
    {
      agent: codeAgent,
      condition: routeConditions.keywords(['code', 'program', 'function'])
    }
  ],
  defaultAgent: mathAgent
});

const result = await router.route('Calculate 2+2');
console.log('Routed to:', result?.agent.name);
console.log('Response:', result?.response);

Route Conditions

import { routeConditions } from 'praisonai';

// Match by keywords
routeConditions.keywords(['math', 'calculate'])

// Match by regex
routeConditions.pattern(/\d+\s*[\+\-\*\/]\s*\d+/)

// Match by metadata
routeConditions.metadata('category', 'technical')

// Always match (for default)
routeConditions.always()

// Combine with AND
routeConditions.and(
  routeConditions.keywords(['code']),
  routeConditions.metadata('language', 'typescript')
)

// Combine with OR
routeConditions.or(
  routeConditions.keywords(['python']),
  routeConditions.keywords(['javascript'])
)

Priority Routing

const router = createRouter({
  routes: [
    {
      agent: urgentAgent,
      condition: routeConditions.keywords(['urgent', 'emergency']),
      priority: 100  // Higher priority
    },
    {
      agent: generalAgent,
      condition: routeConditions.always(),
      priority: 0
    }
  ]
});

Adding Routes Dynamically

const router = createRouter({ routes: [] });

router.addRoute({
  agent: newAgent,
  condition: routeConditions.keywords(['new-topic']),
  priority: 50
});

Context-Aware Routing

const result = await router.route('Help me', {
  history: ['previous message'],
  metadata: { userId: '123', premium: true }
});

Verbose Mode

const router = createRouter({
  routes: [...],
  verbose: true  // Logs routing decisions
});