If you've started integrating Claude, GPT, or other LLMs into your apps, you've probably had that moment. You check your billing. Your jaw drops. A single feature test ran through $200 worth of tokens.
Yeah. Welcome to the fun part of building with AI.
The good news? You can drastically cut costs without sacrificing quality. I've helped teams drop their API spend by 70% without changing what users see. Here's how.
1. Batch Your Requests (This Alone Cuts 50%)
Most people fire off API calls one at a time. Batch processing lets you send hundreds of requests together and get a discount.
Claude's Batch API? 50% cheaper. OpenAI's? Similar deal.
Instead of:
# ❌ One at a time - expensive
for email in emails:
response = client.messages.create(
model="claude-3-5-sonnet-20241022",
messages=[{"role": "user", "content": f"Classify: {email}"}]
)
Do this:
# ✅ Batch - 50% discount
requests = [
{
"custom_id": f"email-{i}",
"params": {
"model": "claude-3-5-sonnet-20241022",
"messages": [{"role": "user", "content": f"Classify: {email}"}]
}
}
for i, email in enumerate(emails)
]
batch = client.messages.batches.create(requests=requests)
If batching takes 24 hours to process, that's fine — batch non-urgent work overnight.
2. Use Cheaper Models for Simple Tasks
You don't need GPT-4 to classify spam or extract structured data. Smaller models are way faster and cheaper.
- GPT-4o: Complex reasoning, creative work, novel problems
- Claude 3.5 Haiku: Fast summaries, classification, simple extraction
- Llama 3.1 (self-hosted or via API): Costs nearly nothing
Real example: A content moderation pipeline was costing $8k/month using GPT-4 for every comment. Switching classifier tasks to Haiku? $400/month. Same accuracy.
Test with Haiku first. Upgrade to Sonnet only if needed.
3. Cache Your Prompts (Free Tokens After First Call)
System prompts and large context documents get reused. Cache them.
With Claude:
response = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
system=[
{
"type": "text",
"text": HUGE_SYSTEM_PROMPT,
"cache_control": {"type": "ephemeral"}
}
],
messages=[{"role": "user", "content": user_query}]
)
After the first call, you pay full price. Every call after that? The cached system prompt is free.
If your system prompt is 50k tokens and you make 100 calls per day:
- Without cache: 50k × 100 = 5M tokens/day
- With cache: 50k × 1 + (1k × 99) ≈ 149k tokens/day
The math compounds.
4. Stream Responses (Faster Perception, Lower Latency Costs)
Streaming doesn't technically save tokens, but it feels faster to users, and it limits unnecessary processing.
with client.messages.stream(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
messages=[{"role": "user", "content": "Write a poem"}]
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
Users see output immediately instead of waiting for the full response. Perception of speed = better UX, and you're not computing tokens you don't need.
5. Use Structured Output (Fewer Tokens, Cleaner Results)
JSON mode saves tokens because the model doesn't generate filler text or natural language waffling.
Instead of:
"The user's sentiment is positive because they used exclamation marks and positive adjectives"
You get:
{"sentiment": "positive", "confidence": 0.92}
With Claude:
response = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
messages=[...],
thinking={"type": "enabled", "budget_tokens": 5000} # Even thinking is optional
)
Structured output = fewer token waste on explanation.
6. Track and Alert on Spending
You can't optimize what you don't measure.
Set up spending alerts:
- OpenAI: Organization settings → Billing → Usage limits
- Claude: Check usage dashboard daily
- DIY: Log every API call with token count, add to a Sheets
One team I know tags every API call with a project name:
headers = {
"anthropic-api-key": API_KEY,
"x-user-id": "project-name-123" # Track per project
}
Now you know which feature is burning cash.
The Real Win
The teams cutting costs aren't doing anything magical. They're just:
- Batching non-urgent work
- Using the right model for the job
- Caching repetitive context
- Monitoring spend like it matters
Start with #1 and #2. Implement caching next. You'll cut costs by 60-70% without touching product.
Want more practical AI workflows and cost optimization patterns? Check out LearnAI Weekly newsletter — real strategies, no AI hype.
Top comments (0)