Anthropic Cost Tuning
Overview
Optimize Claude API spend through model routing, prompt caching, the Message Batches API, and real-time cost tracking. The four biggest levers: model selection (4-19x), prompt caching (10x input), batches (2x), and max_tokens discipline.
Pricing Reference (per million tokens)
| Model |
Input |
Output |
Cache Read |
Cache Write |
| Claude Haiku |
$0.80 |
$4.00 |
$0.08 |
$1.00 |
| Claude Sonnet |
$3.00 |
$15.00 |
$0.30 |
$3.75 |
| Claude Opus |
$15.00 |
$75.00 |
$1.50 |
$18.75 |
Message Batches: 50% off all model pricing for async processing.
Cost Calculator
def estimate_cost(
input_tokens: int,
output_tokens: int,
model: str = "claude-sonnet-4-20250514",
cached_input: int = 0,
use_batch: bool = False
) -> float:
pricing = {
"claude-haiku-4-20250514": {"input": 0.80, "output": 4.00, "cache_read": 0.08},
"claude-sonnet-4-20250514": {"input": 3.00, "output": 15.00, "cache_read": 0.30},
"claude-opus-4-20250514": {"input": 15.00, "output": 75.00, "cache_read": 1.50},
}
rates = pricing[model]
uncached_input = input_tokens - cached_input
cost = (
uncached_input * rates["input"] +
cached_input * rates["cache_read"] +
output_tokens * rates["output"]
) / 1_000_000
if use_batch:
cost *= 0.5
return cost
# Example: 10K requests/day, 500 input + 200 output tokens each
daily = estimate_cost(500, 200, "claude-sonnet-4-20250514") * 10_000
print(f"Daily: ${daily:.2f}") # ~$0.045 * 10K = $450/day
print(f"Monthly: ${daily * 30:.2f}") # ~$13,500/month
# Same with Haiku + batching
daily_optimized = estimate_cost(500, 200, "claude-haiku-4-20250514", use_batch=True) * 10_000
print(f"Optimized: ${daily_optimized:.2f}/day") # ~$22/day (20x cheaper)
Strategy 1: Model Routing
def route_to_model(task: str, complexity: str) -> str:
"""Route tasks to cheapest adequate model."""
# Haiku: classification, extraction, yes/no, routing ($0.80/$4)
if task in ("classify", "extract", "route", "validate"):
return "claude-haiku-4-20250514"
# Sonnet: general tasks, code, tool use ($3/$15)
if complexity in ("low", "medium"):
return "claude-sonnet-4-20250514"
# Opus: only for complex reasoning, research ($15/$75)
return "claude-opus-4-20250514"
Strategy 2: Prompt Caching
# Cache system prompts and reference documents (90% input savings)
# Break-even: 2 requests with same cached content
message = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=256,
system=[{
"type": "text",
"text": large_reference_document, # 10K+ tokens
"cache_control": {"type": "ephemeral"}
}],
messages=[{"role": "user", "content": user_question}]
)
Strategy 3: Batches for Non-Real-Time
# 50% cost reduction for anything that doesn't need immediate response
# Ideal for: summarization pipelines, data extraction, content generation
batch = client.messages.batches.create(requests=[...]) # Up to 100K requests
Strategy 4: Spend Tracking
import anthropic
from dataclasses import dataclass, field
@dataclass
class SpendTracker:
budget_usd: float = 100.0
spent_usd: float = 0.0
requests: int = 0
def track(self, response):
cost = estimate_cost(
response.usage.input_tokens,
response.usage.output_tokens,
response.model,
getattr(response.usage, "cache_read_input_tokens", 0)
)
self.spent_usd += cost
self.requests += 1
if self.spent_usd > self.budget_usd * 0.8:
print(f"WARNING: 80% budget used (${self.spent_usd:.2f}/${self.budget_usd})")
if self.spent_usd > self.budget_usd:
raise RuntimeError(f"Budget exceeded: ${self.spent_usd:.2f}")
tracker = SpendTracker(budget_usd=50.0)
Cost Reduction Checklist
Prerequisites
- Establish an approved budget, billing owner, cost allocation dimensions, and alert thresholds before changing model routing or batch behavior.
- Use a sandbox workspace, synthetic prompts, pinned model IDs, and a versioned pricing snapshot; confirm current rates in the official pricing documentation before making a forecast.
- Configure least-privileged credentials and ensure logs/metrics contain token counts and aggregate cost only, never prompt or response content.
Instructions
- Baseline request volume, input/output/cache tokens, latency, quality, and spend by feature using a redacted measurement window. Do not make routing changes from a single outlier.
- Define a quality floor and route only eligible workloads to the least expensive model that meets it. Use prompt caching only for approved non-sensitive content and batches only where asynchronous completion is acceptable.
- Cap
max_tokens, concurrency, retries, and batch size. Enforce per-feature and per-workspace budgets before requests are sent; fail closed when a budget or scope check cannot be evaluated.
- Test the proposed policy on synthetic fixtures in a sandbox, then canary it with aggregate cost, quality, latency, error, and rate-limit monitoring. Require owner approval before broader rollout.
- If quality, spend, or policy thresholds regress, disable the new route/cache/batch policy, restore the prior configuration, and retain a redacted comparison receipt.
Output
Produce a cost-control receipt containing the pricing snapshot date, policy version, model/batch/cache decisions, token aggregates, projected and observed spend, quality and latency results, budget outcome, canary scope, approval, and rollback reference. Exclude prompt/response text, customer identifiers, API keys, and raw billing exports.
Error Handling
| Failure |
Response |
| Unknown model price or usage field |
Stop forecasting, refresh the official pricing/usage source, and mark the estimate provisional. |
| Budget or quota exceeded |
Reject or queue new work, alert the owner, and do not bypass the guard with another key or workspace. |
| Quality regression after cheaper routing |
Restore the prior route, quarantine affected output, and rerun the quality fixture before another canary. |
| Cache or batch unsuitable for data/latency policy |
Disable that optimization and use the approved synchronous, non-cached path. |
Examples
Evaluate 1,000 synthetic classification prompts in a sandbox with a fixed budget, compare pinned Sonnet against Haiku plus an approved batch policy, assert customer_content_logged=0, and emit budget=within_limit; quality=pass; canary=internal; rollback=route-v1. Do not use live customer prompts to tune pricing.
Resources
Next Steps
For architecture patterns, see anth-reference-architecture.
1---2name: anth-cost-tuning3description: Optimize Anthropic Claude API costs with model routing, prompt caching, batching, and spend monitoring. Use when analyzing Claude API billing, reducing costs, or implementing cost controls and budget alerts. Trigger with phrases like "anthropic cost", "claude billing", "reduce claude spend", "anthropic budget", "claude pricing optimize".4license: MIT5---6# Anthropic Cost Tuning
7
8## Overview
9
10Optimize Claude API spend through model routing, prompt caching, the Message Batches API, and real-time cost tracking. The four biggest levers: model selection (4-19x), prompt caching (10x input), batches (2x), and `max_tokens` discipline.
11
12## Pricing Reference (per million tokens)
13
14| Model | Input | Output | Cache Read | Cache Write |
15|-------|-------|--------|------------|-------------|
16| Claude Haiku | $0.80 | $4.00 | $0.08 | $1.00 |
17| Claude Sonnet | $3.00 | $15.00 | $0.30 | $3.75 |
18| Claude Opus | $15.00 | $75.00 | $1.50 | $18.75 |
19
20**Message Batches:** 50% off all model pricing for async processing.
21
22## Cost Calculator
23
24```python
25def estimate_cost(
26 input_tokens: int,
27 output_tokens: int,
28 model: str = "claude-sonnet-4-20250514",
29 cached_input: int = 0,
30 use_batch: bool = False
31) -> float:
32 pricing = {
33 "claude-haiku-4-20250514": {"input": 0.80, "output": 4.00, "cache_read": 0.08},
34 "claude-sonnet-4-20250514": {"input": 3.00, "output": 15.00, "cache_read": 0.30},
35 "claude-opus-4-20250514": {"input": 15.00, "output": 75.00, "cache_read": 1.50},
36 }
37 rates = pricing[model]
38 uncached_input = input_tokens - cached_input
39
40 cost = (
41 uncached_input * rates["input"] +
42 cached_input * rates["cache_read"] +
43 output_tokens * rates["output"]
44 ) / 1_000_000
45
46 if use_batch:
47 cost *= 0.5
48
49 return cost
50
51# Example: 10K requests/day, 500 input + 200 output tokens each
52daily = estimate_cost(500, 200, "claude-sonnet-4-20250514") * 10_000
53print(f"Daily: ${daily:.2f}") # ~$0.045 * 10K = $450/day
54print(f"Monthly: ${daily * 30:.2f}") # ~$13,500/month
55
56# Same with Haiku + batching
57daily_optimized = estimate_cost(500, 200, "claude-haiku-4-20250514", use_batch=True) * 10_000
58print(f"Optimized: ${daily_optimized:.2f}/day") # ~$22/day (20x cheaper)
59```
60
61## Strategy 1: Model Routing
62
63```python
64def route_to_model(task: str, complexity: str) -> str:
65 """Route tasks to cheapest adequate model."""
66 # Haiku: classification, extraction, yes/no, routing ($0.80/$4)
67 if task in ("classify", "extract", "route", "validate"):
68 return "claude-haiku-4-20250514"
69
70 # Sonnet: general tasks, code, tool use ($3/$15)
71 if complexity in ("low", "medium"):
72 return "claude-sonnet-4-20250514"
73
74 # Opus: only for complex reasoning, research ($15/$75)
75 return "claude-opus-4-20250514"
76```
77
78## Strategy 2: Prompt Caching
79
80```python
81# Cache system prompts and reference documents (90% input savings)
82# Break-even: 2 requests with same cached content
83message = client.messages.create(
84 model="claude-sonnet-4-20250514",
85 max_tokens=256,
86 system=[{
87 "type": "text",
88 "text": large_reference_document, # 10K+ tokens
89 "cache_control": {"type": "ephemeral"}
90 }],
91 messages=[{"role": "user", "content": user_question}]
92)
93```
94
95## Strategy 3: Batches for Non-Real-Time
96
97```python
98# 50% cost reduction for anything that doesn't need immediate response
99# Ideal for: summarization pipelines, data extraction, content generation
100batch = client.messages.batches.create(requests=[...]) # Up to 100K requests
101```
102
103## Strategy 4: Spend Tracking
104
105```python
106import anthropic
107from dataclasses import dataclass, field
108
109@dataclass
110class SpendTracker:
111 budget_usd: float = 100.0
112 spent_usd: float = 0.0
113 requests: int = 0
114
115 def track(self, response):
116 cost = estimate_cost(
117 response.usage.input_tokens,
118 response.usage.output_tokens,
119 response.model,
120 getattr(response.usage, "cache_read_input_tokens", 0)
121 )
122 self.spent_usd += cost
123 self.requests += 1
124
125 if self.spent_usd > self.budget_usd * 0.8:
126 print(f"WARNING: 80% budget used (${self.spent_usd:.2f}/${self.budget_usd})")
127 if self.spent_usd > self.budget_usd:
128 raise RuntimeError(f"Budget exceeded: ${self.spent_usd:.2f}")
129
130tracker = SpendTracker(budget_usd=50.0)
131```
132
133## Cost Reduction Checklist
134
135- [ ] Use Haiku for classification/extraction/routing tasks
136- [ ] Enable prompt caching for repeated system prompts
137- [ ] Use Message Batches for non-real-time processing
138- [ ] Set `max_tokens` to realistic values (not maximum)
139- [ ] Use prefill to reduce output preamble tokens
140- [ ] Implement spend tracking and budget alerts
141- [ ] Monitor via [Usage API](https://docs.anthropic.com/en/api/usage-cost-api)
142
143## Prerequisites
144
145- Establish an approved budget, billing owner, cost allocation dimensions, and alert thresholds before changing model routing or batch behavior.
146- Use a sandbox workspace, synthetic prompts, pinned model IDs, and a versioned pricing snapshot; confirm current rates in the official pricing documentation before making a forecast.
147- Configure least-privileged credentials and ensure logs/metrics contain token counts and aggregate cost only, never prompt or response content.
148
149## Instructions
150
1511. Baseline request volume, input/output/cache tokens, latency, quality, and spend by feature using a redacted measurement window. Do not make routing changes from a single outlier.
1522. Define a quality floor and route only eligible workloads to the least expensive model that meets it. Use prompt caching only for approved non-sensitive content and batches only where asynchronous completion is acceptable.
1533. Cap `max_tokens`, concurrency, retries, and batch size. Enforce per-feature and per-workspace budgets before requests are sent; fail closed when a budget or scope check cannot be evaluated.
1544. Test the proposed policy on synthetic fixtures in a sandbox, then canary it with aggregate cost, quality, latency, error, and rate-limit monitoring. Require owner approval before broader rollout.
1555. If quality, spend, or policy thresholds regress, disable the new route/cache/batch policy, restore the prior configuration, and retain a redacted comparison receipt.
156
157## Output
158
159Produce a cost-control receipt containing the pricing snapshot date, policy version, model/batch/cache decisions, token aggregates, projected and observed spend, quality and latency results, budget outcome, canary scope, approval, and rollback reference. Exclude prompt/response text, customer identifiers, API keys, and raw billing exports.
160
161## Error Handling
162
163| Failure | Response |
164|---|---|
165| Unknown model price or usage field | Stop forecasting, refresh the official pricing/usage source, and mark the estimate provisional. |
166| Budget or quota exceeded | Reject or queue new work, alert the owner, and do not bypass the guard with another key or workspace. |
167| Quality regression after cheaper routing | Restore the prior route, quarantine affected output, and rerun the quality fixture before another canary. |
168| Cache or batch unsuitable for data/latency policy | Disable that optimization and use the approved synchronous, non-cached path. |
169
170## Examples
171
172Evaluate 1,000 synthetic classification prompts in a sandbox with a fixed budget, compare pinned Sonnet against Haiku plus an approved batch policy, assert `customer_content_logged=0`, and emit `budget=within_limit; quality=pass; canary=internal; rollback=route-v1`. Do not use live customer prompts to tune pricing.
173
174## Resources
175
176- [Pricing](https://docs.anthropic.com/en/docs/about-claude/pricing)
177- [Prompt Caching](https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching)
178- [Usage & Cost API](https://docs.anthropic.com/en/api/usage-cost-api)
179- [Message Batches](https://docs.anthropic.com/en/api/creating-message-batches)
180
181## Next Steps
182
183For architecture patterns, see `anth-reference-architecture`.