cost-latency-optimizer — Claude Code skill: cut LLM spend
This skill tracks what your LLM app spends and where it stalls, then hands over caching, model selection, batching and prompt trimming code.
npx skills add patricio0312rev/skills --skill cost-latency-optimizer -a claude-codeWhat cost-latency-optimizer does
The skill gives the agent Python building blocks: a cost analyzer that counts LLM calls, embeddings and tool calls, a Redis-backed cache keyed on a hash of prompt plus model, and a pricing table for gpt-4, gpt-3.5-turbo, claude-3-opus and claude-3-sonnet. There is also a model picker driven by query complexity and a latency tracker that reports average and p95 per operation.
Batching runs through asyncio.gather with a configurable batch size, and streaming is covered as a way to shorten time to first byte. The last piece generates recommendations: it reads the cost totals and average latency, then lists fixes with the expected effect.
The cache needs a Redis client, and the cost math in the sample is hardcoded to GPT-4 rates, so your report will drift if you pay different prices. Check MODEL_PRICING before trusting the numbers. Complexity classification leans on plain heuristics such as query length and the words "analyze", "complex" and "detailed", which means odd phrasing can land in the wrong bucket.
What it helps with
- see what LLM calls cost per day
- find which operation drags response time
- cache repeated prompts instead of paying twice
- route simple queries to a cheaper model
- shrink prompts by dropping wasted tokens
Example prompts for the agent
- analyze my service's LLM spend and show the breakdown by call type
- where are my latency hotspots? add the tracker and pull a report
- suggest config changes that would lower my request costs
How to install cost-latency-optimizer
1Skills CLI (Vercel)
npx skills add patricio0312rev/skills --skill cost-latency-optimizer -a claude-code2Manually: put the files in place
~/.claude/skills/cost-latency-optimizer/SKILL.mdCopy the whole skill folder; for one project use .claude/skills/cost-latency-optimizer/
1Skills CLI (Vercel)
npx skills add patricio0312rev/skills --skill cost-latency-optimizer -a codex2Manually: put the files in place
.agents/skills/cost-latency-optimizer/SKILL.mdFor all projects use ~/.agents/skills/
1Skills CLI (Vercel)
npx skills add patricio0312rev/skills --skill cost-latency-optimizer -a cursor2Manually: put the files in place
.cursor/skills/cost-latency-optimizer/SKILL.mdCursor also reads .agents/skills/ and .claude/skills/
1GitHub CLI
gh skill install patricio0312rev/skills cost-latency-optimizer2Manually: put the files in place
.github/skills/cost-latency-optimizer/SKILL.md1Skills CLI (Vercel)
npx skills add patricio0312rev/skills --skill cost-latency-optimizer -a gemini-cli2Platform CLI
gemini skills install https://github.com/patricio0312rev/skills --path ai-engineering/cost-latency-optimizerManually: put the files in place
.devin/skills/cost-latency-optimizer/SKILL.mdThe legacy .windsurf/skills/ path still works
Skills CLI (Vercel)
npx skills add patricio0312rev/skills --skill cost-latency-optimizer -a opencodeClaude.ai
Zip the folder and upload it in Settings → Capabilities → Skills
Step by step, with commands for each platform: How to install skills in Claude Code: five ways
Questions about cost-latency-optimizer
How do I install cost-latency-optimizer in Claude?
Run npx skills add patricio0312rev/skills --skill cost-latency-optimizer -a claude-code. The skill lands at ~/.claude/skills/cost-latency-optimizer/SKILL.md.
What does cost-latency-optimizer do?
It measures LLM, embedding and tool-call costs, tracks latency, and returns recommendations on caching, model choice, batching and prompt compression.
What do I need for it to work?
The caching examples expect a Redis client, and the cost math uses a pricing table built around Claude and Claude models. You will need to edit that table for your own rates.
Source text
SKILL.md · MIT
---
name: cost-latency-optimizer
description: Reduces LLM costs and improves response times through caching, model selection, batching, and prompt optimization. Provides cost breakdowns, latency hotspots, and configuration recommendations. Use for "cost reduction", "performance optimization", "latency improvement", or "efficiency".
---
# Cost & Latency Optimizer
Optimize LLM applications for cost and performance.
## Cost Breakdown Analysis
```python
class CostAnalyzer:
def __init__(self):
self.costs = {
"llm_calls": 0,
"embeddings": 0,
"tool_calls": 0,
}
self.counts = {
"llm_calls": 0,
"embeddings": 0,
}
def track_llm_call(self, tokens_in: int, tokens_out: int):
# GPT-4 pricing
cost = (tokens_in / 1000) * 0.03 + (tokens_out / 1000) * 0.06
self.costs["llm_calls"] += cost
self.counts["llm_calls"] += 1
def report(self):
return {
"total_cost": sum(self.costs.values()),
"breakdown": self.costs,
"avg_cost_per_call": self.costs["llm_calls"] / self.counts["llm_calls"],
}
```
## Caching Strategy
```python
import hashlib
from functools import lru_cache
class LLMCache:
def __init__(self, redis_client):
self.cache = redis_client
self.ttl = 3600 # 1 hour
def get_cache_key(self, prompt: str, model: str) -> str:
content = f"{model}:{prompt}"
return f"llm_cache:{hashlib.sha256(content.encode()).hexdigest()}"
def get(self, prompt: str, model: str):
key = self.get_cache_key(prompt, model)
return self.cache.get(key)
def set(self, prompt: str, model: str, response: str):
key = self.get_cache_key(prompt, model)
self.cache.setex(key, self.ttl, response)
# Usage
cache = LLMCache(redis_client)
def cached_llm_call(prompt: str, model: str = "gpt-4"):
# Check cache
cached = cache.get(prompt, model)
if cached:
return cached
# Call LLM
response = llm(prompt, model=model)
# Cache result
cache.set(prompt, model, response)
return response
```
## Model Selection
```python
MODEL_PRICING = {
"gpt-4": {"input": 0.03, "output": 0.06},
"gpt-3.5-turbo": {"input": 0.0005, "output": 0.0015},
"claude-3-opus": {"input": 0.015, "output": 0.075},
"claude-3-sonnet": {"input": 0.003, "output": 0.015},
}
def select_model_by_complexity(query: str) -> str:
"""Use cheaper models for simple queries"""
# Classify complexity
complexity = classify_complexity(query)
if complexity == "simple":
return "gpt-3.5-turbo" # 60x cheaper
elif complexity == "medium":
return "claude-3-sonnet"
else:
return "gpt-4"
def classify_complexity(query: str) -> str:
# Simple heuristics
if len(query) < 100 and "?" in query:
return "simple"
elif any(word in query.lower() for word in ["analyze", "complex", "detailed"]):
return "complex"
return "medium"
```
## Prompt Optimization
```python
def optimize_prompt(prompt: str) -> str:
"""Reduce token count while preserving meaning"""
optimizations = [
# Remove extra whitespace
lambda p: re.sub(r'\s+', ' ', p),
# Remove examples if not critical
lambda p: p.split("Examples:")[0] if "Examples:" in p else p,
# Use abbreviations
lambda p: p.replace("For example", "E.g."),
]
for optimize in optimizations:
prompt = optimize(prompt)
return prompt.strip()
# Example: 500 tokens → 350 tokens = 30% cost reduction
```
## Batching
```python
async def batch_llm_calls(prompts: List[str], batch_size: int = 5):
"""Process multiple prompts in parallel"""
results = []
for i in range(0, len(prompts), batch_size):
batch = prompts[i:i + batch_size]
# Parallel execution
batch_results = await asyncio.gather(*[
llm_async(prompt) for prompt in batch
])
results.extend(batch_results)
return results
# 10 sequential calls: ~30 seconds
# 10 batched calls (5 parallel): ~6 seconds
```
## Latency Hotspot Analysis
```python
import time
class LatencyTracker:
def __init__(self):
self.timings = {}
def track(self, operation: str):
def decorator(func):
def wrapper(*args, **kwargs):
start = time.time()
result = func(*args, **kwargs)
duration = time.time() - start
if operation not in self.timings:
self.timings[operation] = []
self.timings[operation].append(duration)
return result
return wrapper
return decorator
def report(self):
return {
op: {
"count": len(times),
"total": sum(times),
"avg": sum(times) / len(times),
"p95": sorted(times)[int(len(times) * 0.95)]
}
for op, times in self.timings.items()
}
# Usage
tracker = LatencyTracker()
@tracker.track("llm_call")
def call_llm(prompt):
return llm(prompt)
# After 100 calls
print(tracker.report())
# {"llm_call": {"avg": 2.3, "p95": 4.1, ...}}
```
## Optimization Recommendations
```python
def generate_recommendations(cost_analysis, latency_analysis):
recs = []
# High LLM costs
if cost_analysis["costs"]["llm_calls"] > 10:
recs.append({
"issue": "High LLM costs",
"recommendation": "Implement caching for repeated queries",
"impact": "50-80% cost reduction",
})
if cost_analysis["avg_cost_per_call"] > 0.01:
recs.append({
"issue": "Using expensive model for all queries",
"recommendation": "Use gpt-3.5-turbo for simple queries",
"impact": "60% cost reduction",
})
# High latency
if latency_analysis["llm_call"]["avg"] > 3:
recs.append({
"issue": "High LLM latency",
"recommendation": "Batch parallel calls, use streaming",
"impact": "50% latency reduction",
})
return recs
```
## Streaming for Faster TTFB
```python
async def streaming_llm(prompt: str):
"""Stream tokens as they're generated"""
async for chunk in llm_stream(prompt):
yield chunk
# User sees partial response immediately
# Time to First Byte: ~200ms (streaming) vs ~2s (waiting for full response)
```
## Best Practices
1. **Cache aggressively**: Identical queries cached
2. **Model selection**: Use cheaper models when possible
3. **Prompt optimization**: Reduce unnecessary tokens
4. **Batching**: Parallel execution for throughput
5. **Streaming**: Faster perceived latency
6. **Monitor costs**: Track per-user, per-feature
7. **Set budgets**: Alert on anomalies
## Output Checklist
- [ ] Cost tracking implementation
- [ ] Caching layer
- [ ] Model selection logic
- [ ] Prompt optimization
- [ ] Batching for parallel calls
- [ ] Latency tracking
- [ ] Hotspot analysis
- [ ] Optimization recommendations
- [ ] Budget alerts
- [ ] Performance dashboard
© patricio0312rev · MIT · patricio0312rev/skills