Cost Optimization
Strategies to reduce your API spending while maintaining quality results.
Overview
Optimizing API costs is crucial for building scalable applications. The following strategies will help you reduce spending while maintaining high quality outputs and performance.
Efficient Batching
Process multiple requests together to reduce overhead and improve efficiency:
// Efficient: Batch multiple requests
const responses = await Promise.all([
api.createPrediction(input1),
api.createPrediction(input2),
api.createPrediction(input3),
]);
// Less efficient: Sequential requests
const result1 = await api.createPrediction(input1);
const result2 = await api.createPrediction(input2);
const result3 = await api.createPrediction(input3);Caching Strategies
Implement smart caching to avoid redundant API calls:
Response Caching
Cache successful API responses with appropriate TTL based on input stability. Store results keyed by input hash to ensure consistent outputs.
Model Selection
Use appropriate model sizes for your use case. Smaller models are faster and cheaper while still providing excellent quality for many applications.
Request Deduplication
Detect and prevent duplicate requests before they reach the API. Monitor for similar inputs from different users that could share the same output.
Resource Optimization
Optimize the resources you request to match your needs:
| Strategy | Impact | Notes |
|---|---|---|
| Resolution Scaling | High | Lower resolutions reduce processing costs |
| Quality Settings | Medium | Adjust quality vs cost tradeoff |
| Async Processing | High | Use webhooks instead of polling |
| Scheduled Batches | High | Process non-urgent jobs during off-peak hours |
Monitoring and Analytics
Track and analyze your usage patterns to identify optimization opportunities:
Key Metrics to Track
- Average cost per request and per output
- Cache hit rate and duplicate detection rate
- Peak usage hours and patterns
- Model and resource utilization by feature
- Error rates and failed request costs
Cost Optimization Best Practices
✓ Recommended Practices
- Implement request deduplication and response caching
- Use webhooks for asynchronous processing
- Batch requests when possible
- Monitor API usage and costs regularly
- Start with conservative resource settings and adjust based on requirements
- Schedule batch jobs during off-peak hours
✗ Avoid These Practices
- Making redundant requests for the same input
- Polling for results - use webhooks instead
- Requesting higher resolution/quality than necessary
- Processing requests synchronously when async would work
- Ignoring usage patterns and cost trends
Cost Tracking Setup
Monitor your spending with our Account API:
const response = await fetch('https://api.deepcreativelabs.com/v1/account/balance', {
headers: {
'Authorization': 'Bearer YOUR_API_KEY'
}
});
const { balance, spent_this_month, limit } = await response.json();
console.log(`Balance: $${balance}`);
console.log(`Spent this month: $${spent_this_month}`);
console.log(`Monthly limit: $${limit}`);