xRouter: Training Cost-Aware LLMs Orchestration System via Reinforcement Learning
16
8 commits
6 linked in READMEs
updated Nov 4, 2025
Welcome to xRouter, Salesforce AI Research's intelligent LLM routing system trained with reinforcement learning to dynamically select optimal models from 20+ available LLMs while optimizing for both performance and cost.
Modern LLM deployments face a widening cost-performance spectrum: premium models deliver strong reasoning but are expensive, while lightweight models are economical yet brittle on complex tasks. xRouter learns end-to-end routing policies that balance quality and cost through explicit cost-aware reward shaping, eliminating the need for hand-engineered routing rules.
For detailed results, see our paper.
# Clone the repository
git clone https://github.com/SalesforceAIResearch/xRouter.git
cd xRouter
# Set up environment
conda create -n xrouter python=3.12
conda activate xrouter
pip install uv
uv pip install torch==2.6.0
uv pip install flash-attn==2.7.3 --no-build-isolation
uv pip install -e .[gpu,math,vllm,test]
pip install litellm rich python-dotenv
export OPENAI_API_KEY="your_openai_key"
export TOGETHER_API_KEY="your_together_key"
export GEMINI_API_KEY="your_gemini_key" # optional
# Host the router model
cd evaluation
bash host_router.sh # Serves on port 8000
# Launch the router API (in another terminal)
bash serve_router.sh # Serves on port 8800
import openai
# Initialize client
client = openai.OpenAI(
base_url="http://localhost:8800/v1",
api_key="dummy"
)
# Send request
response = client.chat.completions.create(
model="router-tool-rl",
messages=[
{"role": "user", "content": "Solve: If x^2 + 2x + 1 = 0, what are the values of x?"}
],
max_tokens=1000
)
print(response.choices[0].message.content)
# Access routing metadata
metadata = response.router_metadata
print(f"Model used: {metadata['model_used']}")
print(f"Total cost: ${metadata['total_cost']:.6f}")
xRouter uses DAPO (Distributional Advantage Policy Optimization) with cost-aware reward shaping:
reward = quality - λ × normalized_cost
Training Features:
Supported Model Tiers:
| Tier | Models | Best For |
|---|---|---|
| Premium | GPT-5, GPT-4.1, o3, Qwen3-235B-Instruct, Kimi K2 | Mission-critical tasks |
| Standard | GPT-5-Mini, GPT-4.1-Mini, o4-Mini, GPT-OSS-120B | Balanced performance |
| Budget | GPT-5-Nano, GPT-4.1-Nano, GPT-4o-Mini, GPT-OSS-20B | High-volume tasks |
| Specialized | o3, DeepSeek-R1, Qwen3-235B-Thinking, Qwen3-Coder-480B | Domain-specific |
@article{qian2025xrouter,
title={xRouter: Training Cost-Aware LLMs Orchestration System via Reinforcement Learning},
author={Qian, Cheng and Liu, Zuxin and Kokane, Shirley and Prabhakar, Akshara and Qiu, Jielin and Chen, Haolin and Liu, Zhiwei and Ji, Heng and Yao, Weiran and Heinecke, Shelby and Savarese, Silvio and Xiong, Caiming and Wang, Huan},
journal={arXiv preprint arXiv:2510.08439},
year={2025}
}
This project builds upon exceptional work from the open-source community:
🏢 Developed by Salesforce AI Research
xRouter: Training Cost-Aware LLMs Orchestration System via Reinforcement Learning
16
8 commits
6 linked in READMEs
updated Nov 4, 2025
Welcome to xRouter, Salesforce AI Research's intelligent LLM routing system trained with reinforcement learning to dynamically select optimal models from 20+ available LLMs while optimizing for both performance and cost.
Modern LLM deployments face a widening cost-performance spectrum: premium models deliver strong reasoning but are expensive, while lightweight models are economical yet brittle on complex tasks. xRouter learns end-to-end routing policies that balance quality and cost through explicit cost-aware reward shaping, eliminating the need for hand-engineered routing rules.
For detailed results, see our paper.
# Clone the repository
git clone https://github.com/SalesforceAIResearch/xRouter.git
cd xRouter
# Set up environment
conda create -n xrouter python=3.12
conda activate xrouter
pip install uv
uv pip install torch==2.6.0
uv pip install flash-attn==2.7.3 --no-build-isolation
uv pip install -e .[gpu,math,vllm,test]
pip install litellm rich python-dotenv
export OPENAI_API_KEY="your_openai_key"
export TOGETHER_API_KEY="your_together_key"
export GEMINI_API_KEY="your_gemini_key" # optional
# Host the router model
cd evaluation
bash host_router.sh # Serves on port 8000
# Launch the router API (in another terminal)
bash serve_router.sh # Serves on port 8800
import openai
# Initialize client
client = openai.OpenAI(
base_url="http://localhost:8800/v1",
api_key="dummy"
)
# Send request
response = client.chat.completions.create(
model="router-tool-rl",
messages=[
{"role": "user", "content": "Solve: If x^2 + 2x + 1 = 0, what are the values of x?"}
],
max_tokens=1000
)
print(response.choices[0].message.content)
# Access routing metadata
metadata = response.router_metadata
print(f"Model used: {metadata['model_used']}")
print(f"Total cost: ${metadata['total_cost']:.6f}")
xRouter uses DAPO (Distributional Advantage Policy Optimization) with cost-aware reward shaping:
reward = quality - λ × normalized_cost
Training Features:
Supported Model Tiers:
| Tier | Models | Best For |
|---|---|---|
| Premium | GPT-5, GPT-4.1, o3, Qwen3-235B-Instruct, Kimi K2 | Mission-critical tasks |
| Standard | GPT-5-Mini, GPT-4.1-Mini, o4-Mini, GPT-OSS-120B | Balanced performance |
| Budget | GPT-5-Nano, GPT-4.1-Nano, GPT-4o-Mini, GPT-OSS-20B | High-volume tasks |
| Specialized | o3, DeepSeek-R1, Qwen3-235B-Thinking, Qwen3-Coder-480B | Domain-specific |
@article{qian2025xrouter,
title={xRouter: Training Cost-Aware LLMs Orchestration System via Reinforcement Learning},
author={Qian, Cheng and Liu, Zuxin and Kokane, Shirley and Prabhakar, Akshara and Qiu, Jielin and Chen, Haolin and Liu, Zhiwei and Ji, Heng and Yao, Weiran and Heinecke, Shelby and Savarese, Silvio and Xiong, Caiming and Wang, Huan},
journal={arXiv preprint arXiv:2510.08439},
year={2025}
}
This project builds upon exceptional work from the open-source community:
🏢 Developed by Salesforce AI Research