Skip to main content
The user submits a task; the router picks the model that best matches requirements and cost.

Quick Start

1

Install Package

First, install the PraisonAI Agents package:
2

Set API Keys

Set your API keys as environment variables:
3

Create a file

Create a new file model_router_example.py:
4

Run the Example

Execute your model router example:
Requirements
  • Python 3.10 or higher
  • API keys for the models you want to use
  • Basic understanding of different LLM capabilities

Which Strategy Should I Use?

Pick a routing_strategy based on whether you optimise for cost, quality, or let the router decide.

YAML-Based Model Configuration

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.

Basic Configuration

Add a models section to your YAML file:

Quick per-agent model (no router needed)

For simple per-agent model assignment without configuring the full Model Router system, use these direct forms:
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.

Model Configuration Fields

Per-Agent LLM Configuration (Named Models)

Each agent can specify its own LLM configuration using named models defined in the models: block above:

Routing Strategies

Configure routing strategy in the workflow section:

Complete Example

CLI Usage

Create a workflow from template:
Run with the --router flag for automatic model selection:

How It Works

The user submits a task; the router analyses complexity, selects the best-matching model, and the agent runs on it.

Understanding Model Router

What is Model Router?

The Model Router:
  • Automatically selects the best LLM for each task
  • Considers task complexity, context length, and requirements
  • Optimizes for performance and cost
  • Supports fallback options if primary model fails
  • Provides detailed routing history and analytics

Features

Intelligent Selection

Analyzes task requirements to choose the optimal model.

Cost Optimization

Balances performance needs with cost considerations.

Capability Matching

Matches task requirements with model capabilities.

Fallback Support

Automatically switches to backup models if needed.

Configuration Options

Advanced Usage

Custom Routing Logic

Routing Analytics

Model Selection Criteria

The router considers multiple factors when selecting models:

Task Complexity

  • Simple calculations → Cost-effective models
  • Complex reasoning → Advanced models
  • Creative tasks → Specialized creative models

Context Length

  • Short context → Standard models
  • Medium context → Enhanced context models
  • Long context → Specialized long-context models

Response Time

  • Real-time needs → Fast models
  • Batch processing → Optimized for throughput
  • Quality priority → Best performing models

Cost Constraints

  • Budget limits → Cost-effective options
  • Quality/cost balance → Optimal value models
  • Premium requirements → Top-tier models

Best Practices

Provide specific task requirements to help the router make better decisions:
Regularly review routing decisions and performance:
Configure budget constraints to control costs:

Troubleshooting

Routing Issues

If wrong models are selected:
  • Review task requirements
  • Check routing rules configuration
  • Enable verbose logging
  • Verify model availability

Performance Problems

If performance is suboptimal:
  • Analyze routing history
  • Adjust selection criteria
  • Update model capabilities
  • Consider custom routing logic

Next Steps

Model Capabilities

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.

Inspect the per-model capabilities the router uses to decide.

Retry with a backup model when the routed choice fails.