Loading...
Loading...
Unlock the full power of CostLens SDK with caching, middleware, retries, and more.
Reduce API costs by up to 80% by caching identical requests:
const costlens = new CostLens({
apiKey: process.env.COSTLENS_API_KEY,
enableCache: true // Enable caching globally
});
const tracked = costlens.wrapOpenAI(openai);
// Cache this specific request for 1 hour
const result = await tracked.chat.completions.create(
{ model: 'gpt-4', messages: [...] },
{ cacheTTL: 3600000 } // 1 hour in milliseconds
);
// Second identical call returns cached result instantly
const cached = await tracked.chat.completions.create(
{ model: 'gpt-4', messages: [...] },
{ cacheTTL: 3600000 }
);
// Clear cache when needed
costlens.clearCache();
💡 Use cases: FAQ responses, static content generation, repeated queries
Automatically retry failed requests with exponential backoff:
const costlens = new CostLens({
apiKey: process.env.COSTLENS_API_KEY,
maxRetries: 3 // Retry up to 3 times
});
// Automatically retries on 5xx errors with backoff: 1s, 2s, 4s
const tracked = costlens.wrapOpenAI(openai);
const result = await tracked.chat.completions.create({
model: 'gpt-4',
messages: [...]
});
// ✅ Handles transient failures automatically
Add custom logic before/after API calls:
const costlens = new CostLens({
apiKey: process.env.COSTLENS_API_KEY,
middleware: [
{
// Run before API call
before: async (params) => {
console.log('Making API call with model:', params.model);
// Modify params if needed
params.temperature = params.temperature || 0.7;
return params;
},
// Run after successful API call
after: async (result) => {
console.log('Tokens used:', result.usage?.total_tokens);
// Transform result if needed
return result;
},
// Run on error
onError: async (error) => {
console.error('API call failed:', error.message);
// Send to error tracking service
}
}
]
});
Track streaming responses automatically:
const tracked = costlens.wrapOpenAI(openai);
const stream = await tracked.chat.completions.stream({
model: 'gpt-4',
messages: [{ role: 'user', content: 'Tell me a long story' }]
});
// Stream chunks to user
for await (const chunk of stream) {
const content = chunk.choices[0]?.delta?.content || '';
process.stdout.write(content);
}
// ✅ Automatically tracked after stream completes
// Includes full content and estimated token count
Efficiently track multiple calls at once:
// Track multiple calls in one request
await costlens.trackBatch([
{ provider: 'openai', model: 'gpt-4', tokens: 100, latency: 500 },
{ provider: 'openai', model: 'gpt-3.5-turbo', tokens: 50, latency: 200 },
{ provider: 'anthropic', model: 'claude-3-opus', tokens: 150, latency: 600 }
]);
// Useful for background jobs or bulk processing
Tag prompts to analyze by use case:
const tracked = costlens.wrapOpenAI(openai);
// Tag with promptId for analytics
const result = await tracked.chat.completions.create(
{ model: 'gpt-4', messages: [...] },
{ promptId: 'customer-support-v2' }
);
// View analytics grouped by promptId in dashboard
// Compare performance across different prompt versions
interface CostLensConfig {
apiKey: string; // Required: Your API key
baseUrl?: string; // Optional: Custom endpoint
enableCache?: boolean; // Optional: Enable caching (default: false)
maxRetries?: number; // Optional: Max retry attempts (default: 3)
middleware?: Middleware[]; // Optional: Custom middleware
}
interface Middleware {
before?: (params: any) => Promise<any>;
after?: (result: any) => Promise<any>;
onError?: (error: Error) => Promise<void>;
}