AI Agent Cost Optimizer
Reduce AI agent spend without reducing shipped quality. Optimize for cost per correct completed task, not raw token count.
When to Activate
- AI coding bills, token usage, API inference cost, or model spend are too high
- Reviewing agent workflows for context waste, retry loops, or cache misses
- Choosing model tiers for planning, implementation, review, or cleanup tasks
- Evaluating claims about prompt caching, cheap models, or token savings
- Capturing repeated workflows so future agents do not rediscover the same context
Core Principle
Price the model and context to the cost of failure.
Use more capable models when a wrong answer could create expensive rework, security risk, data loss, or architectural drag. Use cheaper models and smaller context when the task is bounded, reversible, test-covered, or mechanical.
Do not save tokens in ways that increase retries, hide important evidence, or lower code quality.
Cost Audit Workflow
1. Establish the Spend Shape
Identify what is actually driving cost before recommending changes:
- API versus subscription spend
- Providers and models in use
- Workflows that run most often
- Average input, output, cached input, reasoning, and tool-call tokens when available
- Retry count, failed runs, and human correction time
- Whether costs come from a few large workflows or many small calls
If usage data is unavailable, mark it unknown and inspect local configs, logs, scripts, or provider dashboards only when accessible.
2. Separate Verified Facts From Claims
Pricing, model availability, cache behavior, and benchmark rankings are date-sensitive. Verify current provider documentation before citing specific prices or recommending a model as the default.
Treat social posts, benchmark screenshots, and vendor marketing as hypotheses. Convert them into local tests before changing default routing.
3. Find Token Leaks
Look for these common waste patterns:
- Large files or directories loaded before a focused task is understood
- "Just in case" file includes
- Raw logs, build output, search output, or test output kept after the useful signal is extracted
- Tool loops that resend the same context each retry
- Repeated tool definitions or provider instructions that could be cached
- Long conversations continued after a compact summary would preserve enough state
- Premium models used for formatting, renames, lint fixes, extraction, or boilerplate
- Many small questions asked as separate calls against the same prefix
- Repeated environment discovery that should be captured as a skill, reference, or script
4. Reduce Context Before Switching Models
Fix context shape first:
- Search for symbols, paths, and errors before opening files
- Read the smallest relevant slice that can answer the question
- Keep stable rules, schemas, and workflow instructions in durable skills or references
- Summarize old tool outputs once their purpose is served
- Drop stale context after phase changes
- Batch read-only exploration when the questions share the same context
- Preserve exact evidence for critical debugging, security, billing, or data-loss cases
5. Make Cache Hits Likely
Design stable prefixes:
- Put stable instructions, tool definitions, schemas, and examples before variable task data
- Keep timestamps, session IDs, user-specific details, and retrieved snippets out of the reusable prefix
- Avoid rewriting stable instructions between calls
- Monitor cache-read and cache-write usage when provider responses expose it
- Check provider-specific thresholds, TTLs, and invalidation rules before assuming caching works
Do not generalize that streaming breaks caching. Cache misses usually come from unstable prefixes, unsupported providers or models, thresholds, TTL expiry, or changed tool/message structure.
6. Route by Task Risk
Use this rubric as the starting point:
| Task |
Default Tier |
Promote When |
| Architecture, security-critical review, irreversible operations, high-blast-radius refactors |
Premium |
Already premium; add review or tests |
| Feature implementation, debugging, test writing, routine refactors |
Workhorse |
Failing repeatedly, weak tests, or hidden architecture risk |
| Lint, format, rename, simple extraction, boilerplate, classification |
Utility or local |
Output needs nuanced judgment or affects production behavior |
| Long agentic loops |
Workhorse with strict context budgets |
Loop reaches unclear design decisions or persistent failures |
| Exploration across unknown codebases |
Workhorse or premium for initial map, then cheaper tier for bounded follow-up |
Architecture is unclear or stakes are high |
Use local evaluation over broad leaderboard claims. A cheaper model that doubles retries may be more expensive than the premium model.
7. Batch and Partition Deliberately
Batch related small questions into one call when they share context. Partition broad tasks into independent subtasks when each subtask can run with isolated context and return a compact result.
Avoid spawning extra agents just to parallelize trivial work. Multi-agent systems can increase token spend unless context isolation or wall-clock savings justify the overhead.
8. Capture Repeated Workflows
When a workflow is repeated and stable, capture it so agents stop rediscovering it:
- Write a focused skill for multi-step procedural knowledge
- Move long background material into references loaded only when needed
- Create deterministic scripts for fragile or repeated transformations
- Keep environment facts in repo memory or project docs with verification dates
Capture the final working workflow, not the exploratory path that found it.
9. Measure the Result
Compare before and after using task-level metrics:
- Cost per completed task
- Input, output, cached input, and tool-call tokens
- Cache hit rate
- Retry count
- Wall-clock time
- Test pass rate or review defect rate
- Human correction time
Report savings as measured ranges. Avoid unsupported claims like "80% reduction" unless the baseline and after-state are both known.
Output Format
When auditing a workflow, return:
- Top Leaks - Ordered by likely spend impact
- Low-Risk Fixes - Changes that reduce cost without quality risk
- Routing Changes - Model-tier changes and promotion conditions
- Caching Plan - Stable prefix, variable suffix, and metrics to watch
- Capture Candidates - Workflows worth turning into skills, references, or scripts
- Unverified Claims - Pricing or model-quality claims that need current source checks
Guardrails
- Do not recommend a specific provider or model as default without current pricing and local quality evidence
- Do not remove context required for correctness, safety, compliance, or auditability
- Do not optimize mechanical token count if it increases retries or review burden
- Do not treat prompt caching as magic; validate cache hits in usage data
- Do not encode volatile price tables as enduring skill content
- Do not use editing shortcuts that violate the repository's established workflow
Integration
Related skills:
context-fundamentals - Understand what consumes context
context-optimization - Apply compaction, masking, caching, and partitioning
multi-agent-patterns - Evaluate when sub-agent context isolation is worth the overhead
tool-design - Reduce tool ambiguity and response bloat
skill-capture - Persist repeated workflows as reusable skills
1---2name: ai-agent-cost-optimizer3description: Audit and reduce AI agent token and inference spend through context discipline, prompt caching, model routing, batching, and workflow capture. Use when discussing AI coding bills, token waste, model selection, prompt caching, or agent cost optimization.4---5
6# AI Agent Cost Optimizer
7
8Reduce AI agent spend without reducing shipped quality. Optimize for cost per correct completed task, not raw token count.
9
10## When to Activate
11
12- AI coding bills, token usage, API inference cost, or model spend are too high
13- Reviewing agent workflows for context waste, retry loops, or cache misses
14- Choosing model tiers for planning, implementation, review, or cleanup tasks
15- Evaluating claims about prompt caching, cheap models, or token savings
16- Capturing repeated workflows so future agents do not rediscover the same context
17
18## Core Principle
19
20Price the model and context to the cost of failure.
21
22Use more capable models when a wrong answer could create expensive rework, security risk, data loss, or architectural drag. Use cheaper models and smaller context when the task is bounded, reversible, test-covered, or mechanical.
23
24Do not save tokens in ways that increase retries, hide important evidence, or lower code quality.
25
26## Cost Audit Workflow
27
28### 1. Establish the Spend Shape
29
30Identify what is actually driving cost before recommending changes:
31
32- API versus subscription spend
33- Providers and models in use
34- Workflows that run most often
35- Average input, output, cached input, reasoning, and tool-call tokens when available
36- Retry count, failed runs, and human correction time
37- Whether costs come from a few large workflows or many small calls
38
39If usage data is unavailable, mark it unknown and inspect local configs, logs, scripts, or provider dashboards only when accessible.
40
41### 2. Separate Verified Facts From Claims
42
43Pricing, model availability, cache behavior, and benchmark rankings are date-sensitive. Verify current provider documentation before citing specific prices or recommending a model as the default.
44
45Treat social posts, benchmark screenshots, and vendor marketing as hypotheses. Convert them into local tests before changing default routing.
46
47### 3. Find Token Leaks
48
49Look for these common waste patterns:
50
51- Large files or directories loaded before a focused task is understood
52- "Just in case" file includes
53- Raw logs, build output, search output, or test output kept after the useful signal is extracted
54- Tool loops that resend the same context each retry
55- Repeated tool definitions or provider instructions that could be cached
56- Long conversations continued after a compact summary would preserve enough state
57- Premium models used for formatting, renames, lint fixes, extraction, or boilerplate
58- Many small questions asked as separate calls against the same prefix
59- Repeated environment discovery that should be captured as a skill, reference, or script
60
61### 4. Reduce Context Before Switching Models
62
63Fix context shape first:
64
65- Search for symbols, paths, and errors before opening files
66- Read the smallest relevant slice that can answer the question
67- Keep stable rules, schemas, and workflow instructions in durable skills or references
68- Summarize old tool outputs once their purpose is served
69- Drop stale context after phase changes
70- Batch read-only exploration when the questions share the same context
71- Preserve exact evidence for critical debugging, security, billing, or data-loss cases
72
73### 5. Make Cache Hits Likely
74
75Design stable prefixes:
76
77- Put stable instructions, tool definitions, schemas, and examples before variable task data
78- Keep timestamps, session IDs, user-specific details, and retrieved snippets out of the reusable prefix
79- Avoid rewriting stable instructions between calls
80- Monitor cache-read and cache-write usage when provider responses expose it
81- Check provider-specific thresholds, TTLs, and invalidation rules before assuming caching works
82
83Do not generalize that streaming breaks caching. Cache misses usually come from unstable prefixes, unsupported providers or models, thresholds, TTL expiry, or changed tool/message structure.
84
85### 6. Route by Task Risk
86
87Use this rubric as the starting point:
88
89| Task | Default Tier | Promote When |
90|------|--------------|--------------|
91| Architecture, security-critical review, irreversible operations, high-blast-radius refactors | Premium | Already premium; add review or tests |
92| Feature implementation, debugging, test writing, routine refactors | Workhorse | Failing repeatedly, weak tests, or hidden architecture risk |
93| Lint, format, rename, simple extraction, boilerplate, classification | Utility or local | Output needs nuanced judgment or affects production behavior |
94| Long agentic loops | Workhorse with strict context budgets | Loop reaches unclear design decisions or persistent failures |
95| Exploration across unknown codebases | Workhorse or premium for initial map, then cheaper tier for bounded follow-up | Architecture is unclear or stakes are high |
96
97Use local evaluation over broad leaderboard claims. A cheaper model that doubles retries may be more expensive than the premium model.
98
99### 7. Batch and Partition Deliberately
100
101Batch related small questions into one call when they share context. Partition broad tasks into independent subtasks when each subtask can run with isolated context and return a compact result.
102
103Avoid spawning extra agents just to parallelize trivial work. Multi-agent systems can increase token spend unless context isolation or wall-clock savings justify the overhead.
104
105### 8. Capture Repeated Workflows
106
107When a workflow is repeated and stable, capture it so agents stop rediscovering it:
108
109- Write a focused skill for multi-step procedural knowledge
110- Move long background material into references loaded only when needed
111- Create deterministic scripts for fragile or repeated transformations
112- Keep environment facts in repo memory or project docs with verification dates
113
114Capture the final working workflow, not the exploratory path that found it.
115
116### 9. Measure the Result
117
118Compare before and after using task-level metrics:
119
120- Cost per completed task
121- Input, output, cached input, and tool-call tokens
122- Cache hit rate
123- Retry count
124- Wall-clock time
125- Test pass rate or review defect rate
126- Human correction time
127
128Report savings as measured ranges. Avoid unsupported claims like "80% reduction" unless the baseline and after-state are both known.
129
130## Output Format
131
132When auditing a workflow, return:
133
1341. **Top Leaks** - Ordered by likely spend impact
1352. **Low-Risk Fixes** - Changes that reduce cost without quality risk
1363. **Routing Changes** - Model-tier changes and promotion conditions
1374. **Caching Plan** - Stable prefix, variable suffix, and metrics to watch
1385. **Capture Candidates** - Workflows worth turning into skills, references, or scripts
1396. **Unverified Claims** - Pricing or model-quality claims that need current source checks
140
141## Guardrails
142
143- Do not recommend a specific provider or model as default without current pricing and local quality evidence
144- Do not remove context required for correctness, safety, compliance, or auditability
145- Do not optimize mechanical token count if it increases retries or review burden
146- Do not treat prompt caching as magic; validate cache hits in usage data
147- Do not encode volatile price tables as enduring skill content
148- Do not use editing shortcuts that violate the repository's established workflow
149
150## Integration
151
152Related skills:
153
154- `context-fundamentals` - Understand what consumes context
155- `context-optimization` - Apply compaction, masking, caching, and partitioning
156- `multi-agent-patterns` - Evaluate when sub-agent context isolation is worth the overhead
157- `tool-design` - Reduce tool ambiguity and response bloat
158- `skill-capture` - Persist repeated workflows as reusable skills