A tutorial details a method for distributing the steps of an AI agent across multiple language models (LLMs), sending each step to the model deemed most suitable. When an economical model fails a verification, the request is handled by a more powerful model. The stated goal is to measure the cost per accepted task rather than the price per token.
Routing, Cascading, and Fallback
Routing involves choosing which model processes each request. A router decides before the call, while a cascade first tries a cheap model, checks its response, and only involves a larger model in case of failure. Fallback serves as a backup solution: a second model takes over when the first returns an error.
The tutorial combines the first two approaches. A routing table designates the default model for each type of step, and a validator triggers a single escalation when the economical model’s output is unusable. Each method thus compensates for the other’s weakness.
A Support Agent Tested Under Four Configurations
The experiment focuses on a customer service ticket triage agent, which processes ten short messages. Each ticket goes through three steps: planning, where the agent identifies the customer’s intent (delivery, refund, or account access) and the tool to use; tool calling, where it produces a small JSON object containing the tool name and an argument, such as an order number; and finally synthesis, where it drafts a response citing the facts returned by the tool.
The same set of tickets is submitted to four configurations. In frontier_only, everything goes through the frontier model; in local_only, everything goes through the local model. In routed, planning goes to the frontier model and the rest to the local model, without escalation. routed_escalate uses this same distribution with escalation enabled. Scoring is deterministic, without an LLM-based judge: a ticket is only successful if the intent, tool, arguments, and required facts are all correct. The tutorial tracks the success rate, cost and latency per ticket, escalation rate, and cost per accepted task, which is the total spend divided by the number of successful tickets. As the article puts it, “a cheap configuration that fails half the time is not cheap.”
Costs, Validators, and Router
For setup, you need Python 3.10 or newer, a machine capable of running a small local model via Ollama, and an Anthropic API key. The local model is llama3.2:3b. The key is read from an environment variable, so it never appears in the code.
The cost registry converts tokens, the units of text processed by the model, into dollars. It relies on pricing published by Anthropic: $2 and $10 per million input and output tokens for Claude Sonnet 5.5, $0.10 and $0.50 for Claude Haiku 5.5, for prompts up to 100,000 tokens. Local calls are counted as zero, a simplification noted by the article, since hardware consumes electricity.
The router is just a few lines of code. Its table assigns planning to the frontier model and mechanical steps to the local model. Escalation is only allowed once per step, so costs cannot spiral. The validators, simple Python checks that do not call any model, verify, for example, that the JSON is well-formed and that the argument actually appears in the ticket, which allows spotting a fabricated order number. The evaluation script can first be run on two tickets, then on all ten, and displays a table with one row per configuration.
