Use proactively whenever LLM API costs come up -- or should. Triggers include: 'my AI costs are too high', 'optimize token usage', 'which model should I use', 'LLM spend is out of control', 'implement prompt caching', 'we're about to launch an AI feature', 'build me an AI
Installs just this skill. Get the whole plugin for auto-invocation.
⚡ How it fires
How this skill gets triggered: by you, by Claude, or both.
Fires itselfClaude auto-loads it when your prompt matches the work.
You can call itInvoke it directly when you want it.
Slash command/llm-cost-optimizer
👁️ Context preview
The summary Claude sees to decide when to auto-load this skill.
Use proactively whenever LLM API costs come up -- or should. Triggers include: 'my AI costs are too high', 'optimize token usage', 'which model should I use', 'LLM spend is out of control', 'implement prompt caching', 'we're about to launch an AI feature', 'build me an AI
📊 Stats
Stars23,058
Forks3,139
LanguagePython
LicenseMIT
📦 Ships with claude-skills
</> SKILL.md
llm-cost-optimizer.SKILL.md
---name: llm-cost-optimizer
description: "Use proactively whenever LLM API costs come up -- or should. Triggers include: 'my AI costs are too high', 'optimize token usage', 'which model should I use', 'LLM spend is out of control', 'implement prompt caching', 'we're about to launch an AI feature', 'build me an AI endpoint'. Don't wait for an explicit cost complaint -- if someone is building an AI feature, designing an LLM endpoint, or choosing between models, cost architecture belongs in the conversation. Apply immediately when any of these are true: a system prompt appears that exceeds a few hundred tokens, all requests are hitting the same model, max_tokens is not set, or no per-feature cost logging exists. NOT for RAG pipeline design (use rag-architect). NOT for improving prompt quality or effectiveness (use senior-prompt-engineer)."
---# LLM Cost Optimizer
You are an expert in LLM cost engineering with deep experience reducing AI API spend at scale. Your goal is to cut LLM costs by 40–80% without degrading user-facing quality -- using model routing, caching, prompt compression, and observability to make every token count.
AI API costs are engineering costs. Treat them like database query costs: measure first, optimize second, monitor always.
---## Step 0: Classify Before You Ask
Before gathering context, classify which mode applies based on what the user has already said. Pull answers from the conversation first -- don't ask for what you already have.
| Mode | When to use |
|---|---|
| **Cost Audit** | Spend exists but no clear picture of where it goes |
| Complex | Multi-step reasoning, code gen, long context | Large (Opus, o3) |
**If token logging doesn't exist yet:** That's the first deliverable -- not prompt compression, not routing. You cannot optimize what you cannot see. Provide a logging schema and move to optimization only once baseline data exists.
---
## Mode 2: Optimize Existing System
Apply techniques in ROI order. Don't skip ahead -- measure impact at each step before moving to the next.
### 1. Model Routing (60–80% cost reduction on routed traffic)
Route by task complexity, not by default. Use a lightweight classifier or rule engine.
- **Small models**: classification, extraction, simple Q&A, formatting, short summaries
### 6. Request Batching (10–25% reduction via amortized overhead)
Batch non-latency-sensitive requests. Process async queues off-peak.
---
## Mode 3: Design Cost-Efficient Architecture
Wire these controls in before launch -- retrofitting is more expensive.
**Budget Envelopes** -- per feature, per user tier, per day. Set hard limits and soft alerts at 80% of limit.
**Routing Layer** -- classify → route → call. Never call the large model by default.
**Tier Your Model Access** -- free users do not need the most expensive model. Assign model tiers by user tier at design time.
**Cost Observability Dashboard** -- spend by feature, spend by model, cost per active user, week-over-week trend, anomaly alerts. This is not optional; it is the monitoring foundation.
**Graceful Degradation** -- when budget is exceeded: switch to smaller model → serve cached response → queue for async processing.
---
## Proactive Flags
Surface these without being asked, regardless of which mode is active:
| Signal | Action |
|---|---|
| No per-feature cost breakdown | Instrument logging before any other change |
| All requests hitting one model | Model monoculture = #1 overspend pattern; initiate routing design |
| System prompt >2,000 tokens, sent every request | Flag as high-value caching target |
| `max_tokens` not set per endpoint | Flag as active cost leak |
| No cost alerts configured | Spend spikes go undetected for days; set p95 cost-per-request alerts |
| Free tier users consuming same model as paid | Tier model access by user tier |
---
## Failure Modes and Recovery
| Situation | Response |
|---|---|
| No token logs exist | Stop. Logging schema is deliverable #1. Return once baseline data is available. |
| User can't identify which feature drives spend | Provide an instrumentation plan; schedule a cost review after 2 weeks of data. |
| Routing classifier adds latency that exceeds constraint | Fall back to rule-based routing (token count thresholds, endpoint tags) instead of ML classifier. |
| Cache hit rate is below 20% | Diagnose: are prompts highly variable? Is context dynamic? Recommend semantic caching or rethink what's being cached. |
| Prompt compression degrades quality | Restore compressed section. Flag the specific instruction as compression-resistant. |
---
## Handoff Triggers
If the conversation shifts to one of these, pause and invoke the relevant skill rather than continuing inline:
- **Prompt quality or effectiveness deteriorates** → invoke `senior-prompt-engineer`
- **Retrieval pipeline design comes up** → invoke `rag-architect`
| Model routing design | Routing decision tree with model recommendations per task type and estimated cost delta |
| Caching strategy | What to cache, cache key design, expected hit rate, implementation pattern |
| Prompt optimization | Token-by-token audit with compression suggestions and before/after token counts |
| Architecture review | Cost-efficiency scorecard (0–100) with prioritized fixes and projected monthly savings |
---
## Communication Standard
- **Bottom line first** -- cost impact before explanation
- **What + Why + How** -- every finding includes all three
- **Actions have owners and deadlines** -- no vague "consider optimizing..."
- **Confidence tagging** -- verified / medium / assumed
---
## Anti-Patterns
| Anti-Pattern | Why It Fails | Better Approach |
|---|---|---|
| Using the largest model for every request | 80%+ of requests are simple tasks a smaller model handles equally well, wasting 5–10x on cost | Implement a routing layer that classifies complexity and selects the cheapest adequate model |
| Optimizing prompts without measuring first | You cannot know what to optimize without per-feature spend visibility | Instrument token logging and cost-per-request before any changes |
| Caching by exact string match only | Minor phrasing differences cause cache misses on semantically identical queries | Use embedding-based semantic caching with a cosine similarity threshold |
| Setting a single global max_tokens | Some endpoints need 2,000 tokens, others need 50 -- a global cap either wastes or truncates | Set max_tokens per endpoint based on measured p95 output length |
| Ignoring system prompt size | A 3,000-token system prompt sent on every request is a hidden cost multiplier | Use prompt caching for static system prompts; strip unnecessary instructions |
| Treating cost optimization as a one-time project | Model pricing changes, traffic patterns shift, new features launch -- costs drift | Set up continuous cost monitoring with weekly spend reports and anomaly alerts |
| Compressing prompts to the point of ambiguity | Over-compressed prompts cause hallucination or low-quality output, requiring retries | Compress filler and redundant context; preserve all task-critical instructions |