Model Cascading for Code: A Cascaded Black-Box Multi-Model Framework for Cost-Efficient Code Completion with Self-Testing

Boyuan Chen, Mingzhi Zhu, Brendan Dolan-Gavitt, Muhammad Shafique, Siddharth Garg

TL;DR

This paper introduces a black-box framework that cascades code generation tasks through a hierarchy of LLMs, using self-generated tests to decide when to stop or escalate to a larger model. It achieves significant cost reductions (up to 70%) while maintaining high accuracy, providing a flexible way to navigate the cost-accuracy trade-off in production code completion.

What Problem Does This Paper Address?

Large Language Models (LLMs) have revolutionized code completion, but they come with a hefty price tag. Developers often use "self-testing" schemes—where a model generates multiple solutions and tests, then picks the one that passes the most tests—to boost accuracy. While effective, this approach multiplies inference costs because it requires many forward passes through large, expensive models.

Current cost-reduction techniques like quantization or pruning focus on optimizing a single model. However, in real-world production environments, we often have access to a family of models (e.g., 7B, 13B, and 34B versions). The challenge is determining how to intelligently route queries through these models to minimize cost without sacrificing the high accuracy users expect.

Methods

The authors propose a cascaded multi-model framework with self-testing. Instead of sending every query to the largest model, the system follows a "start small, escalate if needed" strategy:

  1. Cascading Pipeline: A query starts at the smallest model ($m_1$). If the model isn't confident in its answer, the query moves to $m_2$, and so on, until the largest model ($m_n$) is reached.
  2. Self-Testing as a Gatekeeper: At each stage, the model generates $k$ solutions and $k$ test cases. It runs the solutions against the tests and calculates a quality score based on passing rates and test robustness.
  3. Threshold-Based Escalation: A learned threshold $\theta$ determines if the best solution is "good enough." If the score exceeds the threshold, the system returns the answer and stops. Otherwise, it escalates to the next larger model.
  4. Pareto Optimization: The system doesn't just guess parameters. It uses a validation set to find the "Pareto-optimal" combinations of solutions ($k$), test lines ($l$), and threshold ($\theta$) that offer the best accuracy for a given budget.
Figure 3: The cascading pipeline starts with small models and escalates based on self-testing scores.

Datasets

The framework was evaluated on three standard code generation benchmarks:

  • HumanEval: 164 hand-written Python problems.
  • MBPP-sanitized: 427 entry-level Python programming concepts.
  • APPS-test (Introductory subset): 1,000 introductory-level coding challenges.

Key Insights and Ideas

  • The "Sledgehammer" Problem: A key insight is that large models are often overkill. As shown in the Venn diagram below, over 90% of the problems a 34B model can solve are also solvable by its smaller 7B or 13B siblings. Figure 2: Venn diagram showing that smaller models can solve the vast majority of problems handled by larger ones.

  • Verification > Generation: LLMs are generally better at verifying code (writing tests) than generating the code itself. The framework exploits this by using self-generated tests as a reliable signal for when a model has "failed" and needs help from a larger peer.

  • Dynamic Adaptation: Unlike static models, this framework allows servers to shift their strategy based on real-time constraints like user volume or budget, simply by picking a different point on the Pareto-optimal curve.
Figure 2: Venn diagram showing that smaller models can solve the vast majority of problems handled by larger ones.

Key Results

  • Massive Cost Savings: The cascading approach reduced inference costs by an average of 26%, with savings reaching up to 70% in the best-case scenarios (like the Codegen-mono family).
  • Superior Efficiency: Across all accuracy levels, the cascading scheme consistently outperformed single-model self-testing. It achieved the same accuracy as large models but at a fraction of the cost. Figure 1: Comparison showing cascading (green) consistently achieving higher accuracy at lower costs than single-model baselines.

  • Black-Box Compatibility: Because the method is black-box, it works with any model family (WizardCoder, Codegen, etc.) without needing access to internal weights or gradients.

Figure 1: Comparison showing cascading (green) consistently achieving higher accuracy at lower costs than single-model baselines.

Limitations and Future Work

  • The "Floor" Effect: The authors found that if the smallest model in a family has very low accuracy (below 10%), the cascading scheme can actually waste money. The small model generates poor tests that lead to false positives or unnecessary escalations.
  • Global vs. Local Thresholds: Currently, the framework uses a single global threshold $\theta$. Future work could explore using different thresholds for each model in the chain to further fine-tune performance.
  • Latency vs. Cost: While cascading saves money, it can increase latency for difficult problems that eventually reach the largest model. Integrating speculative decoding is suggested as a promising way to address this in future iterations.
  • Language Scope: While the logic is language-agnostic, the current study focused exclusively on Python. Testing on other languages like C++ or Java remains for future work.

Summary updated

BibTeX
@inproceedings{chen2025model,
  author = {Boyuan Chen and Mingzhi Zhu and Brendan Dolan-Gavitt and Muhammad Shafique and Siddharth Garg},
  title = {{Model Cascading for Code: A Cascaded Black-Box Multi-Model Framework for Cost-Efficient Code Completion with Self-Testing}},
  booktitle = {IJCNN},
  year = {2025},
  url = {https://openreview.net/forum?id=AQeh1aXy9S},
}
Figure