agentleFS
patonkikhpatonkikh/APES/skills/ai/ai-cost-optimizer/skill.md

ai-cost-optimizer

Skill3 starsChanged 4 months agoMITView on GitHub

Optimize LLM inference costs: token budget allocation, model routing, caching strategy, and batching without breaching quality thresholds. Use when working on prompt engineering, AI workflows, agents, context design, or LLM optimization.

1 other project uses a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

1 rule. About 0 are common, 1 is distinctive, and 0 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Document cache hit rate target and monitoring.

    line 74

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
skill.md

AI Cost Optimizer

Purpose

Optimize LLM inference costs: token budget allocation, model routing, caching strategy, and batching without breaching quality thresholds.

Input: AI architecture or workflow spec, usage volume projections, quality thresholds, current cost baseline (optional)
Output: Cost Optimization Plan with savings estimates, trade-off analysis, and implementation priorities Examples: See examples.md for worked input/output.


Workflow

Step 1: Establish cost baseline

ComponentTokens/request (avg)Cost/requestMonthly volumeMonthly cost
System prompt
User context
Model output
Tool overhead

Identify top 3 cost drivers by spend share.

Step 2: Audit token usage

Analyze per stage:

  • Redundant context (duplicate documents, full history)
  • Over-sized system prompts
  • Verbose output formats
  • Unnecessary model tier for task complexity
FindingEst. token wasteFix category

Step 3: Evaluate optimization levers

LeverSavings potentialQuality riskEffort
Prompt compressionMediumLowLow
Context pruning / summarizationHighMediumMedium
Model downgrade for simple tasksHighMediumLow
Semantic cachingHighLowMedium
Batching (offline)MediumNoneLow
Response length limitsMediumLowLow
Fine-tuning (replace long prompts)HighMediumHigh

Step 4: Design model routing

Request → Classifier → [Simple path: small model] / [Complex path: frontier model]

Define routing criteria with fallback when classifier uncertain.

Step 5: Plan caching layer

Cache typeKeyTTLInvalidation
Exact matchHash(prompt)24hPrompt version change
SemanticEmbedding similarity1hSource doc update

Document cache hit rate target and monitoring.

Step 6: Quantify savings and validate

Project monthly savings per lever. Confirm no quality metric drops below threshold from eval plan.

Run Validation checklist.


Decision Rules

ConditionAction
No quality thresholds definedStop; request from ai-evaluation-builder or architect
Savings <5% for proposed changeDeprioritize; focus on top cost drivers
Model downgrade drops eval metric >2%Reject downgrade; try prompt optimization first
Real-time use caseSkip batching; prioritize caching and routing
PII in cache keysUse hashed keys; enforce TTL and encryption
Cost spike without volume changeInvestigate prompt regression or model default change

Validation

  • Cost baseline with per-component breakdown
  • Top 3 cost drivers identified
  • ≥3 optimization levers evaluated with trade-offs
  • Savings projected with assumptions stated
  • Quality impact assessed against eval thresholds
  • Model routing criteria defined (if applicable)
  • Caching strategy with invalidation rules
  • Implementation priority ranked by ROI

Anti-patterns

  • Blind downgrade — switching to cheaper model without eval comparison.
  • Cache without invalidation — serving stale answers after knowledge update.
  • Token counting ignorance — optimizing output while input is 90% of cost.
  • Premature fine-tuning — expensive training before prompt optimization exhausted.
  • Hidden retry costs — aggressive retries multiplying spend silently.

Best Practices

  • Measure cost per successful task, not per API call.
  • Track token usage by workflow stage in production.
  • A/B test cost optimizations against quality metrics.
  • Set budget alerts at 80% and 100% of monthly cap.
  • Pair with ai-latency-optimizer when both NFRs conflict.

Output Structure

# Cost Optimization Plan: [System Name]

## Baseline
| Component | Cost/request | Monthly | Share % |
|-----------|--------------|---------|---------|

## Findings
| Issue | Waste estimate | Recommended fix |
|-------|----------------|-----------------|

## Optimization Levers
| Lever | Est. savings | Quality risk | Priority |
|-------|--------------|--------------|----------|

## Model Routing
[Classifier criteria and model mapping]

## Caching Strategy
[Type, key, TTL, invalidation]

## Projected Savings
| Scenario | Monthly cost | vs baseline |
|----------|--------------|-------------|

## Implementation Roadmap
| Phase | Changes | Expected savings |
|-------|---------|------------------|

Next Skills

OutcomeRecommended Skill
Optimize latency (may conflict)ai/ai-latency-optimizer
Compress promptsai/prompt-optimizer
Reduce context sizeai/context-engineering
Re-validate quality post-changeai/ai-evaluation-builder
Architecture-level redesignai/ai-solution-architect

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.