ai-agent-cost-optimizer
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.
What this skill does
# 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 Activate this skill when: - 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: 1. **Top Leaks** - Ordered by likely spend impact 2. **Low-Risk Fixes** - Changes that reduce cost without quality risk 3. **Routing Changes** - Model-tier changes and promotion conditions 4. **Caching Plan** - Stable prefix, variable suffix, and metrics to watch 5. **Capture Candidates** - Workflows worth turning into skills, references, or scripts 6. **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
Related in AI Agents
skill-development
IncludedComprehensive meta-skill for creating, managing, validating, auditing, and distributing Claude Code skills and slash commands (unified in v2.1.3+). Provides skill templates, creation workflows, validation patterns, audit checklists, naming conventions, YAML frontmatter guidance, progressive disclosure examples, and best practices lookup. Use when creating new skills, validating existing skills, auditing skill quality, understanding skill architecture, needing skill templates, learning about YAML frontmatter requirements, progressive disclosure patterns, tool restrictions (allowed-tools), skill composition, skill naming conventions, troubleshooting skill activation issues, creating custom slash commands, configuring command frontmatter, using command arguments ($ARGUMENTS, $1, $2), bash execution in commands, file references in commands, command namespacing, plugin commands, MCP slash commands, Skill tool configuration, or deciding between skills vs slash commands. Delegates to docs-management skill for official documentation.
reprompter
IncludedTransform messy prompts into well-structured, effective prompts — single or multi-agent. Use when: "reprompt", "reprompt this", "clean up this prompt", "structure my prompt", rough text needing XML tags and best practices, "reprompter teams", "repromptception", "run with quality", "smart run", "smart agents", multi-agent tasks, audits, parallel work, anything going to agent teams. Don't use when: simple Q&A, pure chat, immediate execution-only tasks. See "Don't Use When" section for details. Outputs: Structured XML/Markdown prompt, quality score (before/after), optional team brief + per-agent sub-prompts, agent team output files. Success criteria: Single mode quality score ≥ 7/10; Repromptception per-agent prompt quality score 8+/10; all required sections present, actionable and specific.
adaptive-compaction
IncludedAdaptive add-on policy and recovery layer that decides WHEN to compact, prune, snapshot, or fork -- replacing fixed-percent auto-compaction across Claude Code, Codex, and MCP-capable hosts. Trigger on auto-compact timing or damage: "when should I compact", "is it safe to compact now or start a fresh session", "auto-compact fires too early/mid-task", "switching to an unrelated task but the window still has space", "context rot", "answers get worse the longer the session runs", "the agent forgot the plan or my decisions after it summarized", "add a layer on top that manages context without changing the agent", raising autoCompactWindow to give the policy room, or installing/tuning a cross-tool compaction policy or PreCompact hook -- even when "compaction" is never said but the problem is context-window pressure or post-summarization memory loss. Do NOT use to summarize a conversation, build RAG, write a summarization prompt (decides WHEN not HOW), or answer max-context-length trivia.
agent-skill-creator
IncludedCreate cross-platform agent skills from workflow descriptions. Activates when users ask to create an agent, automate a repetitive workflow, create a custom skill, or need advanced agent creation. Triggers on phrases like create agent for, automate workflow, create skill for, every day I have to, daily I need to, turn process into agent, need to automate, create a cross-platform skill, validate this skill, export this skill, migrate this skill. Supports single skills, multi-agent suites, transcript processing, template-based creation, interactive configuration, cross-platform export, and spec validation.
llm-wiki
IncludedUse when building or maintaining a persistent personal knowledge base (second brain) in Obsidian where an LLM incrementally ingests sources, updates entity/concept pages, maintains cross-references, and keeps a synthesis current. Triggers include "second brain", "Obsidian wiki", "personal knowledge management", "ingest this paper/article/book", "build a research wiki", "compound knowledge", "Memex", or whenever the user wants knowledge to accumulate across sessions instead of being re-derived by RAG on every query.
skill-master
IncludedAgent Skills authoring, evaluation, and optimization. Create, edit, validate, benchmark, and improve skills following the agentskills.io specification. Use when designing SKILL.md files, structuring skill folders (references, scripts, assets), ingesting external documentation into skills, running trigger evals, benchmarking skill quality, optimizing descriptions, or performing blind A/B comparisons. Keywords: agentskills.io, SKILL.md, skill authoring, eval, benchmark, trigger optimization.