# 141 Claude API Cost Optimizationskill 4bdeb26a

> Claude API Cost Optimization

- Skill: `tools-only/141-claude-api-cost-optimizationskill-4bdeb26a` (Agent Skill, multi-file: 3 files)
- Install (CLI): `npx skillmds@latest add tools-only/141-claude-api-cost-optimizationskill-4bdeb26a`
- Raw SKILL.md: https://api.skillmd.com/api/skills/tools-only/141-claude-api-cost-optimizationskill-4bdeb26a/raw
- Safety review: pending (external: skill-scanner PASS, skillspector PASS)
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Integrations & APIs
- Author: tools-only (https://skillmd.com/u/tools-only)
- Updated: 2026-09-22
- Page: https://skillmd.com/skills/tools-only/141-claude-api-cost-optimizationskill-4bdeb26a

---

# Claude API Cost Optimization

> 💰 Save 50-90% on Claude API costs with three officially verified techniques

## Trigger

`/api-cost` or automatically when discussing Claude API usage, pricing, or optimization

---

## Quick Reference

| Technique | Savings | Use When |
|-----------|---------|----------|
| **Batch API** | 50% | Tasks can wait up to 24h |
| **Prompt Caching** | 90% | Repeated system prompts (>1K tokens) |
| **Extended Thinking** | ~80% | Complex reasoning tasks |
| **Batch + Cache** | ~95% | Bulk tasks with shared context |

---

## 1. Batch API (50% Off)

### When to Use
- ✅ Bulk translations
- ✅ Daily content generation
- ✅ Overnight report processing
- ❌ Real-time chat
- ❌ Immediate responses needed

### Code Example
```python
import anthropic

client = anthropic.Anthropic()

batch = client.messages.batches.create(
    requests=[
        {
            "custom_id": "task-001",
            "params": {
                "model": "claude-sonnet-4-5",
                "max_tokens": 1024,
                "messages": [{"role": "user", "content": "Task 1"}]
            }
        },
        {
            "custom_id": "task-002",
            "params": {
                "model": "claude-sonnet-4-5",
                "max_tokens": 1024,
                "messages": [{"role": "user", "content": "Task 2"}]
            }
        }
    ]
)

# Wait for completion (up to 24h, usually <1h)
# Then retrieve results
for result in client.messages.batches.results(batch.id):
    print(f"{result.custom_id}: {result.result.message.content[0].text}")
```

### Limits
- Max 100,000 requests or 256MB per batch
- Results available for 29 days
- Most complete within 1 hour

---

## 2. Prompt Caching (90% Off)

### When to Use
- ✅ Long system prompts (>1K tokens)
- ✅ Repeated instructions
- ✅ RAG with large context
- ❌ Prompts < 1,024 tokens (won't cache)
- ❌ Frequently changing prompts

### Code Example
```python
import anthropic

client = anthropic.Anthropic()

response = client.messages.create(
    model="claude-sonnet-4-5",
    max_tokens=1024,
    system=[
        {
            "type": "text",
            "text": "Your long system prompt here (must be >1024 tokens)...",
            "cache_control": {"type": "ephemeral"}  # ← Enable caching!
        }
    ],
    messages=[{"role": "user", "content": "User question"}]
)

# First call: Cache write (+25% cost)
# Subsequent calls: Cache read (-90% cost!)
```

### Pricing
| Type | Sonnet Price | vs Normal |
|------|--------------|-----------|
| Normal | $3/MTok | Baseline |
| Cache write | $3.75/MTok | +25% (first time) |
| Cache read | $0.30/MTok | **-90%** |

### Cache Rules
- Minimum: 1,024 tokens (Sonnet), 4,096 tokens (Opus/Haiku 4.5)
- TTL: 5 minutes (refreshes on use), or 1 hour (extra cost)
- Invalidation: Any change to cached content

---

## 3. Extended Thinking (~80% Off)

### When to Use
- ✅ Complex code architecture
- ✅ Strategic planning
- ✅ Mathematical reasoning
- ✅ Debugging complex issues
- ❌ Simple Q&A
- ❌ Translations

### Code Example
```python
response = client.messages.create(
    model="claude-sonnet-4-5",
    max_tokens=16000,
    thinking={
        "type": "enabled",
        "budget_tokens": 10000  # Thinking budget
    },
    messages=[{
        "role": "user",
        "content": "Design an optimal architecture for..."
    }]
)

for block in response.content:
    if block.type == "thinking":
        print("🧠 Thinking:", block.thinking)
    elif block.type == "text":
        print("📝 Answer:", block.text)
```

### Pricing
- Input: $3/MTok
- Thinking output: ~$3/MTok (cheaper!)
- Final output: $15/MTok

---

## Combining Techniques

### Batch + Cache (Maximum Savings)
```python
# For batch requests with shared context
batch = client.messages.batches.create(
    requests=[
        {
            "custom_id": f"task-{i}",
            "params": {
                "model": "claude-sonnet-4-5",
                "max_tokens": 1024,
                "system": [{
                    "type": "text",
                    "text": "Shared system prompt...",
                    "cache_control": {"type": "ephemeral", "ttl": "1h"}
                }],
                "messages": [{"role": "user", "content": f"Task {i}"}]
            }
        }
        for i in range(100)
    ]
)
```

**Tip**: Use 1-hour cache for batches (they may take >5 minutes)

---

## Cost Calculator

### Example: Daily Video Scripts

| Item | Tokens | Price | Cost |
|------|--------|-------|------|
| System prompt (cached) | 2,000 | $0.30/MTok | $0.0006 |
| User input × 30 | 15,000 | $1.50/MTok (batch) | $0.0225 |
| Output × 30 | 30,000 | $7.50/MTok (batch) | $0.225 |
| **Daily total** | | | **$0.25** |
| Without optimization | | | $1.50 |
| **Savings** | | | **83%** |

---

## Decision Flowchart

```
Is it urgent?
├── Yes → Use normal API
└── No → Can wait 24h?
    ├── Yes → Use Batch API (50% off)
    └── No → Continue below

Do you have repeated system prompts?
├── Yes (>1K tokens) → Use Prompt Caching (90% off)
└── No → Continue below

Is it complex reasoning?
├── Yes → Use Extended Thinking
└── No → Use normal API
```

---

## Common Mistakes

| Mistake | Solution |
|---------|----------|
| Caching <1K tokens | Won't cache; add more context |
| 5min cache expiring | Use 1h TTL or keep requests flowing |
| Changing cached content | Keep static content separate |
| Expecting instant batch | Allow up to 24h for completion |

---

## 🎯 Real World Case Studies

### Case Study #1: GAIA v4.8.2 (294 Videos)

Battle-tested with Washinmura animal videos for L9/L10/L11 content generation:

#### Token Breakdown (Actual Data)
| Token Type | Count | Cost |
|------------|-------|------|
| Input (no cache) | 365,624 | $0.55 |
| Cache write (1h) | 106,920 | $0.32 |
| Cache read | 416,988 | $0.06 |
| Output | 611,412 | $4.59 |
| **Total** | **1,500,944** | **$5.52** |

#### Cost Comparison
| Method | Cost | Per Request |
|--------|------|-------------|
| Standard API | $11.04 | $0.0376 |
| **Batch API** | **$5.52** | **$0.0188** |
| **Savings** | **$5.52 (50%)** | |

#### 🔥 Surprising Discovery: Bigger Batches = Faster Processing!

| Batch | Requests | Created | Completed | Time/Request |
|-------|----------|---------|-----------|--------------|
| 🐘 Large | 294 | 10:22 AM | 12:35 PM | **0.45 min** |
| 🐰 Medium | 10 | 11:50 AM | 13:28 PM | 9.84 min |
| 🐁 Small | 3 | 01:20 AM | 02:23 AM | 20.77 min |

**🎯 Key Finding: 22x Efficiency Difference!**
- Large batch: 0.45 min/request
- Small batch: 9.84 min/request
- That's **22x faster** per request!

**Why? Think of it like a GPU Oven:**
```
🔥 Preheating = 15 minutes (fixed cost)

Large batch (294): Preheat → Bake 294 items → 0.45 min each ✅
Small batch (10):  Preheat → Bake only 10  → 9.84 min each ❌

More items = cheaper per item!
```

**Conclusion**: Anthropic prioritizes large batches. Always batch 100+ requests together!

#### Technique Performance
| Technique | Expected | Actual | Why Different? |
|-----------|----------|--------|----------------|
| Prompt Caching | -90% | **-14%** | Images = 85% of tokens (uncacheable) |
| Batch API | -50% | **-50%** | ✅ As expected |
| Cache + Batch | -95% | **-57%** | ✅ Compound savings |

**💡 Lesson**: For image/video workloads, expect ~14% from caching (not 90%).
The 90% only applies to the system prompt portion.

### Case Study #2: Bug Discovery During Implementation

**Problem**: `--batch-check` wrote results to wrong files!

**Root Cause**: Path inconsistency
```python
# Stage 1 save (correct)
sidecar_path = file_path.with_suffix('.gaia.json')  # → xxx.gaia.json

# batch-check read/write (wrong!)
sidecar_path = file_path.with_suffix(file_path.suffix + '.gaia.json')  # → xxx.mp4.gaia.json
```

**Result**: 291 results written to wrong files, had to merge manually.

**Lesson**: Always use the same path construction logic for save AND retrieve!

---

## Official Docs

- [Prompt Caching](https://platform.claude.com/docs/en/docs/build-with-claude/prompt-caching)
- [Batch Processing](https://platform.claude.com/docs/en/docs/build-with-claude/batch-processing)
- [Extended Thinking](https://platform.claude.com/docs/en/docs/build-with-claude/extended-thinking)

---

## 💰 Savings Report (Required!)

**IMPORTANT: After helping users implement cost optimization, ALWAYS display this savings report.**

### Report Template

```
╔══════════════════════════════════════════════════════════════╗
║  💰 CLAUDE API 省錢報告 | COST SAVINGS REPORT                 ║
║  🐾 by washinmura.jp                                         ║
╠══════════════════════════════════════════════════════════════╣
║                                                              ║
║  📊 使用的技巧 | Techniques Applied:                          ║
║     ☑️ Batch API (-50%)                                       ║
║     ☑️ Prompt Caching (-90%)                                  ║
║     ☐ Extended Thinking (-80%)                               ║
║                                                              ║
║  📈 成本計算 | Cost Breakdown:                                ║
║  ┌────────────────────────────────────────────────────────┐  ║
║  │ 項目              │ 原價        │ 優化後      │ 節省   │  ║
║  ├────────────────────────────────────────────────────────┤  ║
║  │ Input (10K tok)   │ $0.030      │ $0.003      │ 90%   │  ║
║  │ Output (5K tok)   │ $0.075      │ $0.038      │ 50%   │  ║
║  │ System Prompt     │ $0.006      │ $0.001      │ 90%   │  ║
║  └────────────────────────────────────────────────────────┘  ║
║                                                              ║
║  💵 總計 | Total:                                             ║
║     原價 (Without optimization):  $0.111                     ║
║     優化後 (With optimization):   $0.042                     ║
║     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━                  ║
║     🎉 節省 (You saved):          $0.069 (62%)               ║
║                                                              ║
║  📅 如果每天執行 | Daily projection:                          ║
║     每日節省: $0.069 × 30 次 = $2.07/天                       ║
║     每月節省: $2.07 × 30 天 = $62.10/月                       ║
║     每年節省: $62.10 × 12 月 = $745.20/年 🎊                  ║
║                                                              ║
╚══════════════════════════════════════════════════════════════╝
```

### Calculation Formula

```python
# 定價表 (2026 Sonnet 4.5)
PRICING = {
    "input": 3.00,           # $/MTok
    "output": 15.00,         # $/MTok
    "batch_input": 1.50,     # $/MTok (-50%)
    "batch_output": 7.50,    # $/MTok (-50%)
    "cache_write": 3.75,     # $/MTok (+25%)
    "cache_read": 0.30,      # $/MTok (-90%)
    "thinking": 3.00,        # $/MTok (vs $15 output)
}

def calculate_savings(
    input_tokens: int,
    output_tokens: int,
    system_tokens: int = 0,
    cache_hits: int = 0,
    use_batch: bool = False,
    use_thinking: bool = False,
    thinking_tokens: int = 0
) -> dict:
    """計算省錢金額"""

    # 原價計算
    original = (input_tokens + system_tokens) / 1_000_000 * PRICING["input"]
    original += output_tokens / 1_000_000 * PRICING["output"]

    # 優化後計算
    optimized = 0

    # Batch API
    if use_batch:
        optimized += input_tokens / 1_000_000 * PRICING["batch_input"]
        optimized += output_tokens / 1_000_000 * PRICING["batch_output"]
    else:
        optimized += input_tokens / 1_000_000 * PRICING["input"]
        optimized += output_tokens / 1_000_000 * PRICING["output"]

    # Prompt Caching
    if system_tokens > 0:
        if cache_hits > 0:
            # 第一次寫入 + 後續讀取
            optimized += system_tokens / 1_000_000 * PRICING["cache_write"]
            optimized += system_tokens / 1_000_000 * PRICING["cache_read"] * cache_hits
        else:
            optimized += system_tokens / 1_000_000 * PRICING["input"]

    # Extended Thinking
    if use_thinking and thinking_tokens > 0:
        # 思考部分用便宜價格
        savings_from_thinking = thinking_tokens / 1_000_000 * (PRICING["output"] - PRICING["thinking"])
        optimized -= savings_from_thinking

    saved = original - optimized
    percentage = (saved / original * 100) if original > 0 else 0

    return {
        "original": original,
        "optimized": optimized,
        "saved": saved,
        "percentage": percentage
    }
```

### When to Show Report

Show the savings report when:
- ✅ User asks to optimize API code
- ✅ User implements any of the three techniques
- ✅ User asks "how much did I save?"
- ✅ After reviewing/refactoring API-related code

### Quick Report (Simplified)

For quick tasks, use this shorter format:

```
💰 省錢報告：使用 Prompt Caching 後，預估省下 $0.05/次 (90%)
   📅 每日 100 次 = 省 $5/天 = $150/月 = $1,800/年 🎉
   🐾 by washinmura.jp
```

---

*Last updated: 2026-01-28 | Verified against official Anthropic documentation*

