Sub topic 16
Routing Requests Between Small and Large Models
This chapter assumes you have read the chapters on model sizes, token pricing, and evaluation. You should also be able to read basic Python and call a model through an OpenAI-style client.
Why route at all
Earlier chapters led to one rule: pick the smallest model that meets your requirements. In practice, requests vary. A request to extract an invoice number is easy. A request to reconcile three contradictory contracts is hard. One model for everything forces a choice. A model sized for the hard cases overpays on the easy ones. A model sized for the easy cases fails on the hard ones.
Routing sends each request to a model that fits it. Most traffic goes to a cheap, fast small model. Only the difficult remainder goes to a large model.
Two common patterns
- Rule-based routing. You decide before calling any model, using signals you can compute: task type, input length, or which feature of your app made the call. It is predictable and cheap to run.
- Escalation (cascade). You call the small model first and check the result. If the check fails, you call the large model. It adapts to difficulty, but failed attempts add latency and cost.
A working example
This code combines both patterns. It uses a rule first, then tries the small model and escalates if the output is not valid. The model names come from environment variables, which keeps the swappable design from the portability chapter.
import os
from openai import OpenAI
client = OpenAI(
base_url=os.environ["LLM_BASE_URL"],
api_key=os.environ.get("LLM_API_KEY", "not-needed-for-local"),
)
SMALL = os.environ["LLM_SMALL_MODEL"]
LARGE = os.environ["LLM_LARGE_MODEL"]
def call(model: str, prompt: str) -> str:
response = client.chat.completions.create(
model=model,
messages=[{"role": "user", "content": prompt}],
temperature=0,
)
return response.choices[0].message.content
def looks_valid(answer: str) -> bool:
# Task-specific check: here, an invoice ID must appear.
return "INV-" in answer
def route(prompt: str) -> str:
if len(prompt) > 20_000: # rule: long inputs go straight to the large model
return call(LARGE, prompt)
answer = call(SMALL, prompt) # escalation: try small first
if looks_valid(answer):
return answer
return call(LARGE, prompt)Choosing the check
The escalation check decides whether routing works. Prefer checks you can compute:
- Output matches a required format, such as valid JSON or a regular expression.
- Required fields are present.
- A simple rule from your evaluation set passes.
Avoid relying on the small model to rate its own confidence. Models can be wrong with great certainty.
Common errors
- Routing without measuring. Use your evaluation set from the earlier chapter. Record how often the small model passes. If only 40% pass, most requests pay for two calls, and a single large model may be cheaper.
- Ignoring added latency. An escalated request waits for both calls.
- Weak checks. A check that passes wrong answers sends bad output to users.
- Complexity too early. Routing adds code, tests, and monitoring. Start with one model and add routing when cost or latency data justifies it.
Exercise
The small model costs $0.20 per 1,000 requests and passes your check on 80% of them. The large model costs $2.00 per 1,000 requests. Ignoring latency, what is the cost per 1,000 requests with escalation, and is it cheaper than using only the large model?
Show answer
Every request calls the small model ($0.20). The 20% that fail also call the large model, which adds 0.2 × $2.00 = $0.40. The total is $0.60 per 1,000 requests, which is cheaper than $2.00 for the large model alone. This assumes the check catches all bad answers.