Learn how to create AI agents that can dynamically route tasks to specialized LLM instances.
A low-latency workflow where inputs are dynamically routed to the most appropriate LLM instance or configuration, optimizing efficiency and specialization.
The RouterAgent must be imported from praisonaiagents.agent.router_agent, not directly from praisonaiagents.
Copy
from praisonaiagents.agent.router_agent import RouterAgentfrom praisonaiagents.llm.model_router import ModelRouterfrom praisonaiagents import Task, PraisonAIAgents# Option 1: Simple RouterAgent with model listrouter_agent = RouterAgent( name="Smart Router", role="Task Router", # Use 'role' not 'description' goal="Route tasks to optimal models", backstory="I analyze tasks and select the most appropriate model", models=["gpt-4o-mini", "gpt-4o", "claude-3-5-sonnet-20241022"], routing_strategy="cost-optimized", # "auto", "manual", "cost-optimized", "performance-optimized" verbose=True)# Execute tasks (not chat)result = router_agent.execute("What is 2+2?")print(f"Result: {result}")# For complex tasksresult = router_agent.execute("Write a comprehensive business plan for a SaaS startup")print(f"Result: {result}")# Get usage report to see which models were usedusage_report = router_agent.get_usage_report()print(f"Usage report: {usage_report}")
The RouterAgent is designed to work within the PraisonAI framework:
Copy
from praisonaiagents.agent.router_agent import RouterAgentfrom praisonaiagents import Task, PraisonAIAgents# Create router agentrouter_agent = RouterAgent( name="Adaptive Assistant", role="Multi-Model Assistant", goal="Complete tasks using the most appropriate model", backstory="I intelligently route tasks to different models based on complexity", models=["gpt-4o-mini", "gpt-4o", "claude-3-5-sonnet-20241022"], routing_strategy="auto", verbose=True)# Create tasks for the router agentsimple_task = Task( description="What is the capital of France?", expected_output="A simple answer", agent=router_agent)complex_task = Task( description="Analyze the economic impact of renewable energy adoption in developing countries", expected_output="A comprehensive analysis", agent=router_agent)# Run with PraisonAIAgentsagents = PraisonAIAgents( agents=[router_agent], tasks=[simple_task, complex_task], process="sequential")results = agents.start()# View resultsfor task_id, result in results['task_results'].items(): print(f"Task: {result.description[:50]}...") print(f"Result: {result.raw[:100]}...") print("-" * 50)
from praisonaiagents.agent.router_agent import RouterAgentfrom praisonaiagents import Task, PraisonAIAgents# Example from examples/agents/router-agent-cost-optimization.pyrouter = RouterAgent( name="Cost-Optimized Assistant", role="Efficient Task Processor", goal="Complete tasks with optimal cost-performance balance", models=[ "gpt-4o-mini", # Cheap, fast for simple tasks "gpt-4o", # Balanced for medium complexity "gpt-4", # Powerful for complex tasks "claude-3-5-sonnet-20241022" # Alternative provider ], routing_strategy="cost-optimized", primary_model="gpt-4o-mini", fallback_model="gpt-4o")# The router will automatically:# - Use gpt-4o-mini for simple queries# - Upgrade to gpt-4o or gpt-4 for complex tasks# - Fall back to gpt-4o if the primary model fails