Sub topic 11
Routing steps to the right model
This chapter assumes you can read basic Python and know the idea of model tiers (small, mid, large) from the earlier chapter on choosing between them. It also assumes you know that an agent loop makes many separate model calls.
Why route at all
An agent session is not one task. It is a series of steps, and the steps differ in difficulty. Planning a refactor needs strong reasoning. Summarizing 300 lines of test output does not. If every step goes to the largest model, you pay large-model rates for work a small model does just as well.
Routing means choosing a model for each step instead of for the whole session. The model tiers differ in price and latency, so a good routing policy lowers both cost and wait time.
A routing policy as a lookup
The simplest policy maps a step kind to a model tier. Anything you did not list falls back to a default.
ROUTES = {
"plan": "large-model",
"edit": "mid-model",
"summarize_tool_output": "small-model",
"run_tests": "small-model",
}
def pick_model(step_kind, default="mid-model"):
return ROUTES.get(step_kind, default)
print(pick_model("plan")) # large-model
print(pick_model("other")) # mid-modelThe model names are placeholders. Replace them with the tiers your provider offers. The default matters: an unknown step should go to a safe middle option, not to the cheapest or the most expensive model.
Choosing what goes where
Use these questions to assign a step to a tier:
- Does a wrong answer cost a lot to fix? Plans and edits do, because later steps build on them. Use a stronger model.
- Is the output easy to check? Test results and linter output are. A cheap model can handle them, and a failure shows up right away.
- Does the step mostly compress text? Summarizing tool output reduces context for later turns, and a small model does it well.
- Is latency noticeable to the user? Fast small models help in interactive loops.
Routing also combines with subagents. A subagent that searches the repository and returns a short summary can run on a small model, while the main agent stays on a stronger one.
Escalation
A static table cannot know when a cheap model is struggling. A common addition is escalation: try the cheaper tier first, and move up if a check fails.
TIERS = ["small-model", "mid-model", "large-model"]
def run_with_escalation(task, attempt, check):
"""Try each tier in order. Return (model, result) for the first that passes."""
for model in TIERS:
result = attempt(model, task)
if check(result):
return model, result
return None, None
# Toy stand-ins: only the large model "solves" the task.
def attempt(model, task):
return {"model": model, "solved": model == "large-model"}
def check(result):
return result["solved"]
print(run_with_escalation("fix bug", attempt, check))
# ('large-model', {'model': 'large-model', 'solved': True})Escalation only works when check is reliable, such as a passing test suite. Without a trustworthy check, you cannot tell a good cheap answer from a bad one.
Common errors
- Switching models in the middle of a conversation can break prompt caching. Caches are usually tied to a specific model, so a new model reads the whole context at the full input rate. Route at step boundaries where the context is small, or to a subagent with its own context.
- Routing by guess instead of by data. Check your choices with cost per resolved task, as covered in a later chapter.
- Too many tiers. Two or three are usually enough, and each extra route is another rule to maintain.
Exercise
Your agent routes "run_tests" to a small model, but the small model's summaries often miss the failing test name, so the large model has to re-read the raw output. What should you check before keeping this route?
Show answer
Compare the total cost per resolved task with and without the route. The re-reads add large-model input tokens, so the saving from the small model may be cancelled out. If the small model fails often, move the step to a mid-tier model or change the prompt so the summary always includes the failing test name.