AI Agent Cost Optimization — Why Agent Bills Grow Quadratically
Agentic AI systems (tool-using, multi-step LLM loops) have fundamentally different cost profiles than simple API calls. Costs grow quadratically with step count. This guide explains why and provides 8 techniques to control agent spending.
Why agent costs are quadratic
In a simple API call, you pay for input + output once. In an agent loop, each step sends the full conversation history (system prompt + all previous steps + tool results) as input. Step 1: S tokens. Step 2: S + R₁ tokens. Step N: S + R₁ + R₂ + … + Rₙ₋₁ tokens. Total input = S×N + R×N(N-1)/2. This is O(N²). A 20-step agent with 1K tokens per step uses ~210K total input tokens — 10× what you might expect from "20 steps × 1K tokens."
Real-world agent cost examples
A coding agent (Claude Code or similar) running 30 steps with a 5K system prompt and 2K average tool output per step: total input ≈ 1.03M tokens. At Claude Sonnet 5.5 pricing ($2.50/M input), that single agent run costs ~$2.58 in input alone, plus ~$3–5 in output. A customer support agent averaging 8 steps costs ~$0.15–0.30 per conversation. Scale to 100K conversations/month and that is $15K–30K/month.
8 techniques to reduce agent costs
1. Cap maximum steps (hard limit). 2. Summarize conversation history every 5–10 steps instead of keeping raw history. 3. Truncate tool outputs — a 50K-token file read should be summarized to 2K. 4. Use a cheaper model for simple tool-use steps (routing). 5. Cache the system prompt (prompt caching — saves 90% on the S×N term). 6. Use batch API for offline agent runs. 7. Pre-filter which tools to include per step (fewer tool definitions = fewer input tokens). 8. Exit early when the task is complete — don't let the agent keep "verifying."
Prompt caching is the #1 agent cost lever
The system prompt (S) is repeated on every step. In a 20-step agent with a 5K-token system prompt, that is 100K tokens just for system prompt repetition. With Anthropic prompt caching: 100K × $0.30/M (cache read) = $0.03 vs 100K × $3.00/M (uncached) = $0.30. That is a 10× saving on the single largest cost component. If you run agents and don't cache, you are leaving money on the table.
Model routing for agents
Not every agent step needs the smartest model. Step types: (1) Tool selection / simple reasoning → GPT-5 mini ($0.25/M). (2) Complex analysis / writing → Claude Sonnet 5.5 ($2.50/M). (3) Final answer synthesis → flagship model. A 20-step agent where 14 steps use the cheap model and 6 use the premium model costs roughly 60% less than running all 20 on the premium model.
Related calculators
Related guides
Frequently Asked Questions
How much does a typical AI agent cost per run?
It varies enormously by step count and model. A simple 5-step agent on GPT-5 mini: ~$0.01–0.03. A complex 30-step coding agent on Claude Sonnet 5: ~$2–8. A research agent that reads multiple web pages and runs 50+ steps: ~$5–20. Use the Agent Cost Calculator to model your specific workflow.
Should I use a framework like LangChain or build my own agent loop?
For cost optimization, a custom loop gives you full control over history management, tool output truncation, and model routing. Frameworks add convenience but often send full untruncated history by default. If using a framework, check how it handles context window management — many don't truncate or summarize, leading to unnecessary cost.
How do I set a cost budget per agent run?
Track cumulative tokens across all steps in real-time. Before each step, check: (cumulative_input + estimated_next_input) × price ≤ budget. If the next step would exceed the budget, force the agent to summarize and return its best answer with current information. Set budget based on task value: $0.10 for support tickets, $1.00 for code review, $5.00 for research tasks. Log budget overflows for analysis.
What is the optimal number of agent steps for cost efficiency?
Diminishing returns set in rapidly. Steps 1–5: highest value per step (planning, key tool calls). Steps 6–10: moderate value (refinement, verification). Steps 11–20: low incremental value in most tasks. Steps 20+: usually indicates the agent is stuck or over-verifying. Set a soft limit at 10 steps with a hard limit at 20. If your agent consistently needs 15+ steps, the system prompt or tool design likely needs improvement.
How much does tool output truncation save?
Tool outputs (file contents, API responses, search results) are often the largest tokens in an agent context. A 50K-token file read that gets truncated to 2K saves 48K tokens — and those savings multiply on every subsequent step. In a 10-step agent, one large untruncated tool output at step 3 adds 48K × 7 = 336K extra tokens across remaining steps. At $3/M (Sonnet 5), that is $1.00 wasted on a single untruncated output.
Can I use different models for different agent steps?
Yes — this is model routing applied to agents. Use a cheap model (GPT-5 mini, $0.25/M) for tool-calling steps that just need parameter extraction. Use a premium model (Claude Sonnet 5, $3/M) for reasoning and final synthesis. A typical agent has 2–3 reasoning steps and 5–8 execution steps. Routing execution to the cheap model saves 60–80% on those steps. The quality impact is minimal because tool-calling steps are formulaic.
How do multi-agent systems affect costs?
Multi-agent architectures (orchestrator + specialist agents) multiply costs because each agent maintains its own context. An orchestrator with 3 specialist agents, each running 5 steps, costs roughly 4× a single 15-step agent due to context duplication and inter-agent communication tokens. Use multi-agent only when genuine parallelism or specialization justifies the cost premium. For most tasks, a single well-designed agent with good tools is cheaper and equally effective.
Guide updated 2026-11-18. Pricing data verified weekly. See methodology · sources.