Abstract
Large Language Models (LLMs) for code generation face a significant trade-off between performance and cost. While large models excel at complex tasks, they are prohibitively expensive for simpler ones. This paper introduces AdaptiveLLM, a dynamic routing framework that utilizes Chain-of-Thought (CoT) length as a proxy for task difficulty. By matching problem complexity to model capability, AdaptiveLLM achieves a 7.86% improvement in accuracy while reducing resource costs by nearly 89% compared to state-of-the-art baselines.
Introduction
In the field of AI-assisted coding, developers face a "Goldilocks" problem: large, powerful models (e.g., Qwen-32B) are capable but expensive and slow for simple tasks, while small models are efficient but prone to failure on complex logic. Existing LLM routing solutions often suffer from high resource intensity, reliance on subjective human-annotated difficulty labels, and a lack of consideration for actual inference costs. AdaptiveLLM addresses these issues by providing an automated, objective, and cost-aware selection mechanism.
Methodology
The AdaptiveLLM framework operates through a three-stage pipeline designed to automate model selection:
- Automated Difficulty Assessment via CoT: The framework uses a reasoning model (specifically DeepSeek-R1-Distill-Qwen-32B) to generate a Chain-of-Thought for a given problem. The length of this reasoning process serves as a highly reliable indicator of how "hard" the problem is for an AI.
- Difficulty-Aware Embeddings: These CoT lengths are clustered into three levels using k-means. A CodeBERT model is then fine-tuned using triplet contrastive loss to create vector representations (embeddings) where problems of similar difficulty are grouped together in the vector space.
- Optimal Model Selection: Finally, an XGBoost classifier takes these embeddings and selects the best model from a candidate pool of eight LLMs (ranging from 1.5B to 33B parameters). The selection is guided by a custom scoring formula that balances the probability of a correct answer (
pass@1) against the financial cost of the tokens.
Datasets and Evaluation
The framework was trained and evaluated on three well-known benchmarks representing a gradient of difficulty:
- HumanEval: Simple, function-level code completion tasks.
- LeetCodeSample: Mid-level algorithmic challenges with longer descriptions.
- CodeContests: High-complexity competitive programming problems involving advanced paradigms like dynamic programming and graph theory.
Key Results
AdaptiveLLM demonstrates superior efficiency across these benchmarks. Compared to the baseline ComplexityNet, AdaptiveLLM improved the pass@1 score by 7.86% while reducing resource consumption by 88.9%. When compared to using a single high-end model, AdaptiveLLM demonstrated a 15% accuracy improvement at the same cost level. For pass@1 tasks, it reduced inference costs by 88.3% by intelligently routing simple tasks to tiny 1.5B models and reserving the "heavy hitters" for truly difficult code.
Discussion and Insights
A striking insight from this research is that CoT length is a better predictor of LLM performance than human labels. AI "intuition" often differs significantly from human perception; problems labeled "Hard" by humans may be solved easily by an LLM with a short CoT. Furthermore, the authors found that larger reasoning models produce more stable and consistent CoT lengths, providing a reliable "complexity meter." AdaptiveLLM effectively navigates the cost-performance Pareto frontier by picking the cheapest model likely to succeed.
Conclusion and Future Work
AdaptiveLLM offers a significant step forward in cost-efficient AI coding. However, limitations remain, including the stochastic nature of CoT generation and the current focus on single-file code generation. Future work will explore multi-file, project-level tasks and more nuanced cost modeling for architectures like Mixture-of-Experts (MoE), which may have many parameters but low active inference costs.
Summary updated