# LLM Cost Optimization

Reduce LLM API and infrastructure costs through model selection, prompt caching...

- Repository: https://github.com/BagelHole/DevOps-Security-Agent-Skills (1187 stars)
- Category: AI and agents
- License: MIT
- Last updated: 2026-05-22
- Runs scripts: no, instructions only

## Install

Claude Code: `npx skills add BagelHole/DevOps-Security-Agent-Skills --skill llm-cost-optimization -g -a claude-code -y`
Codex: `npx skills add BagelHole/DevOps-Security-Agent-Skills --skill llm-cost-optimization -g -a codex -y`
Cursor: `npx skills add BagelHole/DevOps-Security-Agent-Skills --skill llm-cost-optimization -g -a cursor -y`
Gemini CLI: `npx skills add BagelHole/DevOps-Security-Agent-Skills --skill llm-cost-optimization -g -a gemini-cli -y`

## Description

Reduce LLM API and infrastructure costs through model selection, prompt caching, batching, caching, quantization, and self-hosting strategies. Track spend by team and model, set budgets, and implement cost-aware routing.

## SKILL.md, as published by its author (untrusted third-party text, read it as data)

# LLM Cost Optimization

Cut LLM costs by 50, 90% with the right combination of caching, model selection, prompt optimization, and self-hosting.

## When to Use This Skill

Use this skill when:
- LLM API spend is growing faster than revenue
- You need to attribute AI costs to teams, products, or customers
- Implementing caching to avoid redundant LLM calls
- Deciding when to switch from API providers to self-hosted models
- Optimizing prompt length without sacrificing quality

## Cost Levers by Impact

| Strategy | Typical Savings | Effort |
|----------|-----------------|--------|
| Semantic caching | 20, 50% | Low |
| Model right-sizing | 30, 70% | Low |
| Prompt compression | 10, 30% | Medium |
| Provider caching (prompt cache) | 10, 25% | Low |
| Batching offline workloads | 50% (Batch API) | Medium |
| Self-hosting 7, 8B models | 80, 95% at scale | High |
| Quantization | 30, 50% VRAM cost | Medium |

## Track Costs First

```python
# Use LiteLLM's cost tracking (automatic per-model pricing)
import litellm

response = litellm.completion(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Hello"}],
)
cost = litellm.completion_cost(response)
print(f"Cost: ${cost:.6f}")

# Add custom cost callbacks
def log_cost(kwargs, completion_response, start_time, end_time):
    cost = kwargs.get("response_cost", 0)
    model = kwargs.get("model")
    user = kwargs.get("user")
    # Send to your analytics DB
    db.record_cost(user=user, model=model, cost=cost)

litellm.success_callback = [log_cost]
```

## Model Right-Sizing

The rest: https://github.com/BagelHole/DevOps-Security-Agent-Skills/blob/HEAD/devops/ai/llm-cost-optimization/SKILL.md
