Back to skills
SKILL.md
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
Works with
Security analysis
100/100npx -y skills add tools-only/X-Skills --skill 141-claude-api-cost-optimizationskill_4bdeb26a --agent claude-codeAre you the author of 141 Claude Api Cost Optimizationskill 4bdeb26a?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.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*
Attribution
Comments
Loading commentsโฆ
Newsletter
Get the weekly list of safe new skills, and the ones we pulled.