LIVE · Thursday 3 December

How to Reduce the Cost of AI Agents in Production: Which Lever Fits Interactive, Background and Repeated-Step Agents

Prompt caching, model routing, context pruning and batch each pay off for a different kind of agent, and some cancel each other out. A guide by workload, with the arithmetic on Anthropic's list prices.

By Nick7 min read

Every production agent should start with prompt caching, because agents resend their whole history on every turn and a cache read costs a tenth of normal input on most of Anthropic's models and a twentieth on Opus 5.5 and Sonnet 5.5. After that, the right lever depends on the workload. Interactive agents gain most from a cache that never breaks, so route by session or subagent, never per turn, and trim tool output at the source. Background agents should add the 50% batch discount that Anthropic, OpenAI and Google all offer. A high-volume repeated step is the one place a smaller model clearly pays, as its own subagent with its own cache.

The one thing every agent has in common

An agent's cost is dominated by input it has already sent. Each turn carries the tool definitions, the system prompt and every earlier message and tool result. Anthropic's prompt-caching docs bill the repeated part, when it matches byte for byte, at 0.1 times base input on most models and 0.05 times on Opus 5.5 and Sonnet 5.5. Writes cost 1.25 times base for the 5-minute cache, which is refreshed free whenever it is used, and twice base for the 1-hour cache. OpenAI's guide describes caching that is on by default for supported models, with cached input "discounted up to 95%."

The condition is stability. Anthropic builds the cache in the order tools, system prompt, messages, and a change at one level invalidates everything after it. Editing a tool, changing tool choice, changing thinking or output_config.effort settings, or toggling fast mode all invalidate some or all of the cache, according to the docs' own table. So does a timestamp in the system prompt. Prompts under the minimum length, 512 tokens on Sonnet 5.5 and as high as 4,096 on older models, are not cached at all, "and no error is returned." The usage fields cache_read_input_tokens and cache_creation_input_tokens show whether it is working.

Everything below assumes that is in place.

Comparing the levers

Lever Best fit What it costs you What breaks it, or what it breaks
Prompt caching Every multi-turn agent Write premium of 1.25x or 2x on new tokens Any change to tools, system prompt, effort or model
Per-session or subagent routing Agents with separable subtasks Orchestration work Mid-session switches lose the cache
Tool-output trimming at source Agents reading files, logs, search results Engineering time per tool Nothing, if done before tokens enter context
Tool-result clearing and compaction Long sessions near the context limit Lost detail in summaries Invalidates the cache from the edit point
Batch API Evals, backfills, nightly jobs Up to 24 hours' wait Cache hits become best-effort

If you run an interactive agent

A support agent, a copilot or a coding assistant waits on a person, so batch is out, and the cache is the lever that matters. Keep it warm: Anthropic's 5-minute cache lifetime is measured from each use, so a user taking a turn every few minutes keeps it alive for free.

Routing is where interactive agents most often lose money. JFrog's July 23, 2026 post puts it plainly: "Switching models mid-session invalidates the cache, dropping cached tokens to zero." OpenAI's guide agrees that a different model has "different weights and caching behavior." JFrog's remedy is to make routing a session-level decision, lock the model, system prompt and tools when the session starts, delegate subtasks to subagents that start fresh on a suitable model, summarise before any unavoidable switch, and disable gateway auto-routing inside agent loops.

The price sheet explains why. Claude Sonnet 5.5's cache read costs $0.10 per million tokens; Claude Haiku 5.5's uncached input also costs $0.10 per million. A router that moves a warm Sonnet session to Haiku saves nothing on input that turn. Its saving is on output, $0.50 per million on Haiku against $10 on Sonnet, and on the later turns that stay put.

Trim tool output before it reaches the model: return only needed fields, truncate logs, paginate results. That shrinks every later turn without touching the cache, because the short version is what gets cached. Output is the most expensive token on every model here, so capping reasoning on simple steps makes sense, but set the effort level per session or per subagent: changing output_config.effort turn by turn inside one conversation can invalidate the cached history and pays a cache write each time.

If you run a long-running coding or research agent

Here the context itself becomes the problem. Anthropic's context-management tools offer tool-result clearing, which replaces old tool results with a placeholder, and compaction, which summarises the whole transcript once it passes a threshold (150,000 tokens by default). In one run in Anthropic's March 20, 2026 cookbook, clearing cut a research agent's peak context from 335,279 tokens to 173,137.

Both rewrite history, so both break the cache from that point. The cookbook's advice is to "clear enough tokens to make the cache invalidation worthwhile," which its clear_at_least setting enforces. Compaction also loses specifics: in the cookbook's probe, three of three high-level facts survived and none of three obscure appendix values did. Set thresholds high and clear in large chunks.

For coding agents specifically, Parsec offers a context layer for Claude Code, Codex and opencode that trims unused tool output and thinking before requests reach the provider; its Pro tier is free to start, according to its site. In Parsec's own benchmark, run on 100 SWE-bench Verified tasks with Claude Sonnet 4.6 and cache-aware pricing, the layer cost about 39% less than the uncompressed baseline and solved 62 tasks to the baseline's 57. That is Parsec's benchmark, not an independent one, it covers coding agents only, and while the harness is public and can be re-run, no independent re-run has been published.

If you run a background agent

Anything not waiting on a person belongs in a batch API. Anthropic's docs say "all usage is charged at 50 percent of the standard API prices," that most batches finish within an hour, and that a batch expires if not complete in 24. OpenAI's batches complete within 24 hours at a 50% discount; Google's Gemini Batch API targets 24 hours at 50% of standard cost.

Caching still applies: Anthropic says the two discounts stack. But because batch requests are processed asynchronously and concurrently, cache hits are "best-effort," with typical hit rates of 30 to 98%. Anthropic recommends the 1-hour cache for batches that share context, and identical cache_control blocks across every request.

If you have a high-volume repeated step

Some agents call a model over and over for the same narrow job: classify a ticket, extract fields, pick a tool. That is where a small model clearly pays, provided it runs as its own subagent or pipeline step with its own stable prompt and cache, not as a mid-conversation switch. On Anthropic's prices, Haiku 5.5's cache read is $0.01 per million tokens and its output $0.50, against $0.10 and $10 for Sonnet 5.5.

The open question is quality. Nobody has published cost, latency and success figures for moving an agent's repeated steps from a frontier model to a small post-trained model on real agent traces, or how many traces such a model needs. Score the small model on a held-out sample of the step's real inputs, and count retries in the cost.

A worked example

Take one interactive session with these assumptions: 20 turns, a 10,000-token prefix of tools and system prompt, history growing by 2,000 tokens a turn, 500 output tokens a turn, all turns within the 5-minute cache window, and Anthropic's list prices for Sonnet 5.5 ($2 input, $2.50 cache write, $0.10 cache read, $10 output per million) and Haiku 5.5 for prompts up to 100,000 tokens ($0.10, $0.125, $0.01, $0.50).

  • The session sends 580,000 input tokens and receives 10,000 output.
  • Sonnet 5.5, no caching: $1.16 of input plus $0.10 of output, $1.26.
  • Sonnet 5.5, cached: 532,000 tokens read ($0.053), 48,000 written ($0.12), $0.10 of output: about $0.27, 78% less.
  • One routing bounce: if a router sends some turns to Haiku and Sonnet's cache expires, Sonnet's return at turn 15 re-reads its 38,000-token prompt at the write price, about $0.095 instead of $0.0086 for a warm turn. That one turn adds roughly a third to the cached session's cost.
  • Whole session on Haiku 5.5, cached: about $0.016, if Haiku completes the task.
  • Sonnet 5.5, cached, run as a batch: about $0.14, if the cache hits hold.

The difference between the last two lines and the rest is the argument for routing by session: choose the cheapest model that completes the task, then let it keep its cache.

What these numbers do not show

They are list-price arithmetic, not measurements. No public benchmark has replayed one set of agent traces through each of these levers separately and stacked, reporting cost per completed task, cache hit rate and task success together, so the combined savings quoted in vendor posts have not been measured independently. Parsec says on its routing comparison page that a shared K/V cache avoids the mid-conversation switching penalty for models it serves, but the page defines no workload or baseline, and no one has published an independent test of cross-model cache transfer.

Bottom line

Interactive agent: cache, freeze configuration per session, route by session or subagent, trim tool output at the source. Long-running coding or research agent: add tool-result clearing or compaction with high thresholds, and accept the cache write each time. Background agent: batch, with the 1-hour cache. High-volume repeated step: test a small model as its own subagent and judge it on cost per completed task. In every case, measure on your own traffic before trusting a percentage.

Sources

More in Chips & Compute

All Chips & Compute →