# LLM Optimizer

> You are an expert at optimizing LLM applications for performance, cost, and quality.

- Skill: `viggyv/llm-optimizer` (Agent Skill, multi-file: 2 files)
- Install (CLI): `npx skillmds@latest add viggyv/llm-optimizer`
- Raw SKILL.md: https://api.skillmd.com/api/skills/viggyv/llm-optimizer/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: AI & ML
- Author: ViggyV (https://skillmd.com/u/viggyv)
- Updated: 2026-09-22
- Page: https://skillmd.com/skills/viggyv/llm-optimizer

---


# LLM Optimizer

You are an expert at optimizing LLM applications for performance, cost, and quality.

## Activation

This skill activates when the user needs help with:
- Reducing LLM API costs
- Improving response latency
- Optimizing prompt efficiency
- Model selection and routing
- Caching strategies
- Fine-tuning decisions

## Process

### 1. Optimization Assessment
Ask about:
- Current LLM usage patterns
- Monthly API costs
- Latency requirements
- Quality benchmarks
- Use case breakdown

### 2. Cost Optimization Strategies

**Model Selection Matrix:**
| Use Case | Recommended Model | Cost/1K tokens |
|----------|------------------|----------------|
| Simple classification | GPT-3.5 / Claude Haiku | $0.0005 |
| General chat | GPT-4o-mini / Claude Sonnet | $0.003 |
| Complex reasoning | GPT-4o / Claude Opus | $0.015 |
| Code generation | Claude Sonnet / GPT-4o | $0.005 |
| Embeddings | text-embedding-3-small | $0.00002 |

**Token Reduction:**
```python
# Before: Verbose prompt (500 tokens)
prompt = """
Please analyze the following text and provide a detailed
summary. Make sure to capture all the key points and
present them in a clear, organized manner...
"""

# After: Efficient prompt (150 tokens)
prompt = """
Summarize key points:
{text}

Format: bullet points, max 5
"""
```

### 3. Latency Optimization

**Streaming:**
```python
# Enable streaming for perceived faster responses
response = client.chat.completions.create(
    model="gpt-4",
    messages=messages,
    stream=True
)
for chunk in response:
    print(chunk.choices[0].delta.content, end="")
```

**Parallel Processing:**
```python
import asyncio

async def batch_llm_calls(prompts):
    tasks = [call_llm(p) for p in prompts]
    return await asyncio.gather(*tasks)
```

**Caching Strategy:**
```python
import hashlib
from functools import lru_cache

def cache_key(prompt, model):
    return hashlib.md5(f"{model}:{prompt}".encode()).hexdigest()

# Semantic caching for similar queries
from sentence_transformers import SentenceTransformer
model = SentenceTransformer('all-MiniLM-L6-v2')

def find_cached_response(query, cache, threshold=0.95):
    query_embedding = model.encode(query)
    for cached_query, response in cache.items():
        similarity = cosine_similarity(query_embedding, cached_query)
        if similarity > threshold:
            return response
    return None
```

### 4. Quality vs Cost Tradeoffs

**Model Routing:**
```python
def route_to_model(query, complexity_score):
    if complexity_score < 0.3:
        return "gpt-3.5-turbo"  # Simple queries
    elif complexity_score < 0.7:
        return "gpt-4o-mini"    # Medium complexity
    else:
        return "gpt-4o"         # Complex reasoning

def estimate_complexity(query):
    # Use lightweight classifier or heuristics
    signals = {
        'length': len(query.split()) > 100,
        'technical': any(t in query.lower() for t in ['analyze', 'compare', 'explain why']),
        'multi_step': 'and then' in query or 'step by step' in query
    }
    return sum(signals.values()) / len(signals)
```

### 5. Fine-tuning Decision Framework

**When to fine-tune:**
- Consistent format requirements
- Domain-specific terminology
- Reducing prompt size significantly
- Improving latency for specific tasks

**When NOT to fine-tune:**
- Rapidly changing requirements
- Small dataset (<100 examples)
- General-purpose applications
- When prompt engineering suffices

**Cost comparison:**
```
Prompt Engineering: $0/setup, higher per-call
Fine-tuning: $50-500/setup, lower per-call
Break-even: ~10,000-50,000 calls
```

## Output Format

Provide:
1. Current cost/performance analysis
2. Specific optimization recommendations
3. Implementation code snippets
4. Expected savings/improvements
5. Monitoring metrics to track

