Loading...
Loading...
Track failed API calls to monitor error rates and identify issues.
import { CostLens } from 'costlens';
const costlens = new CostLens({
apiKey: process.env.COSTLENS_API_KEY
});
const start = Date.now();
try {
const result = await openai.chat.completions.create(params);
await costlens.trackOpenAI(params, result, Date.now() - start);
} catch (error) {
// Track the error
await costlens.trackError(
'openai',
params.model,
JSON.stringify(params.messages),
error,
Date.now() - start
);
throw error; // Re-throw to handle in your app
}
When you track an error, we capture:
When you exceed API rate limits:
Error: Rate limit exceeded
Solution: Implement exponential backoff or upgrade your API plan
When parameters are incorrect:
Error: Invalid model specified
Solution: Check model name spelling and availability
When API keys are invalid:
Error: Invalid API key
Solution: Verify your API key is correct and active
Go to your dashboard to see:
Set up alerts in Settings to get notified when: