Model Routing
PilotDeck Agent Series
Router is PilotDeck’s model routing engine. It automatically selects the most appropriate model based on task complexity and tries an alternate model when a request fails, reducing unnecessary model overhead.
1. Core Competencies
| Ability | function |
|---|---|
| TokenSaver smart downshift | Analyze the complexity of user input, automatically select matching model levels, and use cheap models for simple tasks |
| Fallback fault tolerance chain | When a rollback error occurs in a request, alternate models are tried in sequence |
| Scenario scene routing | Apply different routing policies based on Agent type (main Agent / sub-Agent) |
| Custom Router extension | Support custom routing plug-ins to implement your own routing logic |
2. Routing Decision Process
- Custom routing: Optional plugin provides model overrides.
- scene recognition: Recognize explicit model, default calls, and subagent calls.
- TokenSaver Determination: When it is not explicitly selected or covered by a custom model, reuse the existing gear according to the enable condition, or call the judgment model.
- Execute decisions: Determine the model, gear and scene, and then execute the request; automatic arrangement can be enabled when the conditions are met.
When the Router is closed, the model selected by the request is inherited.
3. TokenSaver Intelligent Downshifting
TokenSaver uses the judgment model (Judge) to analyze the task, and then selects the model corresponding to the Tier (Tier). The savings come primarily from model price differences.
3.1 Working Principle
- Judge: Use a lightweight model to analyze the complexity of user requests based on recent user messages.
- gear matching: Select the corresponding model gear according to the judgment result.
- Continuous call reuse: Saved models and gears can be reused when the same task is executed continuously to reduce repeated judgments.
When there is a previous cache hit record, the Router will also compare the estimated input cost of retaining the current model (cache read vs. uncached input) and the cost of switching models to reprocess the context to avoid downshifting but increasing the input cost.
3.2 Gear Position Example
| gear | Applicable tasks |
|---|---|
simple | Simple Q&A, confirmation, lightweight file operations |
medium | Short article generation, regular code generation, and reading and writing a small amount of files |
reasoning | Single-agent multi-file analysis, data analysis, multi-step workflow and research |
complex | Requires sub-agent division of labor, professional collaboration or parallel advancement |
Gears can be customized, and multiple gears can share models.complex The key is the need for division of labor, and in-depth analysis of single agents is usually classified into reasoning。
3.3 Configure TokenSaver
The following fragments need to be merged into the existing configuration; the sample model must first be defined in the model pool.
router:
tokenSaver:
enabled: true
judge: openai/gpt-4.1-mini
defaultTier: medium
subagent:
policy: judge
tiers:
simple:
model: openai/gpt-4.1-mini
medium:
model: openai/gpt-4.1-mini
reasoning:
model: openai/gpt-4.1
complex:
model: openai/gpt-4.1
4. Fallback Fault-Tolerant Chain
When the execution model encounters a fallback error, the Router tries other models according to the backup chain. Alternate models may also fail, eventually with an error returned to the caller.
4.1 Error Types That Can Trigger Fallback
- Retryable timeouts, throttling, server or network errors.
- Errors such as quota exhaustion, model non-existence, authentication errors, etc. allow recovery by changing the model.
- Some tool parameters are in the wrong format.
Problems that can be recovered through compression, such as context being too long, will be handled by the corresponding recovery mechanism first, and the model will not be switched directly according to ordinary errors.
4.2 Configure Fallback
router:
fallback:
default:
- openai/gpt-4.1-mini
- deepseek/deepseek-chat
The alternative uses the configured provider/model String. By default, up to 5 alternate models are tried, available maxFallbacks Tweak; alternative chains are not enabled when explicitly specifying a model.
4.3 Retry Strategy
Router supports transient error retry and empty response retry:
- Instantaneous retry: For errors that meet the conditions, delay retry on the current model. The current YAML parser is not reading
router.transientRetry, so it is not available as a YAML configuration item. - Retry with empty response: Retry results that meet the null response criteria. You can adjust the number of attempts through the following fields. The default value is 2.
router:
zeroUsageRetry:
enabled: true
maxAttempts: 2
5. Scenario Scene Routing
| scene | Description |
|---|---|
default | Use the default scene model; agent.model |
explicit | Explicitly specify the model |
subagent | Sub-Agent call, independent routing strategy can be applied |
Subagent strategy support judge and skip:judge Entering judgment, the gear can be reused during continuous execution;skip Skip judgment. The default subagent model is given by agent.subagents.default Configure, omit or set to inherit When inheriting the default model of the main agent.
6. Auto-Orchestrate
When the main agent hits the trigger gear, automatic orchestration can inject orchestration prompt words and adjust the tool list, allowing the main agent to plan and delegate, and the sub-agents to execute. The default trigger gear is complex。
router:
autoOrchestrate:
enabled: true
triggerTiers: [complex]
7. Statistics Panel
After statistics are turned on, the Router records the model, gear, scenario, input and output token, cache read and time of the execution request. Costs are prioritized using available service providers to return costs, otherwise they are estimated based on model prices.
Savings are the “cost of the same recorded usage estimated at the baseline model price − execution fee”. The baseline prefers using the default scene model.
8. Event Bus
Router sends events at key nodes for easy monitoring and debugging:
| event | Description |
|---|---|
pilotdeck_router_decision | Routing decision completed |
pilotdeck_router_fallback | Switch alternate model |
pilotdeck_router_token_saver_failed | Decision failed |
pilotdeck_router_zero_usage_retry | Retry with empty response |
pilotdeck_router_transient_retry | Transient error retry |
pilotdeck_router_execute_failed | Final execution failed |
pilotdeck_router_custom_failed | Custom routing failed |
For complete fields and configuration examples, see YAML configuration file, see the operating steps Router User Guide。