Controlling the cost of AI agents
Where agent costs come from and the simple controls that keep them predictable.
Models charge for input tokens (everything sent) and output tokens (what comes back), with output usually several times dearer. An agent sends the whole conversation again on every step, including every tool result so far, so long runs grow expensive faster than you'd guess.
The biggest savings come from design, not discounts: fewer steps (good tool descriptions), smaller tool results (the last 50 log lines, not the file), and the smallest model that passes your evaluations. Some providers also discount repeated input through prompt caching.
Add hard limits: a budget per run in your code, rate limits per user, and a spending limit or alert on the provider account. Record tokens and cost in every trace so you can see which requests are expensive.
Cost. Model prices are per million tokens and change often; always check the provider's current page and work out a GBP estimate before running anything paid.
Use it when
- before giving an agent to many users.
- a bill was higher than expected.
- you're choosing between models.
How it works
- Measure
- Tokens and cost per step in traces.
- Budget
- Stop a run over its limit.
- Shrink context
- Trim tool output and old steps.
- Route
- Small model first, larger only when needed.
- Alert
- Spending limits on the account.
Options
- Small model
- Cheap and fast; fine for simple, well-tooled jobs.
- Large model
- Better at hard reasoning; use where evaluations show it's needed.
Example. Harbourline Design capped each agent run at a budget of 5p and trimmed log tool results to 50 lines. Monthly cost fell by 70%, and the cap caught one runaway loop on its first day.
Don't confuse it with
- Input tokens
- Grow with every step and tool result.
- Output tokens
- Usually fewer, but priced higher.
Words to know
- Token
- About four characters of English text.
- Prompt caching
- A discount for resending the same opening text.
- Model routing
- Choosing a model per job by difficulty.
Where to look
- Traces
- Provider billing and limits
Practise it
Read it in the guide, next to the tasks and labs.
Open in DevOps, Hands On