Agentbrisk

AI Agent Cost Optimization: How to Cut Costs Without Killing Performance

April 8, 2026 · Editorial Team · 9 min read · cost-optimizationai-deploymentproduction

AI agents in production cost more than most teams expect. The demo runs fine on a free tier. The production deployment serving real users at real volume arrives with a bill that requires a budget meeting. This happens because demos are optimized to look impressive. Production is optimized for nothing until someone looks at the numbers.

The good news is that the cost gap between "naive implementation" and "thoughtful implementation" is often 60-80%. The strategies below are not hypothetical, they're in production at teams running agents at scale. None of them require sacrificing answer quality in any meaningful way.


Start with model selection, because it moves the needle most

The single biggest lever in agent cost is which model you call. The price difference between frontier models and mid-tier models is not incremental, it's an order of magnitude.

As of May 2026, here's how the major models compare on cost:

ModelInput (per 1M tokens)Output (per 1M tokens)
Claude 4 Opus~$15~$75
Claude 3.7 Sonnet~$3~$15
Claude 3.5 Haiku~$0.80~$4
GPT-5~$10~$30
GPT-5 nano~$0.15~$0.60
Gemini 2.5 Pro~$7~$21
Llama 4 (self-hosted)~$0 (compute only)~$0 (compute only)

The right question is not "which model is best?" It's "which model is good enough for this specific subtask?"

Claude 4 Opus and GPT-5 are genuinely better for complex reasoning, ambiguous instructions, and tasks where the right answer matters a lot. They're not 10x better at every task. For tasks like parsing structured data, classifying text, formatting output, or summarizing a well-defined chunk of content, Claude 3.5 Haiku or GPT-5 nano can perform at 95% of the quality for 5% of the cost.

The practical implementation of this is a routing layer. Before calling any model, classify the incoming task by complexity and route it accordingly. Simple, deterministic tasks go to a cheap model. Complex, open-ended tasks go to the frontier model. This alone typically cuts model costs by 40-60% for agents that handle varied workloads.


Prompt caching: the fastest win for high-volume agents

If your agent sends the same system prompt, the same long context, or the same documents on every request, you're paying to re-process that content every single time. Prompt caching makes you pay only once.

Anthropic's prompt caching allows you to mark portions of your input (system prompts, document context, tool definitions) as cacheable. When the same content appears in subsequent requests, Anthropic charges the cached input token price instead of the full input price. The discount is significant: cached tokens cost about 10% of uncached tokens on Anthropic's API.

The implementation is straightforward. You add a cache_control parameter to the relevant parts of your messages array:

response = client.messages.create(
    model="claude-3-7-sonnet-20250219",
    max_tokens=1024,
    system=[
        {
            "type": "text",
            "text": your_long_system_prompt,
            "cache_control": {"type": "ephemeral"}
        }
    ],
    messages=conversation_history
)

For an agent that sends a 2,000-token system prompt on every request, prompt caching drops the effective cost of that system prompt from ~$6 per 1M calls to ~$0.60 per 1M calls. At 100,000 requests a day, that's a meaningful monthly saving.

OpenAI has a similar feature. For GPT-5 and supported models, prompt caching happens automatically for repeated prefixes above 1,024 tokens, you don't need to explicitly mark content as cacheable. The discount is roughly 50% on cached input tokens. This means if you're consistent about keeping your system prompt first and unchanged across requests, you get automatic caching without any code changes.

The most common mistake: changing the system prompt or document context between requests when you don't need to. Even small changes to the prompt prefix break the cache. Keep stable content at the top, session-specific content at the bottom.


Batch APIs for non-real-time workloads

Not every agent task needs to complete in under a second. A surprising amount of agent work is asynchronous: analyzing documents overnight, generating reports on a schedule, processing a queue of customer requests, running evaluations on a test set.

For these workloads, batch APIs dramatically reduce cost.

Anthropic's Message Batches API processes requests asynchronously and returns results within 24 hours. The cost is 50% of the standard API price. You submit a batch of up to 10,000 requests, check back for results, and pay half the per-token rate. For bulk document processing, evaluation runs, or any overnight processing task, this is an easy cost cut.

OpenAI's Batch API works similarly: 50% discount for requests processed within 24 hours, with a 100K request per batch limit.

The implementation pattern is to separate your agent's workloads into synchronous (user is waiting) and asynchronous (background processing). Route everything asynchronous to the batch API. An agent workflow that runs nightly to summarize the day's data, or that processes uploaded documents before a user's next session, is a good batch candidate.


Context window management reduces token costs per request

The context window guide covers how context windows work technically. The cost angle is this: input token costs are per token, so a bloated context window is a direct cost driver.

An agent session that runs for an hour and accumulates 800K tokens of conversation history in context is not cost-neutral. If that context is passed to the model on every tool call, you're paying for 800K input tokens on each of those calls. A long coding session with a naive context implementation can cost $30-50 in API calls from context size alone.

Practical strategies:

Summarize and compact. When your context reaches a threshold, have the agent produce a concise summary of what's happened so far: the goal, the key decisions, the current state. Replace the full history with the summary. Claude Code does this automatically. If you're building a custom agent, implement it explicitly.

Use retrieval instead of loading. Don't load every document into context at the start of a session. Load the system prompt and task. Let the agent retrieve specific documents via tool calls when it needs them. This keeps the working context tight and only expands it when there's a genuine need. See the RAG guide for the retrieval architecture.

Trim tool outputs. Agents often receive verbose tool results (full API responses, long document extracts) that contain far more than the agent actually needs. Preprocess tool outputs to extract the relevant data before passing them back into context. A web page that returns 50,000 tokens of HTML probably contains 500 tokens of relevant content.


Hybrid local and cloud: where self-hosting pays off

Llama 4 and other open-weight models have reached a quality level where they're genuine options for production workloads, not just for local experimentation. The cost model is different: instead of paying per token, you pay for GPU compute.

The crossover point depends on your volume. At low request volume, cloud APIs are cheaper because you're not paying for idle compute. At high volume, the per-token cost of cloud APIs exceeds the cost of running your own GPU instances.

A rough rule of thumb: if you're spending more than $5,000/month on API calls for a stable workload, it's worth modeling whether a self-hosted deployment pays off. The break-even varies, but teams running serious production loads often find that self-hosting mid-tier models for the high-volume tasks cuts total spending by 50-70%.

The practical hybrid architecture: use cloud APIs (Claude, GPT-5) for complex reasoning tasks where model quality matters. Use a self-hosted Llama 4 deployment for high-volume, lower-complexity tasks like classification, extraction, and formatting. The routing logic is similar to the model selection routing described above, just with a different decision about cloud vs. local rather than Opus vs. Haiku.

Tools like Ollama and vLLM are the most commonly used local inference servers. Both handle Llama 4 well. vLLM has better throughput for high-concurrency production workloads. Ollama is easier to get running quickly for development and lower-traffic deployments.


RAG as a cost management tool

This is often missed: RAG isn't just about giving agents access to large document corpora. It's also a cost control mechanism.

Without RAG, giving an agent access to your company's documentation means loading that documentation into context. If the documentation is 2 million tokens and you're paying $3 per million input tokens, that's $6 per request just for the context. With RAG, the same agent can access the same documentation by retrieving only the relevant chunks, maybe 5,000 tokens per request instead of 2 million.

At scale, this difference is extreme. A support agent handling 10,000 conversations per day against a 2M token knowledge base:

  • Without RAG: $60,000/day in input tokens
  • With RAG (top-5 chunks, ~5K tokens): $150/day

These are real numbers. The RAG guide covers the implementation. The point here is that RAG justifies its architectural complexity in part on cost grounds, not just on capability grounds.


Output token costs: write shorter prompts, get shorter outputs

Output tokens typically cost 5-10x more than input tokens. An agent that produces verbose responses, explanations of its reasoning, lengthy preambles, repeated context, burns output tokens unnecessarily.

Prompt engineering for cost: instruct the model explicitly to be concise. Tell it not to repeat back what you asked. Tell it to output only the final answer, not the chain of thought, unless you actually need the chain of thought for debugging. For structured data extraction, specify that you want only the JSON output with no surrounding prose.

# Instead of:
"Analyze this customer feedback and tell me what the main issues are."

# Write:
"List the main issues in this customer feedback. Return JSON only:
{'issues': ['...', '...']}"

For agents that use tools in a loop, each tool call generates output tokens for the function call plus input tokens for the tool result. Minimize the number of tool calls per task by combining operations where possible and avoiding tool calls for information the model already has in context.


Monitoring: you can't optimize what you don't measure

None of the above matters if you're flying blind on costs. Before optimizing, instrument your agent to track:

  • Total tokens per session (input and output separately)
  • Model called per request
  • Cache hit rate for prompt caching
  • Cost per task type (classification, extraction, generation, etc.)
  • P95 token count per session (outlier sessions are often the main cost driver)

Most cloud providers expose this via their usage dashboards. For more detailed per-request tracking, logging the usage field from every API response into your own database gives you the granularity to identify which workflows are expensive and why.

The pattern I see most in teams that have gotten serious about agent costs: they start with rough total spend visibility, identify the top-3 cost drivers, optimize those specifically, and only then look at further marginal improvements. The 80/20 principle applies, a few specific patterns (large context windows, no prompt caching, routing everything to frontier models) account for most of the overspend.


A checklist for cost review

Before shipping an agent to production or before reviewing an existing agent's costs, check these items:

  • Is every call going to the most expensive model available, or do you have routing based on task complexity?
  • Are stable inputs (system prompts, document context) using prompt caching?
  • Are asynchronous workloads using batch APIs?
  • Are long sessions compacting or summarizing context instead of accumulating it unbounded?
  • Are tool outputs preprocessed to return only relevant data?
  • Is the agent being prompted to produce concise outputs where verbosity isn't needed?
  • Have you modeled whether self-hosted inference is cost-effective at your current volume?

Getting through that list typically gets costs down to a fraction of what a naive implementation produces. The implementation work is not trivial, but it's also not research, these are patterns that work and can be applied systematically.

Search