Skip to content

Building with agents, lesson 9.2

Gateways, model routing and cost

When every app calls the models directly, the bill is one number nobody can explain. This lesson puts a gateway in front: a capped key per team, the right model for each job, cheaper repeats, and the cost of each finished task.

What do you run?

Every example below changes to fit what you pick. The businesses are made up for teaching.

The picture, one step at a time

Each box is one stage. The picture lights up as you scroll.

Step 1 of 6

Every app with its own key

Before a gateway, every app, script and agent holds its own key to each model provider. The bill arrives as one number, and nobody can say which team or which job spent it. If a key leaks, you hunt for it in a dozen places. Picture that for customer emails.

Step 2 of 6

One gateway, one key per team

A gateway is a small service that every app calls instead of calling the model providers directly. It holds the real provider keys. Each team gets its own key to the gateway, with a monthly cap and an alert when it gets close.

Step 3 of 6

Route by the job

Not every call needs the biggest model. Easy, narrow jobs, like picking a label, pulling out a date or answering yes or no, go to a small model. Hard ones go to a big one. Or try the small one first and step up only when a check on its answer fails.

The RouteLLM paper (Ong and others, 2024) trained routers that choose between a strong and a weak model, and reported cutting costs "by over 2 times in certain cases" without hurting the quality of answers.

Which job should go to the small model?

Step 4 of 6

Cache what repeats, batch what can wait

Many calls start with the same long instructions. Caching keeps that part ready on the provider's side, so later calls pay much less for it. And work nobody is waiting for, like overnight runs, can go as a batch at a lower price.

Anthropic's prices as of November 2026: writing to a five-minute cache costs 1.25 times the normal input price, and reading from it a tenth of the normal input price or less. Its batch service costs half the normal price, and most batches finish within an hour.

Step 5 of 6

A fallback list

Providers go down and hit their limits. The gateway keeps a list: if the first model fails or is too slow, the request goes to the next one. Test the fallback before you need it, because a different model may answer in a slightly different shape.

Step 6 of 6

Count the cost per finished task

Because every call goes through the gateway, it can log the team, the task and the tokens for each one. Add up all the calls one task made, retries included. That is the number to watch: what a finished job costs, not what one call costs.

Made-up numbers: a task makes 6 calls at 2p each, and 1 task in 5 has to be run again. Roughly what does a finished task cost?

Which saving fits?

Here are four jobs in your business. Smaller model: the job is narrow and easy to check. Cache: the same long text starts every request. Batch: nobody is waiting for the answer.

  1. 1Labelling each incoming email as order, return or other.

  2. 2Every request starts with the same 3,000-word returns policy and tone guide.

  3. 3Rewriting 2,000 product descriptions over the weekend.

  4. 4Checking whether a review mentions a damaged item: yes or no.

0 of 4 right so far, 4 to go.

A cost calculator

This is what you keep: the sum for what one finished task costs, step by step, so you can work it out for any job.

A cost calculator

  1. Count the calls one finished task makes, and the average tokens in and out of each call.
  2. Multiply by the price per token of the model each call goes to.
  3. Take off what caching saves on the repeated part, and the batch discount on anything that can wait.
  4. Add the retries: if one task in five is run again, multiply by 1.2.
  5. Divide the team's monthly cap by that cost to see how many tasks the budget buys.

Price the finished task, not the call: route the easy jobs, cache what repeats, batch what can wait.

Next: Lesson 9.3: Rolling agents out across a company. How to start with one team, measure what changes, and widen only what worked.