Cost and performance
“An agent’s bill does not grow with what it does. It grows with what it remembers.”
Objective
Locate where an agent’s money is spent, and present the levers that reduce cost and latency — in the order of application that gives the highest return for the effort.
11.1 Where the money goes
Three places, in order of impact:
-
Resent history. Each step resends everything. It is the largest cost of a long agent — and it grows faster than linearly, as Chapter 5 shows.
-
Tool output. A tool that returns 50 KB of log burns context in one step and in all the following ones.
-
System prompt. Pasted-in skills, long examples, redundant instructions.
Intuition commonly points to the model as the main factor. In practice, for agents with many steps, the model is the second factor: the first is how many times you query it and with how much text.
11.2 The levers, in order of return
| Lever | Effect | Effort |
|---|---|---|
| Truncate tool output | High | Low |
| Skill index instead of content | High | Low |
| Context compaction with a ceiling | High | Medium |
| Prompt caching (when available) | Medium | Low |
| Smaller model for subtasks | Medium | Medium |
| Parallelize subagents | Latency | High |
Table 11.1. Cost levers, ordered by return on effort.
The first row is the one almost nobody does and the one that gives the highest return. A tool that returns an entire 200 KB log is paying for 200 KB in every subsequent step, forever. Truncating to 2 KB with a truncation notice solves the problem at the source.
11.3 Latency is not cost
Cost and latency have different solutions. Cost is solved with fewer tokens. Latency is solved with parallelism and with fewer steps.
Fewer steps come from better tools: one tool that does in one call what three did in three. Parallelism comes from independent subagents running at the same time, as in Chapter 8.
What does not work: asking the model to “be faster”. It has no access to the clock.
11.4 What to measure
Without measurement, optimization is guesswork. Four numbers cover the essentials:
-
Tokens per step — average and maximum. A maximum far above the average points to a chatty tool.
-
Steps per task — how many steps a completed task takes on average. This is the number that affects cost the most.
-
Completion rate — percentage of tasks that end with a final answer, not because of the budget.
-
Cost per completed task — the number the finance team will ask for. Note that it includes the cost of the tasks that did not complete.
The third number is often forgotten. An agent that completes 60% of tasks but spends the whole budget on the other 40% may cost more per completed task than an agent that is “more expensive” per step and completes 95%.
Common pitfall
Optimizing tokens per step without looking at the completion rate. Cutting the context to the minimum can make the agent forget the goal and spend more steps — or give up. The metric to optimize is cost per completed task, not cost per step.