Model routing: how RouteLLM selects strong and weak models

RouteLLM serves and evaluates routers; its paper studies router training to select stronger or weaker LLMs during inference.

Verified

On this page

Definition

RouteLLM is a framework for serving and evaluating LLM routers. Model routing selects a model for a request before calling it. The RouteLLM paper studies a training framework that dynamically selects a stronger or weaker LLM during inference. This selects between models, a different decision from choosing a provider for one already selected model. RouteLLM illustrates the path here; a related tool’s directory description does not establish that it uses the same algorithm. The paper says a router can direct simpler queries to smaller models and more complex ones to larger models, which balances response quality with cost efficiency. The paper's approach routes between two classes of models: strong models, which provide high-quality responses at a high cost, such as GPT-4, and weak models, which offer lower-quality responses at a reduced cost, such as Mixtral-8x7B.

At a glance

In RouteLLM, a router returns the strong model's win rate for a prompt, and the user-specified cost threshold decides which model receives the request.

  • A user prompt goes to calculate_strong_win_rate, which returns the win rate for the strong model conditioned on that prompt.
  • If that win rate is greater than the user-specified cost threshold, the request is routed to the strong model.
  • Otherwise, the request is routed to the weak model.
  • A higher cost threshold leads to lower cost but may lead to lower-quality responses.

The source does not state the order of these points, so they are listed without arrows.

How it works

The paper separates reward modeling from routing: reward modeling assesses response quality after an LLM generates it, whereas routing involves selecting the appropriate LLM beforehand. The paper's framework leverages human preference data and employs data augmentation techniques to enhance performance. Its primary source of preference data is 80k battles from the online Chatbot Arena platform, where users submit prompts and receive responses from two anonymous models.

In Chatbot Arena, users review the responses and then vote for a winner or declare a tie. In training, the paper excludes model responses and retains only the winner identities. The RouteLLM README lists the mf router as using a matrix factorization model trained on the preference data.

The sw_ranking router uses a weighted Elo calculation for routing, where each vote is weighted according to how similar it is to the user's prompt. The bert router uses a BERT classifier trained on the preference data. The causal_llm router uses a LLM-based classifier tuned on the preference data.

The random router randomly routes to either model. Each routing request has a cost threshold that controls the tradeoff between cost and quality. When making a request to the server, clients specify the router and cost threshold for each request in the model field, using the format router-[ROUTER NAME]-[THRESHOLD].

For instance, a model of router-mf-0.5 specifies that the request should be routed using the mf router with a threshold of 0.5. The range of meaningful thresholds varies depending on the type of router and the queries received. Regardless of the model pair used, an OPENAI_API_KEY is currently still required to generate embeddings for the mf and sw_ranking routers.

Example

The four original blocks below cover installation, Controller initialization, threshold calibration and a request. Initialization replaces the OpenAI client with the mf router and configures the strong model, weak model and corresponding credentials. Read initialization first, then calibration, and finally inspect how the request’s model field identifies the router and threshold. Calibration is a separate preparation step rather than an operation required for every request. This workflow was not executed for this article. The official walkthrough selects gpt-4-1106-preview as the strong model and anyscale/mistralai/Mixtral-8x7B-Instruct-v0.1 as the weak model, setting the API credentials accordingly. These are historical identifiers from the original walkthrough; their current availability was not confirmed. Check service and model availability and your permission to use them before execution. The historical example does not establish current service entitlements. It then uses data from Chatbot Arena as an example to calibrate the threshold for 50% GPT-4 calls.

The original calibration output uses 0.11593 as the threshold, corresponding to approximately 50% of queries routed to GPT-4 on the walkthrough data. The original command and output are retained for comparison; they are not a threshold or routing share measured by this site. Distinguish the desired share of strong-model calls from a single request’s estimated strong-model win rate. Comparing that rate with the threshold determines the branch for the request. The official Hello request specifies the router and calibrated threshold in its model field and asks the client to generate a completion. Here that field identifies a routing strategy rather than one provider’s model name. The code below preserves the original form. Returned content, cost and model allocation are not reported as results measured for this article. Because thresholds are calibrated on an existing dataset, the README notes that the percentage of calls routed to each model will differ based on the actual queries received.

The README recommends calibration on data resembling the queries actually received. With a different query distribution, the same threshold may produce a different strong-model share. Inspect quality and allocation on representative queries you may use before selecting a threshold. Historical calibration describes the original setting and makes no fixed savings promise for a new task. RouteLLM also includes an evaluation framework to measure the performance of different routing strategies on benchmarks. The README says it currently supports the benchmarks mmlu, gsm8k, and mt-bench.

The paper reports that its routers exhibit strong generalization capabilities, maintaining performance even when routing between LLMs not included in training. On MMLU, the paper reports that routers trained only on the Arena dataset perform at the level of the random router, which it attributes to most MMLU questions being out-of-distribution. The paper reports that a higher benchmark-dataset similarity score is correlated with stronger performance on that benchmark for routers trained using the corresponding dataset.

The paper notes that real-world applications may have distributions that differ substantially from the benchmarks it evaluates on. For its cost analysis, the paper calculates the inverse of the ratio of GPT-4 calls made by its top-performing router relative to the random baseline, because the cost of GPT-4 is the dominant factor in that analysis. The paper reports savings of up to 3.66x in its own cost analysis, with its models, data and accounting assumptions. This is attributed as a research result rather than converted into a current service-cost promise. An application needs a fresh check using its actual models, prices and query distribution.

The paper estimates average costs of $24.7 and $0.24 per million tokens for GPT-4 and Mixtral 8x7B respectively. These are historical estimates used in the paper, not today’s quotations from a model provider. Check the relevant service, billing unit and current price separately when choosing a deployment.

From the official source
Shell
pip install "routellm[serve,eval]"
From the official source
python
import os
from routellm.controller import Controller

os.environ["OPENAI_API_KEY"] = "sk-XXXXXX"
# Replace with your model provider, we use Anyscale's Mixtral here.
os.environ["ANYSCALE_API_KEY"] = "esecret_XXXXXX"

client = Controller(
  routers=["mf"],
  strong_model="gpt-4-1106-preview",
  weak_model="anyscale/mistralai/Mixtral-8x7B-Instruct-v0.1",
)
From the official source
Plain text
> python -m routellm.calibrate_threshold --routers mf --strong-model-pct 0.5 --config config.example.yaml
For 50.0% strong model calls for mf, threshold = 0.11593
From the official source
python
response = client.chat.completions.create(
  # This tells RouteLLM to use the MF router with a cost threshold of 0.11593
  model="router-mf-0.11593",
  messages=[
    {"role": "user", "content": "Hello!"}
  ]
)

Sources

This page is written from the official sources below. Open them to check the original text.

Verified ·

This page is written by AI from the official sources above and checked automatically. This site has not tested it. Found a mistake? Tell us.