Loading...
Loading...
Get 3-5x better performance with batch processing, smart caching, and circuit breaker protection.
Batch Processing: 3-5x faster with reduced HTTP overhead
Smart Caching: 90% faster authentication with 5-minute cache
Circuit Breaker: 99.9% uptime with automatic failure protection
Smart Routing: Automatic task detection and optimal model selection
Get started with performance optimizations in 5 minutes:
npm install costlensimport CostLens from 'costlens';
const costlens = new CostLens({
apiKey: 'your-api-key',
smartRouting: true, // 💰 Auto-route to cheaper models
enableCache: true, // 💰 Cache repeated requests
autoOptimize: true, // 💰 Optimize prompts automatically
costLimit: 0.10, // 💰 Max $0.10 per request
});// Instead of individual requests:
for (const request of requests) {
await costlens.trackOpenAI(request.params, request.result, request.latency);
}
// Use batch processing for 3-5x better performance:
await costlens.trackBatch([
{ provider: 'openai', model: 'gpt-4', tokens: 150, latency: 1200 },
{ provider: 'anthropic', model: 'claude-3', tokens: 200, latency: 1000 },
{ provider: 'openai', model: 'gpt-3.5-turbo', tokens: 100, latency: 800 }
]);// Check your savings and performance
const analytics = costlens.getCostAnalytics();
console.log('Cache Hit Rate:', analytics.cacheHitRate * 100 + '%');
console.log('Total Savings: $' + analytics.totalSavings);
console.log('Average Latency:', analytics.averageLatency + 'ms');
console.log('Error Rate:', analytics.errorRate * 100 + '%');
// Calculate potential savings
const savings = await costlens.calculateSavings('gpt-4', messages);
console.log('Potential Savings: ' + savings.savingsPercentage + '%');
console.log('Recommended Model: ' + savings.recommendedModel);Here's how to optimize a customer support system with batch processing:
// Before: Slow and expensive
const responses = [];
for (const query of customerQueries) {
const response = await openai.chat.completions.create({
model: 'gpt-4', // Always expensive
messages: [{ role: 'user', content: query.question }]
});
responses.push(response);
}
// Result: 10 requests = 10 API calls = $0.50 = 5 seconds
// After: Fast and cheap with optimizations
const costlens = new CostLens({
apiKey: 'your-api-key',
smartRouting: true, // Auto-route to cheaper models
enableCache: true, // Cache repeated requests
autoOptimize: true, // Optimize prompts
});
const tracked = costlens.wrapOpenAI(openai);
const responses = [];
// Process in batches for 3-5x better performance
const batchSize = 5;
for (let i = 0; i < customerQueries.length; i += batchSize) {
const batch = customerQueries.slice(i, i + batchSize);
// Process batch
const batchPromises = batch.map(async (query) => {
const response = await tracked.chat.completions.create({
model: 'gpt-4', // Automatically routes to GPT-3.5-turbo for simple tasks
messages: [{ role: 'user', content: query.question }]
});
return response;
});
const batchResults = await Promise.all(batchPromises);
responses.push(...batchResults);
}
// Result: 10 requests = 2 batches = $0.10 = 1 second (3-5x faster, significantly cheaper)
// Check your savings
const analytics = costlens.getCostAnalytics();
console.log(`Saved $${analytics.totalSavings} with ${analytics.cacheHitRate * 100}% cache hit rate`);