How to reduce AI agent latency and cost: measure first, then cut steps

For agent builders4 min read

A backend engineer in Nairobi watching a stopwatch-style timing chart on a monitor, wearing glasses, at a desk with a plant, daylight.

Here is how to reduce AI agent latency and cost: measure where the time and tokens go in each run, then cut the biggest item. Usually that is the number of model calls. This post gives a small meter you can add to any agent, then the changes that tend to pay off: fewer steps, easier routing and hard caps.

Why agents are slow and expensive

Each step in an agent loop is a model call, often followed by a tool call. Cost and delay grow with the number of steps, and every step re-sends the growing conversation. Anthropic's article on building agents states the trade directly: agents trade latency and cost for better performance, so use them when that trade is worth it.

Source: Building effective agents(opens in a new tab)

Add a meter first

You cannot reduce what you have not counted. This small class records steps, tokens and elapsed time for one run, and turns tokens into money using prices you enter yourself. Take the prices from your provider's pricing page, since they change.

Python
import timefrom dataclasses import dataclass, field @dataclassclass RunMeter:    price_in: float        # money per 1M input tokens (from your provider)    price_out: float       # money per 1M output tokens    started: float = field(default_factory=time.perf_counter)    steps: int = 0    tokens_in: int = 0    tokens_out: int = 0     def add_step(self, tokens_in, tokens_out):        self.steps += 1        self.tokens_in += tokens_in        self.tokens_out += tokens_out     def report(self):        cost = (self.tokens_in * self.price_in                + self.tokens_out * self.price_out) / 1_000_000        return {"steps": self.steps,                "tokens_in": self.tokens_in,                "tokens_out": self.tokens_out,                "cost": round(cost, 4),                "seconds": round(time.perf_counter() - self.started, 2)} meter = RunMeter(price_in=3.0, price_out=15.0)   # example numbers onlyfor tin, tout in [(1200, 150), (1900, 120), (2600, 300)]:    meter.add_step(tin, tout)print(meter.report())

Notice that input tokens rise at every step in the example. That is the usual pattern, because each call includes the earlier history. Most providers return token counts in their responses, so feed those numbers into add_step rather than estimating.

Log the report with every run, and compare median and high percentile values across tasks. The metrics post in this series shows how to compute them.

How to reduce AI agent latency: changes that usually pay off

ChangeWhy it helpsWhat to watch
Remove stepsEach step is a model call and a delaySuccess rate on the eval set
Route easy requests to a smaller modelThe article describes sending easy queries to a smaller modelMisrouted hard requests
Run independent calls in parallelTotal time becomes the slowest callRate limits
Shorten the contextFewer input tokens each stepLost information
Cap iterationsA stuck loop cannot run foreverTasks that need more steps

The building agents article describes routing as sending an input to a specialized follow-up, such as a smaller model for easy queries, and parallelization as running calls at the same time. It also recommends stopping conditions such as a maximum number of iterations.

Start with the largest cost

  1. Run your eval set with the meter on. Sort runs by cost.
  2. Read the three most expensive transcripts. Look for repeated calls, tool results that are far too large, and loops.
  3. Fix the cause. A tool that returns a whole document when one field is needed is a common one.
  4. Re-run the eval set. Keep the change only if success did not drop.
  5. Set a step cap just above the highest healthy run, and alert when runs hit it.

Frameworks may have their own defaults. AgentOven's feature page, for example, lists a default of 10 turns for its agent loop and a default context budget of 16,000 tokens per agent. Check the defaults in whatever you use and set them on purpose.

Source: AgentOven features(opens in a new tab)

One more habit helps: keep a price list of your own runs. Multiply the cost of a typical run by the number of runs you expect per day, and compare it with the value of the task. If a run costs more than the work is worth, no amount of tuning on latency will fix the design.

Do not cut blindly

A cheaper agent that fails more often can cost more once you count retries, human review and unhappy users. Always read cost next to success rate. Anthropic's eval guide also suggests using constraints such as turn limits to measure efficiency, which gives you a way to compare two versions on the same budget.

Source: Demystifying evals for AI agents(opens in a new tab)

Last, simplify first. If a single model call with good context does the job, the cheapest agent is no agent. The article recommends starting with the simplest solution and adding complexity only when it measurably helps.