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.
1# Clone the repository
2git clone https://github.com/SalesforceAIResearch/xRouter.git
3cd xRouter
4
5# Set up environment
6conda create -n xrouter python=3.12
7conda activate xrouter
8
9pip install uv
10uv pip install torch==2.6.0
11uv pip install flash-attn==2.7.3 --no-build-isolation
12uv pip install -e .[gpu,math,vllm,test]
13pip install litellm rich python-dotenv
1export OPENAI_API_KEY="your_openai_key"
2export TOGETHER_API_KEY="your_together_key"
3export GEMINI_API_KEY="your_gemini_key" # optional
1# Host the router model
2cd evaluation
3bash host_router.sh # Serves on port 8000
4
5# Launch the router API (in another terminal)
6bash serve_router.sh # Serves on port 8800
1import openai
2
3# Initialize client
4client = openai.OpenAI(
5 base_url="http://localhost:8800/v1",
6 api_key="dummy"
7)
8
9# Send request
10response = client.chat.completions.create(
11 model="router-tool-rl",
12 messages=[
13 {"role": "user", "content": "Solve: If x^2 + 2x + 1 = 0, what are the values of x?"}
14 ],
15 max_tokens=1000
16)
17
18print(response.choices[0].message.content)
19
20# Access routing metadata
21metadata = response.router_metadata
22print(f"Model used: {metadata['model_used']}")
23print(f"Total cost: ${metadata['total_cost']:.6f}")
1@article{qian2025xrouter,
2 title={xRouter: Training Cost-Aware LLMs Orchestration System via Reinforcement Learning},
3 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},
4 journal={arXiv preprint arXiv:2510.08439},
5 year={2025}
6}