Skip to content
Back to skills

141 Claude Api Cost Optimizationskill 4bdeb26a

ASecurity

> ๐Ÿ’ฐ Save 50-90% on Claude API costs with three officially verified techniques

  • 9 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added October 11, 2026
code-qualitypythondebuggingrefactoringapiperformancedocumentation

Works with

  • cli
  • api

Security analysis

A100/100

Scanned October 11, 2026

npx -y skills add tools-only/X-Skills --skill 141-claude-api-cost-optimizationskill_4bdeb26a --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of 141 Claude Api Cost Optimizationskill 4bdeb26a?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for 141 Claude Api Cost Optimizationskill 4bdeb26a
[![Security: A โ€” Skills Directory](https://www.skillsdirectory.com/api/skills/tools-only-141-claude-api-cost-optimizationskill-4bdeb26a/badge)](https://www.skillsdirectory.com/skills/tools-only-141-claude-api-cost-optimizationskill-4bdeb26a)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

SKILL.md
# 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*

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading commentsโ€ฆ

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.