Intelligent LLM selection based on task requirements and model capabilities for optimal performance and cost efficiency.
from praisonaiagents import Agentagent = Agent(name="router-demo", instructions="Pick the best model for each task.")agent.start("Summarise this long document cheaply")
The user submits a task; the router picks the model that best matches requirements and cost.
export OPENAI_API_KEY="${OPENAI_API_KEY:?Set OPENAI_API_KEY in your shell}"export ANTHROPIC_API_KEY=your_anthropic_key_hereexport GOOGLE_API_KEY=your_google_key_here
3
Create a file
Create a new file model_router_example.py:
from praisonaiagents import Agent, Task, AgentTeamfrom praisonaiagents.llm import ModelRouter# Initialize the model routerrouter = ModelRouter()# Analyze task complexity and select appropriate modeltask_description = "Calculate the sum of 15 + 27"complexity = router.analyze_task_complexity(task_description)selected_model = router.select_model(task_description)print(f"Task complexity: {complexity}")print(f"Selected model: {selected_model}")# Create an agent with the selected modelsmart_agent = Agent( name="Smart Assistant", role="Adaptive AI Assistant", goal="Complete tasks using the most appropriate model", instructions="Analyze task requirements and provide accurate results", llm=selected_model # Use the model selected by router)# Create a simple tasksimple_task = Task( name="simple_calculation", description=task_description, expected_output="The numerical result", agent=smart_agent)# Create and run workflowworkflow = AgentTeam( agents=[smart_agent], tasks=[simple_task])print("Starting Model Router Workflow...")print("=" * 50)results = workflow.start()print("Done!")
You can configure custom models directly in your agents.yaml or workflow.yaml files. This allows you to define model profiles with costs, capabilities, and complexity levels without writing Python code.
For simple per-agent model assignment without configuring the full Model Router system, use these direct forms:
roles: researcher: role: Researcher goal: Research {topic} llm: gpt-4o-mini # string form writer: role: Writer goal: Write about {topic} llm: model: groq/llama3-70b-8192 # dict form
Precedence: per-agent llm > manager_llm (for hierarchical process) > global model resolved from llm_config[0].model / CLI / env.Framework support: Per-agent llm is honored by both crewai and the praisonai-native adapter.
AutoGen/AG2 limitation: This adapter currently uses only llm_config[0] — per-agent LLM assignment is not yet supported.
Deep dive into model-specific capabilities and features
Router Agent
Learn about the RouterAgent for dynamic task routing
The Model Router System continuously learns from usage patterns to improve selection accuracy over time. Regular monitoring and adjustment of routing rules ensures optimal performance.