Abstract
Selecting the optimal Large Language Model (LLM) for a single task is challenging, but selecting a sequence of LLMs for a multi-step pipeline introduces complex dependencies where each model's output influences the subsequent model's performance and cost. We present SeqBandits, a neural contextual bandit algorithm designed to navigate these dependencies online. SeqBandits optimizes for a "Net Reward" that balances accuracy and monetary cost across interconnected subtasks. Our evaluation on medical and telecommunications datasets demonstrates that SeqBandits outperforms static baselines and standard bandit approaches, achieving up to a 7.60% improvement in net reward.
Introduction
The proliferation of Large Language Models (LLMs) has created a "paradox of choice" for developers. While high-end models offer superior accuracy, they come with significant financial costs. Conversely, smaller, cheaper models may suffice for simpler tasks but fail on complex ones. Current "router" solutions typically focus on single-task selection. However, real-world applications often involve multi-stage pipelines—such as summarizing a medical record before performing a diagnosis. In such pipelines, the choice of an upstream model (e.g., the summarizer) directly impacts the input quality, success rate, and token cost of downstream models. Existing selection methods often treat these subtasks in isolation or rely on fixed sequences, failing to account for these dynamic dependencies.
Methodology: The SeqBandits Framework
We frame the model selection problem as a neural contextual multi-armed bandit (MAB) problem. The core of our approach is the Sequential Bandits (SeqBandits) algorithm, which learns to select the optimal model for each subtask in a pipeline through online interaction.
Neural Reward Modeling
Unlike traditional approaches that use a single global model to predict success, SeqBandits maintains a separate neural network for every (subtask, LLM) pair. This architectural choice is critical; it prevents the algorithm from prematurely converging on a single model and ensures robust exploration of the model space.
Cost-Aware Optimization
The algorithm optimizes for a "Net Reward," defined as:
Net Reward = Accuracy - (α * Cost)
where α is a scaling factor for monetary cost based on per-token pricing. To estimate costs before execution, we employ a BERT-based regression model to predict the number of output tokens for a given query and model.
Experimental Setup
We evaluated SeqBandits on two domain-specific pipelines:
- Medical Diagnosis Prediction: Utilizing a dataset of 100 de-identified reports from MIMIC-III, the pipeline consists of a Summarizer followed by a Diagnoser.
- TeleQnA: A dataset of 10,000 telecommunications questions processed through a three-stage pipeline: Summarization -> Answering -> Explanation.
Results and Discussion
Our results indicate that a pipeline approach (e.g., Summarizer + Diagnoser) frequently outperforms single-model execution, as breaking tasks into smaller components makes them more manageable for LLMs.
Performance and Efficiency
In the medical pipeline, SeqBandits achieved a 7.60% improvement in net reward over the best static baseline (always using Llama). In the telecom setting, it showed a 6.51% improvement over random selection. Crucially, SeqBandits consistently identified the "sweet spot" on the Pareto frontier, achieving the highest net reward while maintaining near-minimal total costs.
Context-Dependent Selection
The algorithm learned that the "optimal" model is highly context-dependent. For instance, the best model for diagnosis varies based on which model performed the preceding summarization. Furthermore, SeqBandits often favored models like Llama and GPT-3.5 Turbo for medical tasks, as they provided high accuracy at a fraction of the cost of specialized medical models.
Conclusion
SeqBandits demonstrates the efficacy of using neural contextual bandits for optimizing LLM pipelines. By accounting for inter-task dependencies and balancing accuracy against cost, it provides a robust framework for navigating the complex LLM ecosystem. Future work will explore incorporating latency into the optimization objective and developing "agentic" systems capable of dynamic task decomposition.
Summary updated