AI agents are powerful — and surprisingly easy to make expensive. Unlike a chatbot you ping occasionally, agents run loops, call tools, spawn sub-agents, and can rack up serious API costs before you realize what's happening. Here's how to keep that under control.
Why Agents Cost More Than You Expect
A single agent loop typically involves:
- Input tokens: your system prompt + full conversation history + tool schemas
- Output tokens: the model's response + any tool calls
- Tool call results: often pumped back into the next loop as more input
In a long-running agent with a big system prompt, 80%+ of your spend can be on input tokens — context you're paying to resend every single loop. Multiply that by 50 loops, and even a "cheap" model gets expensive fast.
The Big Levers
1. Choose the Right Model for the Job
Not every task needs GPT-4 or Claude Opus. Routing matters enormously.
| Task | Recommended Tier | |------|------------------| | Simple classification, routing, parsing | Small model (Haiku, Gemini Flash, Llama 8B) | | Most tool use, reasoning, writing | Mid-tier (Sonnet, GPT-4o mini) | | Complex multi-step reasoning | Top-tier (Opus, GPT-4o) — use sparingly |
The pattern: use the smallest model that reliably completes the task. Test by intentionally downgrading and checking failure rate. Often you'll find mid-tier handles 90% of cases.
2. Trim Your System Prompt
Your system prompt is paid on every request. A 3,000-token system prompt running 100 loops = 300,000 tokens of input just for the instructions.
Audit ruthlessly:
- Remove anything the model doesn't actually need
- Cut examples if they're not earning their keep
- Move reference material to a retrieval tool instead of pasting it inline
Target: keep your base system prompt under 800 tokens if possible. Every 500 tokens you cut = real money back over time.
3. Compress or Truncate History
Most agents naively append every message to history. This is expensive and often unnecessary.
Better approaches:
- Rolling window: only keep the last N turns in context
- Summarization: periodically summarize old history into a compact paragraph and drop the raw messages
- Task-scoped context: for sub-agents, only pass what's relevant to their specific job — not the full parent conversation
4. Cache What Doesn't Change
Many providers (Anthropic, OpenAI) offer prompt caching. If your system prompt is large and static, caching can cut input costs by 80-90% on cached portions.
Set it up once and let it run. Biggest ROI-per-hour improvement most agent builders skip.
5. Rate Limit Your Loops
Agents without loop controls can spin out of control. Always implement:
- Max iterations: hard cap on how many loops an agent can run per task (e.g., 20)
- Max tokens per session: budget ceiling before the agent reports back instead of continuing
- Timeouts: wall-clock time limit so a stuck agent doesn't idle and rack up cost
MAX_LOOPS = 20
loop_count = 0
while not task_complete:
if loop_count >= MAX_LOOPS:
return "Max iterations reached — check in with human"
# ...
loop_count += 1This single pattern prevents most runaway-cost incidents.
6. Audit Your Tool Call Volume
Each tool call that returns a large payload gets added to your context. Watch for:
- Search tools returning 5,000-word documents when you needed a paragraph
- API tools returning full JSON responses with 80% irrelevant fields
- Loop tools that recursively call more tools
Fix: make your tools return summaries or relevant excerpts, not full payloads. A search tool that returns 3 bullet points costs a fraction of one that returns the full article.
A Practical Cost Audit
Once a week, pull your API dashboard and ask:
- What's my average input/output ratio? If input >> output, you're paying mostly for context — look at prompt compression first.
- What models am I using? Are any expensive models being used for simple tasks?
- Any spike days? A cost spike usually means a loop ran away or a prompt suddenly got big.
- Cost per task completed? Track this over time. Downward trend = you're optimizing. Flat or up = investigate.
Budget Guardrails by Use Case
| Use Case | Reasonable Cost Target | |----------|----------------------| | Simple automation (daily summary, triage) | < $0.02/run | | Research agent (web search, multi-step) | < $0.20/run | | Complex multi-agent workflow | < $1.00/run | | Anything over $2/run | Needs architectural review |
These aren't universal — adjust for your volume and business value. But if a simple task costs $0.50, something is wrong.
Quick Wins Checklist
- [ ] System prompt under 1,000 tokens
- [ ] Max iterations set on all agent loops
- [ ] Model routing in place (not everything on the expensive model)
- [ ] Prompt caching enabled if your provider supports it
- [ ] History truncation or summarization implemented
- [ ] Tool responses summarized before being added to context
- [ ] Weekly cost audit on the calendar
Want Battle-Tested Configs?
At Ask Patrick (askpatrick.co), the Library includes agent configurations already optimized for cost — with system prompts kept lean, tool schemas that return clean structured data, and routing patterns that use expensive models only when needed. If you're building serious agent systems, these templates save you a lot of trial and error.
Want the full playbook?
Get copy-paste AI templates, prompt frameworks, and agent patterns — all in one place.
Get Access — It’s FreeNo credit card. No fluff. Just the good stuff.