AI agents

The Economics of AI Agents: Cost Optimization, Resource Management, and ROI Strategies

What drives the cost of an agent run and how to control it: prompt caching, batch APIs, model routing, context management, budgets and ROI per task.

By the APIStack team··9 min read

A phone calculator, a pen and printed tax forms laid out on a table

An agent's bill is not the price of one model call multiplied by the number of tasks. An agent calls the model in a loop, and each call resends everything that came before it. This article explains how that shapes cost, which pricing mechanisms reduce it, and how to express the result as a cost per completed task that can be compared with the value of the task.

It is written for developers who run agents against a metered model API and need to forecast and control the spend. All prices quoted were read from the vendors' own pricing pages at the time of writing (November 2026) and will change; treat them as examples of the structure.

Where the money goes

Model APIs charge per token, with separate rates for input and output. On Anthropic's price list, output tokens cost five times as much as input tokens on the current models: at the time of writing (November 2026), Claude Sonnet 5.5 is listed at $2 per million input tokens and $10 per million output tokens.

Despite that ratio, input usually dominates an agent's bill, because of the loop. On every turn the request contains the system prompt, the tool definitions, and the full history of earlier tool calls and results. A run of 20 turns pays for the first tool result 19 more times. Total input grows roughly with the square of the number of turns.

Three other things are billed as input and are easy to miss:

  • Tool definitions. Names, descriptions and schemas in the tools parameter are input tokens on every request. Anthropic's pricing page also lists a fixed tool-use system prompt added whenever tools are present (286 tokens on Claude Sonnet 5.5 at the time of writing).
  • Tool results. A tool that returns a 20,000-character JSON document adds that document to every later turn.
  • Server-side tools. Some carry their own usage charge on top of tokens; the same page lists web search at $10 per 1,000 searches.

A worked example

The function below computes the model cost of one run. It assumes a fixed prefix (system prompt and tools), a constant number of tokens added per turn, and, when cache prices are given, that each turn reads the previous turn's prompt from the cache and writes only the new part.

Python
def run_cost(turns, prefix, per_turn, out_per_turn, price_in, price_out,
             cache_read=None, cache_write=None):
    """Dollar cost of one agent run. Prices are per million tokens."""
    uncached = read = written = 0
    for t in range(turns):
        prompt = prefix + per_turn * t
        if cache_read is None:
            uncached += prompt
        else:
            previous = 0 if t == 0 else prefix + per_turn * (t - 1)
            read += previous
            written += prompt - previous
    cost = uncached * price_in + out_per_turn * turns * price_out
    if cache_read is not None:
        cost += read * cache_read + written * cache_write
    return cost / 1_000_000

print(run_cost(20, 6000, 1500, 300, 2.00, 10.00))              # 0.87
print(run_cost(20, 6000, 1500, 300, 2.00, 10.00, 0.10, 2.50))  # 0.1833
print(run_cost(40, 6000, 1500, 300, 2.00, 10.00))              # 2.94

With a 6,000-token prefix, 1,500 tokens added per turn and 300 output tokens per turn, 20 turns send 405,000 input tokens and produce 6,000 output tokens. At the Sonnet 5.5 list prices above, that is $0.87, of which output is $0.06. Doubling the run to 40 turns costs $2.94, more than three times as much, which is the quadratic growth in practice.

The second line applies the listed cache prices for the same model ($0.10 per million tokens read, $2.50 per million written with the five-minute lifetime). The same run costs about $0.18. This is arithmetic on list prices under ideal cache behavior, not a benchmark, but it shows why caching is the first lever to pull.

Cache the stable prefix

Prompt caching lets the provider reuse its processing of a prompt prefix it has seen recently. It suits agents exactly, because each turn's prompt is the previous turn's prompt plus a little more.

On the Claude API, prompt caching is requested with a cache_control field. The cached prefix covers tools, then system, then messages, and a hit requires an exact match. Writes cost 1.25 times the base input price for a five-minute lifetime or 2 times for one hour; reads cost 0.1 times the base price on most models and less on some of the newest. A single top-level field enables automatic caching, which moves the breakpoint forward as the conversation grows.

JSON
{
  "model": "claude-sonnet-5-5",
  "max_tokens": 1024,
  "cache_control": { "type": "ephemeral" },
  "system": "You are a billing support agent. Follow the refund policy below.",
  "messages": [{ "role": "user", "content": "Order 1042 arrived damaged." }]
}

On the OpenAI API, prompt caching is automatic for supported models and also works by prefix match. Its guide gives the same structural advice: stable content first, dynamic content last, append instead of rewriting.

Caching fails silently, so the rules that keep the prefix stable matter more than the feature flag:

  • Keep timestamps, request IDs and user-specific text out of the system prompt. Put them in the latest message.
  • Do not add, remove or reorder tools during a run. On the Claude API a change to tool definitions invalidates the whole cached prefix. The 2026-07-28 MCP changelog asks servers to return tools in a deterministic order for this reason.
  • Check the usage fields on every response. On the Claude API, cache_read_input_tokens of zero on the second turn means the prefix changed or was below the model's minimum cacheable length.

Shrink what enters the context

Caching lowers the price of repeated tokens; context management lowers their number. Anthropic's context engineering guide describes the main techniques: compaction (summarize a nearly full context and continue from the summary), clearing old tool results the agent no longer needs, notes kept outside the context window, and sub-agents that explore in their own context and return a short summary.

Tool design has the largest effect. A tool that returns only the fields the agent needs, with pagination and a sensible default limit, is cheaper on every subsequent turn than one that returns a full record. The same applies to tool definitions: Anthropic's post on code execution with MCP describes loading definitions on demand and filtering data in a sandbox before it reaches the model, and gives an example where token use fell from 150,000 to 2,000.

A catalog can also be exposed as a few generic tools instead of one tool per operation. The APIStack MCP endpoint does this with three tools (search_apis, get_api_details, call_api), so the definitions an agent carries stay the same size as the catalog grows.

Sub-agents reduce the lead agent's context but not the total. Anthropic's multi-agent research system post reports that multi-agent systems use about 15 times the tokens of a chat interaction, against about 4 times for a single agent, and concludes that they need tasks valuable enough to pay for it.

Match the model and the mode to the work

Each lever below is documented on the vendors' pricing pages; the right-hand column is what limits it.

LeverEffect on priceConstraint
Prompt cachingRepeated input billed at a fraction of the base ratePrefix must be identical and reused within the cache lifetime
Batch API50% off input and output on both the Claude and OpenAI APIsAsynchronous; Claude batches usually finish within an hour but may take up to 24
Smaller modelLarge: current Claude models are listed from $0.10 to $10 per million input tokensLower capability; needs an evaluation to confirm quality holds
Fewer turnsQuadratic saving on inputRequires better tools or a fixed workflow
Smaller tool resultsSaving repeated on every later turnRequires changing the tool, not the prompt

Batch mode suits work nobody is waiting for: nightly classification, backfills, and evaluation runs. On the Claude API the batch discount and caching discounts stack.

Model routing sends each request to the cheapest model that handles it. The safe way to introduce it is in a workflow step with a narrow job, such as classification or extraction, and to run the same evaluation set on both models before switching. Routing inside a single agent run is harder to justify, because changing the model mid-run discards the cache.

Enforce a budget in code

OWASP lists unbounded consumption among its top ten risks for LLM applications. An agent loop with no ceiling is the common way to meet it: a confused agent retries the same failing tool call until something outside stops it.

Give every run a turn limit and a cost limit, checked after each model response. This guard uses the usage fields the Claude API returns; price holds per-million-token rates.

JavaScript
function createBudget({ maxTurns, maxCostUsd, price }) {
  let turns = 0
  let cost = 0
  return function record(usage) {
    turns += 1
    cost += (usage.input_tokens * price.input
      + (usage.cache_read_input_tokens || 0) * price.cacheRead
      + (usage.cache_creation_input_tokens || 0) * price.cacheWrite
      + usage.output_tokens * price.output) / 1e6
    if (turns >= maxTurns || cost >= maxCostUsd) {
      throw new Error(`Run stopped after ${turns} turns and $${cost.toFixed(2)}`)
    }
    return cost
  }
}

Log the returned cost with the run identifier and the task type. That log is the data for everything in the next section.

Measure cost per successful task

Cost per run flatters an agent that fails cheaply. The number to manage is cost per successful task:

Text
cost per successful task =
  (model spend + tool and API spend + human review time x hourly cost)
  / number of tasks completed correctly

Failed and retried runs stay in the numerator and leave the denominator, so a cheaper model that succeeds less often can cost more per result. Human review belongs in the numerator too. If a person checks every output, the review time is usually larger than the model spend, and reducing the share of outputs that need review is worth more than any token optimization.

The return is then a comparison with what the same task costs without the agent, measured on the same task types. Three rules keep the comparison honest. Measure success with a fixed evaluation set and with outcomes in production, not with the agent's own report. Segment by task type, since an average hides the task that loses money. Re-measure after every price, model or prompt change, because each one moves both the numerator and the success rate.

Summary

  • Agent cost is driven by input tokens resent on every turn; it grows roughly with the square of the number of turns.
  • Cache the stable prefix, keep it byte-identical, and verify cache reads in the usage fields.
  • Reduce tokens at the source: lean tool results, few tool definitions, compaction at deliberate points.
  • Use batch mode for work nobody is waiting for and smaller models for narrow steps, confirmed by an evaluation.
  • Cap every run in code and manage cost per successful task, including retries and human review.

Build with APIStack

One API key for validation, dates and holidays, web checks and reference data, over REST or as MCP tools for your AI agents. The free plan includes 1,000 calls a month.

Keep reading