InferRoute is built upon robust theoretical frameworks for cost-performance trade-offs and multi-tier cascading inference.
Derived from the paper "FrugalGPT: How to Use Large Language Models While Reducing Cost and Improving Performance" (Chen et al., Stanford University, 2023), InferRoute implements three key cost-saving mechanics:
Prunes long prompt histories or few-shot example prefixes down to at most 1 context example when querying cheap local models (Ollama/vLLM), restoring full complexity only when cascading to commercial endpoints.
Uses standard Redis completion caches. Matches queries in under 10ms, avoiding upstream model fees completely for exact duplicate concurrent prompts.
Sequentially routes queries through a chain of backends (Ollama ➔ vLLM ➔ Gemini ➔ OpenAI). A Reliability Judge assesses output quality at each tier, escalating to the next tier if the quality score falls below \(\tau\).
Buffers SSE stream chunks server-side to detect infinite loops or gibberish outputs. Speculatively cancels degraded local streams and escalates to premium cloud nodes mid-stream to avoid client-facing disruptions.
The FrugalGPT framework models cost-performance optimization as a decision sequence under cost bounds. By arranging models in ascending order of cost and capabilities (e.g., \(M_1, M_2, \dots, M_k\)), the system routes the query sequentially. For each model \(M_j\), the response is validated by a specialized quality assessor (Reliability Judge). If the response quality satisfies the threshold (\(Q(M_j, x) \ge \tau\)), the generation stops, avoiding subsequent cloud execution fees. Otherwise, the request escalates to the next model tier, guaranteeing response quality while keeping average costs minimal.
Adjust the sliding acceptance threshold \(\tau\) and click Simulate to trace the sequential escalation path.
Based on the paper "RouterBench: A Benchmark for Multi-LLM Routing System" (Li et al., Martian, 2024), InferRoute structures content-aware models using standard cost-quality constraints.
The routing engine maximizes target utility for prompt \(x\) by selecting backend \(m\):
Here, \(\lambda\) is the cost-quality trade-off parameter (willingness-to-pay), \(\text{Quality}_{\text{pred}}\) is the predicted model quality score (0.0 to 1.0), and \(\text{Cost}(m)\) represents model API execution fees.
To evaluate a routing policy globally across budgets, we calculate the **AIQ (Area under the cost-quality curve)** using the Trapezoidal Rule:
InferRoute implements six distinct routing strategies matching the RouterBench & FrugalGPT frameworks:
The RouterBench framework models LLM selection as a multi-objective optimization problem. By defining the parameter \(\lambda\) (cost-quality trade-off coefficient), the scoring equation evaluates the economic utility of selecting a model \(m\) for a prompt \(x\). The parameter \(\lambda\) represents a user's willingness-to-pay: setting a higher \(\lambda\) prioritizes response quality, while a lower \(\lambda\) emphasizes cost savings. The Area under the cost-quality curve (AIQ) measures the cumulative routing performance across all budget constraints, serving as a unified metric for evaluating routing efficiency.
Read and preview the full research publications associated with this routing engine directly in your browser:
商业大模型单次调用费用昂贵,而开源/小尺寸模型(如 Llama、GPT-3.5)极其便宜但准确率参差不齐。本论文提出通过调度低成本模型并搭配判定机制,以在保留高准确率的同时大幅削减总费用。
级联模型 (Cascade Decision): 设定模型序列 \((M_1, M_2, \dots, M_k)\) 以及质量评估器 \(J: \text{Response} \to [0, 1]\)。
对于请求 \(x\),系统依次生成 \(y_i = M_i(x)\),若 \(J(y_i) \ge \tau\)(接受度阈值),则立刻终止级联返回,否则 escalation 到下一级。
相比直接调用 GPT-4,FrugalGPT 可降低高达 90% 的总账单,并指出小模型无法有效吸收冗长上下文,提示词裁剪至关重要。
级联选路运行在 main.py 的级联流中,裁剪在 prompt_adapter.py,评分判定运行在 validator.py。
大模型路由逐步多样化,但缺乏标准化的评估基准和数学框架来对比不同路由器在性价比上的优劣。
效用评分公式: \(S(m, x) = \lambda \cdot Q_{\text{pred}}(m, x) - C(m)\),其中 \(\lambda\) 代表用户的支付意愿系数,\(Q\) 代表模型的质量预测,\(C\) 代表计费成本。
AIQ 曲线下面积积分: \(\text{AIQ} = \int_{c_{\min}}^{c_{\max}} Q(c) \, dc\),衡量在各种预算曲线下的全局选路表现。
引入预测型 MLP 路由器相比静态概率分配(Zero Router)可提升整体 AIQ 达 15% 以上。Oracle 决策上限揭示了路由组合的潜能。
效用评分与路由在 router.py 中的 KNN/MLP 选路策略中运行,帕累托分析和 AIQ 计算在 plot_results.py。
解决企业在拥有高并发免费本地小模型群(Edge)与计费的云端强模型(Cloud)时,如何实现高可用混合选路,减少多级判定带来的 TTFT 耗时。
难度分类器 (Difficulty Estimator): \(D(x) = \text{Classifier}(x) \in \{0, 1\}\),直接判定请求难易度并直达目标模型,强调一击即中。
中等体量分类器能以 85% 以上精度区分复杂度。能够降低多达 40% 的平均网络往返延迟,节约超 60% 费用。
在 learned_router.py 中实现了提取 prompt 任务特质(数学、代码等)的特征估计和直达策略分流。
针对写作、创意、日常对话等缺乏唯一标准解的任务,探讨如何利用大模型竞技场(Chatbot Arena)产生的人类真实偏好对战数据训练二分类器。
偏好对战概率 (Bradley-Terry Extension): \(P(M_{\text{strong}} \succ M_{\text{cheap}} \mid x) = \sigma(f(x))\),通过交叉熵损失优化预测。概率大于阈值 \(\theta\) 时上报强模型,否则分流至便宜模型。
在 Arena 上能在维持 GPT-4 95% 满意度的同时,缩减 50% API 费用,并验证了轻量级分类网络的优越性。
概率选路决策与阈值判定借鉴了该设计(router.py),拟在后续工作中引入专门的偏好二分类预测器 `preference_router.py`。
指出当存在 3 个以上候选模型池时,传统的 MSE 回归训练机制会导致在高预算(大 \(\lambda\))时决策权坍缩,强制全选最昂贵模型。
决策感知排序损失 (Decision-Aware Ranking Loss):
\(\mathcal{L}_{\text{rank}} = -\sum_{i \ne j} \log \sigma \Big( \big(\text{Utility}(M_i, x) - \text{Utility}(M_j, x)\big) \cdot \mathbb{I}(M_i \succ M_j) \Big)\),强调学习两模型效用之差,维持边界决策概率。
EquiRouter 成功解决回归多分类塌陷问题,在同等质量下,高预算区间多降低 17% 开销。
网关对效用归一化进行了放塌陷微调。未来将在 `benchmarks/train_router.py` 中直接换用此排名损失函数进行优化。
大模型调用费用大多由生成字数决定。若不设防生成字数,大模型输出的冗长答复会极大地蚕食路由的成本红利。
联合寻优公式: \(\max_{m, L} \left[ \text{Quality}(m, x, L) - \lambda \cdot \text{Cost}(m, L) \right]\),其中 \(L\) 代表限制最大输出 token 字数。
常识问答和提取任务在缩短字数后质量维持原样,这为输出开销带来了 4-5 倍的缩减,显著加快了端到端流式接收。
在 prompt_adapter.py 中实现了动态提示词注入与长度自适应,根据模型档次调整 payload `max_tokens` 参数。
单次单轮分类分流遇到超复杂的多步推理或代码排错任务基本失灵。复杂任务需要多轮拆解、反复求证与多次升级路由。
RL 奖励机制: \(\mathcal{R} = \mathcal{R}_{\text{accuracy}}(y) + \mathcal{R}_{\text{format}}(\text{think\_blocks}) - \beta \cdot \text{Cost}_{\text{inference}}\),用强化学习调教 local 选路 agent。Agent 生成带有 `<think>` 思维链的逻辑步骤,拆分调度子请求并汇总。
训练过的 8B 代理学会了在思考链中调度预算,仅耗费 GPT-4 35% 的成本就达到了等同程度的数学解答水准。
对应了网关在 main.py 级联检验模块中配置的失败回退重试与条件流阻断。
解决路由领域实验设计混乱、微调偏好漂移等数据漂移(Data Drift)带来的评测不稳定性,亟需大规模科学对照评测基准。
构建了 400K 级多任务标准测试集,证实路由存在 Scaling Laws(缩放定律):决策模型并非越大越好,1B 以下的特征分类器往往性能/能耗性价比最高。
网关所采用的 Reproducible Evaluation Harness 脚本提供了基础实验测试设计方法与任务配置格式(workload.json)。
为大模型服务架构及硬件开销分摊在资源优化垂直领域的科学分类(Taxonomy)建立体系。
从路由特征空间(Embedding/Text/Agent)、选路时间节点(Pre-generation/In-generation/Post-generation)与基础设施成本(本地 GPU 折旧 vs 云 API 计费)对比了各种架构的吞吐量、响应延时等折中机制。
确定了 InferRoute 数据平面与控制平面分离、网关多指标 Prometheus 监控的设计方针。
Formal scientific citations for the core research papers referenced during the design and optimization of the InferRoute gateway:
Chen, L., Zaharia, M., & Zou, J. (2023). FrugalGPT: How to use large language models while reducing cost and improving performance. arXiv preprint arXiv:2305.05196.
@article{chen2023frugalgpt,
title={FrugalGPT: How to use large language models while reducing cost and improving performance},
author={Chen, Lingjiao and Zaharia, Matei and Zou, James},
journal={arXiv preprint arXiv:2305.05196},
year={2023}
}
Li, T., Martian Team, et al. (2024). RouterBench: A Benchmark for Multi-LLM Routing System. arXiv preprint arXiv:2403.11164.
@article{li2024routerbench,
title={RouterBench: A Benchmark for Multi-LLM Routing System},
author={Li, Teh-Hsien and others},
journal={arXiv preprint arXiv:2403.11164},
year={2024}
}
Ding, J., et al. (2024). Hybrid LLM: Cost-Efficient and Quality-Aware Query Routing. arXiv preprint arXiv:2404.14944.
@article{ding2024hybrid,
title={Hybrid LLM: Cost-Efficient and Quality-Aware Query Routing},
author={Ding, Jiayi and others},
journal={arXiv preprint arXiv:2404.14944},
year={2024}
}
Ong, I., Almahairi, A., Wu, V., Chiang, W. L., Wu, T., Gonzalez, J. E., Kadous, M. W., & Stoica, I. (2025). RouteLLM: Learning to Route LLMs with Preference Data. Proceedings of the Thirteenth International Conference on Learning Representations (ICLR).
@inproceedings{ong2025routellm,
title={RouteLLM: Learning to Route LLMs with Preference Data},
author={Ong, Isaac and Almahairi, Amjad and Wu, Vincent and Chiang, Wei-Lin and Wu, Tianhao and Gonzalez, Joseph E. and Kadous, M. Waleed and Stoica, Ion},
booktitle={The Thirteenth International Conference on Learning Representations},
year={2025}
}
Lai, G., & Ye, H. J. (2026). When Routing Collapses: On the Degenerate Convergence of LLM Routers. arXiv preprint arXiv:2602.03478.
@article{lai2026when,
title={When Routing Collapses: On the Degenerate Convergence of LLM Routers},
author={Lai, Guannan and Ye, Han-Jia},
journal={arXiv preprint arXiv:2602.03478},
year={2026}
}
Anonymous (2026). R2-Router: A New Paradigm for LLM Routing with Reasoning. arXiv preprint arXiv:2602.02823.
@article{r2router2026,
title={R2-Router: A New Paradigm for LLM Routing with Reasoning},
journal={arXiv preprint arXiv:2602.02823},
year={2026}
}
Anonymous (2025). Router-R1: Teaching LLMs Multi-Round Routing and Aggregation via Reinforcement Learning. arXiv preprint arXiv:2506.09033.
@article{routerr12025,
title={Router-R1: Teaching LLMs Multi-Round Routing and Aggregation via Reinforcement Learning},
journal={arXiv preprint arXiv:2506.09033},
year={2025}
}
Anonymous (2026). LLMRouterBench: A Massive Benchmark and Unified Framework for LLM Routing. arXiv preprint arXiv:2601.07206.
@article{llmrouterbench2026,
title={LLMRouterBench: A Massive Benchmark and Unified Framework for LLM Routing},
journal={arXiv preprint arXiv:2601.07206},
year={2026}
}
Anonymous (2025). Doing More with Less: A Survey on Routing Strategies for Resource Optimisation in Large Language Model-Based Systems. arXiv preprint arXiv:2502.00409.
@article{resource_routing_survey_2025,
title={Doing More with Less: A Survey on Routing Strategies for Resource Optimisation in Large Language Model-Based Systems},
journal={arXiv preprint arXiv:2502.00409},
year={2025}
}
The sequence details how the gateway interceptor resolves client requests, manages cache, allocates concurrency slots, and executes cascades:
Resolves client headers to tenant ID and asserts balance balance \(> \$0.00\). Applies a resilient fail-open policy if the database is unreachable.
Performs a Redis exact completion lookup. If missing, checks the Radix Trie prefix index to score warm KV-cache affinity on self-hosted model backends.
Queries concurrency limits to dynamically protect local GPU memory allocations, rejecting or cascading requests to cloud buffers if limits are breached.
Routes prompts to the chosen backend. For cascades, it buffers output stream tokens, runs heuristics, and transparently initiates speculative escalations upon validation failures.
Logs latency telemetry and final aggregated token costs asynchronously to PostgreSQL database ledgers, decrementing tenant credit limits.
InferRoute features an interactive client playground dashboard (served at the root / path) allowing developers to monitor and simulate gateway functions in real-time:
Displays financial metrics including cumulative API dollars saved, tokens processed, Redis cache hit rates, average Time-to-First-Token (TTFT), and system uptime in real-time.
Renders a live vertical step visualizer tracking individual requests. Watch prompts flow through Cache lookup ➔ Concurrency limit verification ➔ Primary model execution ➔ speculative loops cancellation ➔ Cascade trigger.
Simulates tenant wallet balances and limits. Allows manual top-up adjustments (e.g., refilling $10.00 trial credits) to inspect rate-limiting triggers and HTTP 402 payment requirements.
Allows manual injection of failures (latency spikes, HTTP 500 crashes, network dropouts) into specific backend nodes to observe gateway self-healing, automatic failovers, and circuit-breaker status changes in real-time.
Below are evaluation statistics sweeping mixture ratios (\(p\)), trade-off factors (\(\lambda\)), and cascade thresholds (\(\tau\)) over workload dataset prompts:
| Routing Strategy | Cost per Request ($ USD) | Avg Quality Score (0 - 1.0) | Avg Latency (ms) | SLO Compliance | Fallback Hops |
|---|---|---|---|---|---|
| Oracle Router Optimal | $0.000022 | 0.78 | 258ms | 100.0% | 0.0% |
| KNN Router (\(\lambda = 1.00\)) | $0.000019 | 0.75 | 266ms | 100.0% | 0.0% |
| MLP Router (\(\lambda = 0.50\)) | $0.000023 | 0.75 | 258ms | 100.0% | 0.0% |
| Cascade Router (\(\tau = 0.60\)) | $0.000017 | 0.62 | 239ms | 100.0% | 66.7% |
| Always OpenAI Cloud | $0.000044 | 0.75 | 250ms | 100.0% | 0.0% |
| Always Ollama Local | $0.000000 | 0.31 | 190ms | 100.0% | 8.3% |
These curves show the actual measured performance frontier across swept cost levels:
Pareto sweeps comparing KNN, MLP, FrugalGPT Cascades, and the Zero Router baseline. Note the efficient frontier pushed to the top-left by the learned routers.
Comparison of processing latency and time-to-first-token (TTFT) metrics across different routing scenarios.
Through exact stream deduplication via Redis Pub/Sub, multiple concurrent burst requests calling duplicate system prompts are coalesced into a single upstream model invocation.
Prefix-affinity routing identifies Warm KV-caches on GPU nodes using a Radix Trie, routing prompts to nodes with active context caches to eliminate prefill latency.
InferRoute provides a built-in evaluation framework to verify the cost-quality trade-offs of all routing algorithms under realistic workload datasets. The system sweeps ratios and willingness-to-pay parameters to export Pareto curves:
This script iterates across dataset prompts, simulating requests against mock or real endpoints and logging cost, latency, quality, and routing outputs:
python benchmarks/run_router_eval.py
This script reads the raw evaluation outcomes, fits the cost-quality points using the Trapezoidal Rule to calculate Area Under the Curve (AIQ), and exports standard PNG curves:
python benchmarks/plot_results.py
The evaluation sweeps the target cloud mixture ratio \(p \in [0, 1]\) for the Zero Router baseline, and sweeping trade-off thresholds \(\lambda \in [0, 1]\) or \(\tau \in [0, 1]\) for KNN, MLP, and Cascade routing algorithms to systematically construct the Pareto frontier.
InferRoute maintains system resilience through autonomous closed-loop feedback controllers.
Inspired by TCP Vegas congestion control, the gateway dynamically scales concurrent request slots based on measured latency queue sizes. It auto-throttles requests during model spikes to prevent local GPU OOMs.
Monitors consecutive timeouts and error codes. Transitions from CLOSED to OPEN upon 5 consecutive failures, bypassing degraded local nodes to fallback cloud targets instantly, recovering automatically via HALF-OPEN testing.
Play with all 9 academic routing policies directly in your browser. This sandbox simulates prefix cache check, Vegas limiter slots, RouteLLM Bradley-Terry勝率 matching, R2-Router word restraints, and Router-R1 agentic draft correction.