The Real Cost Breakdown
Before optimizing, you need to understand where tokens actually go. Here's the breakdown from our ZOO production system (10 agents, ~2,400 calls/day):
| Component | % of Tokens | Monthly Cost | Optimization Potential |
|---|---|---|---|
| System prompts | 35% | $420 | HIGH |
| Conversation history | 28% | $336 | HIGH |
| Tool definitions | 15% | $180 | MEDIUM |
| Tool outputs | 12% | $144 | MEDIUM |
| User messages | 7% | $84 | LOW |
| Model overhead | 3% | $36 | LOW |
Key insight: 63% of your costs come from system prompts and conversation history — things you control completely.
Strategy #1: Prompt Compression (Save 30-50%)
The average system prompt we see in production is 1,200-2,000 tokens. It can almost always be reduced to 400-600 tokens without losing behavior.
Before: The Bloated Prompt (1,450 tokens)
You are an expert AI assistant named Felix working for ZOO Technologies.
You are helpful, friendly, and knowledgeable. You should always be polite
and professional. When responding to users, you should provide detailed
and thorough answers. You have access to various tools including web
search, file reading, code execution, and database queries. You should
always think step by step before answering. If you don't know something,
you should say so. You should never make up information...
After: The Compressed Prompt (380 tokens)
[Felix — ZOO AI Agent]
Tools: web_search, file_read, code_exec, db_query
Rules: Think step-by-step. Be concise. Use markdown. Cite sources.
Ask if unclear. Admit unknowns. Log actions. Prioritize accuracy.
That's a 74% reduction in system prompt tokens. Same behavior. Same quality.
Strategy #2: Intelligent Model Routing (Save 40-60%)
Not every task needs GPT-4. Most agent tasks are actually simple:
| Task Type | Appropriate Model | Cost Ratio |
|---|---|---|
| Classification | GPT-4o-mini | 1x |
| Simple extraction | GPT-4o-mini | 1x |
| Summarization | GPT-4o | 3-5x |
| Complex reasoning | GPT-4o | 3-5x |
| Code generation | GPT-4o | 3-5x |
The insight: 60-70% of agent sub-tasks can run on mini models. But most developers use one model for everything.
Strategy #3: Semantic Caching (Save 50-70%)
Agents often process similar requests. A semantic cache stores responses keyed by meaning, not exact text.
User: "What's the weather in NYC?" → Cache MISS → Call API → Store
User: "How's the weather in New York?" → Cache HIT → Return stored
User: "NYC weather forecast" → Cache HIT → Return stored
At ZOO, our semantic cache achieves a 34% hit rate on production traffic, saving ~$1,600/month.
Strategy #4: Token Budget Enforcement (Save 20-35%)
Set hard limits per agent, per task, and per conversation. When the budget is hit, the agent must summarize and continue or escalate.
budget = TokenBudget(
max_tokens_per_conversation=30000,
max_tokens_per_day=200000,
max_cost_per_day=5.0
)
# Each call checks budget before executing
allowed, action, msg = budget.check_budget(estimated_tokens)
if not allowed:
return f"Budget limit reached: {msg}"
Strategy #5: Agent Decomposition (Save 30-50%)
Instead of one agent doing everything (with a massive prompt and all tools loaded), decompose into specialized sub-agents:
BEFORE: Agent "Felix" — 2000 token prompt, 15 tools, GPT-4o always
Cost per task: $0.015-0.045
AFTER: Router Agent — 200 tokens, 1 tool, GPT-4o-mini
→ Research Agent — 400 tokens, 3 tools, GPT-4o-mini
→ Writing Agent — 300 tokens, 2 tools, GPT-4o
→ Code Agent — 350 tokens, 4 tools, GPT-4o
Average cost per task: $0.004-0.012 (60-70% reduction)
Strategy #6: Output Streaming & Truncation (Save 15-25%)
Set maximum output tokens per task type. A classification task doesn't need 2000 tokens of output:
MAX_OUTPUT_TOKENS = {
"classification": 100,
"extraction": 500,
"summarization": 1000,
"qa": 1500,
"code_generation": 4000,
}
Strategy #7: Batch Processing (Save 40-60%)
Instead of processing items one-by-one, batch them into single API calls:
# 10 individual calls: 10 × (200 + 150 overhead) = 3,500 tokens
# 1 batch call: 150 + 10×200 + 100 formatting = 2,250 tokens
# Savings: 36% on overhead alone
Real Results: Before & After
Here's the actual data from our ZOO production system after implementing all 7 strategies:
| Metric | Before | After | Change |
|---|---|---|---|
| API calls/day | 2,400 | 1,680 | -30% |
| Avg tokens/call | 3,200 | 1,400 | -56% |
| Daily token usage | 7.68M | 2.35M | -70% |
| Daily cost | $19.20 | $3.53 | -82% |
| Cache hit rate | 0% | 34% | — |
| GPT-4o usage | 100% | 28% | -72% |
| Monthly cost (10 agents) | $5,760 | $1,058 | -82% |
Quality Metrics (No Degradation)
| Metric | Before | After |
|---|---|---|
| Task completion rate | 94.2% | 95.1% |
| User satisfaction | 4.3/5 | 4.4/5 |
| Avg response time | 2.1s | 1.4s |
| Error rate | 3.8% | 2.1% |
Key insight: Quality actually improved because forced compression made prompts clearer, and model routing ensured the right model for each task.
Frequently Asked Questions
Will these optimizations affect agent quality?
No — if done correctly. Prompt compression removes fluff, not behavior. Model routing uses the right model for each task. Our data shows quality stayed the same or improved.
Which strategy should I implement first?
Start with prompt compression (biggest impact, zero risk) and model routing (high impact, low risk). Then add caching and budget enforcement.
How much can I realistically save?
For most agents: 60-80%. For agents with heavy repetitive workloads: up to 90%.
How do I monitor costs in production?
Use the TokenBudget class above. Log every call with tokens and cost. Set up alerts at 50%, 75%, and 90% of daily budget. Review weekly.