An autonomous AI gateway router that intelligently routes incoming API requests to the most appropriate backend model. Built with LoRA fine-tuning on Qwen2.5-0.5B-Instruct + a classification head, achieving 100% routing accuracy with 1.44ms average latency .
Input: "Analyze this research paper..."
│
▼
┌─────────────────────────────────────────┐
│ Qwen2.5-0.5B-Instruct (LoRA-adapted) │
│ Target modules: q/k/v/o/gate/up/down │
│ LoRA rank: 64, alpha: 64 │
│ Output: Last token hidden state [896] │
└─────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────┐
│ Classification Head │
│ Dropout(0.1) → Linear(896 → 6) │
└─────────────────────────────────────────┘
│
▼
Output: "gpt-4-turbo" (probability: 0.92)
1 import torch
2 from transformers import AutoTokenizer , AutoModelForCausalLM
3 from peft import PeftModel
4 import json
5
6 # Load model
7 base_model = AutoModelForCausalLM . from_pretrained ( "unsloth/Qwen2.5-0.5B-Instruct" )
8 model = PeftModel . from_pretrained ( base_model , "dknguyen2304/model-router" )
9 tokenizer = AutoTokenizer . from_pretrained ( "unsloth/Qwen2.5-0.5B-Instruct" )
10
11 # Load classifier head
12 classifier = torch . nn . Sequential (
13 torch . nn . Dropout ( 0.1 ) ,
14 torch . nn . Linear ( 896 , 6 )
15 )
16 classifier . load_state_dict ( torch . load ( "classifier.pt" , map_location = "cpu" ) )
17
18 # Label mapping
19 labels = [ "gpt-4-turbo" , "gpt-3.5-turbo" , "claude-3-opus" ,
20 "claude-3-sonnet" , "gemini-pro" , "mixtral-8x7b" ]
21
22 # Inference
23 prompt = "Write a complex recursive algorithm to solve the Tower of Hanoi"
24 inputs = tokenizer ( prompt , return_tensors = "pt" , max_length = 512 , truncation = True )
25
26 with torch . no_grad ( ) :
27 outputs = model ( ** inputs , output_hidden_states = True )
28 hidden = outputs . hidden_states [ - 1 ] [ : , - 1 , : ] # last token
29 logits = classifier ( hidden )
30 prediction = labels [ logits . argmax ( dim = - 1 ) . item ( ) ]
31
32 print ( f"Route to: { prediction } " )
├── adapter_model.safetensors # LoRA adapter weights
├── adapter_config.json # PEFT/LoRA configuration
├── classifier.pt # Classification head weights
├── router_config.json # Router configuration
├── label_mapping.json # Label ↔ ID mappings
└── config/
├── training_config.yaml # Training hyperparameters
└── deepspeed_config.json # DeepSpeed config
1 @misc{model-router-2026,
2 title={Model Router: Intelligent AI Gateway Request Routing via LoRA Fine-tuning},
3 author={dknguyen2304},
4 year={2026},
5 url={https://huggingface.co/dknguyen2304/model-router}
6 }