You are using Claude Code and hit:
Error: Rate limit exceeded. Please wait before retrying.
Or your costs are higher than expected. Here is how to manage it.
Fix 1: Check your current limits
Anthropic has different limits based on your plan:
# Check API usage
curl https://api.anthropic.com/v1/usage \
-H "x-api-key: $ANTHROPIC_API_KEY"
Limits by plan:
- Free tier: Limited requests
- Pay-as-you-go: $100/month initial limit
- Max subscription: Higher limits
Fix 2: Use prompt caching
Claude Code supports prompt caching, reducing token usage:
# Cache system prompts and repeated context
response = client.messages.create(
model="claude-sonnet-5",
max_tokens=4096,
system=[
{
"type": "text",
"text": "Your long system prompt here...",
"cache_control": {"type": "ephemeral"}
}
],
messages=[{"role": "user", "content": "Your request"}]
)
Caching reduces cost by 90% for repeated context.
Fix 3: Use Sonnet instead of Opus
Sonnet is cheaper than Opus:
| Model | Input cost | Output cost | Best for |
|---|---|---|---|
| Opus 4.8 | $15/1M | $75/1M | Complex reasoning |
| Sonnet 5 | $3/1M | $15/1M | Most tasks |
Use Sonnet for routine tasks, Opus only when needed:
# In Claude Code, switch models
/model claude-sonnet-5
Fix 4: Reduce context size
Smaller prompts use fewer tokens:
# Instead of sending entire files
cat huge_file.py | claude "explain this"
# Send only relevant parts
head -100 huge_file.py | claude "explain this"
Claude Codeβs /compact command reduces context:
/compact
Fix 5: Use batch API for bulk tasks
For multiple independent requests, use the batch API:
# Batch API is 50% cheaper
response = client.messages.create(
model="claude-sonnet-5",
max_tokens=1024,
messages=[{"role": "user", "content": "task"}],
metadata={"user_id": "batch-job-1"}
)
Fix 6: Monitor costs
Track your spending:
# Check recent API calls
curl https://api.anthropic.com/v1/usage \
-H "x-api-key: $ANTHROPIC_API_KEY" | jq '.daily_costs'
Set up billing alerts in the Anthropic console.
Fix 7: Use OpenRouter as alternative
OpenRouter may have different rate limits and pricing:
# Configure Claude Code to use OpenRouter
export ANTHROPIC_BASE_URL=https://openrouter.ai/api/v1
export ANTHROPIC_API_KEY=your-openrouter-key
OpenRouterβs pricing may differ from direct Anthropic API.
Still hitting limits?
- Request limit increase β Contact Anthropic support
- Use local models β Ollama for routine tasks, Claude for complex ones
- Optimize prompts β Shorter, more specific prompts use fewer tokens
- Cache aggressively β System prompts and tool definitions should be cached
FAQ
How much does Claude Code cost per month?
It depends on usage. With Sonnet 5 ($3/$15 per 1M tokens), moderate use costs $50-100/month. Heavy use can cost $200+. Using prompt caching reduces costs by 90% for repeated context.
Can I use Claude Code for free?
Not directly. Claude Code uses Anthropicβs API, which requires payment. However, you can use the free tier of Claude (limited requests) or switch to local models with Ollama for free inference.
How do I reduce Claude Code costs?
Use Sonnet instead of Opus for routine tasks, enable prompt caching, reduce context size, and use the /compact command regularly. See our AI API Pricing guide for cost optimization strategies.
Related: Claude Code Complete Guide Β· Claude Code vs Aider Β· AI API Pricing Compared 2026 Β· OpenRouter Complete Guide Β· Best Free AI APIs 2026