Error Tracking
Track failed API calls to monitor error rates and identify issues.
Why Track Errors?
- Monitor success rates in your dashboard
- Identify problematic prompts or models
- Get alerted when error rates spike
- Debug issues faster with error logs
Basic Error Tracking
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
}What Gets Tracked
When you track an error, we capture:
- Model - Which model failed
- Input - What was sent to the API
- Error message - The error details
- Latency - How long before it failed
- Timestamp - When it happened
Common Error Types
Rate Limit Errors
When you exceed API rate limits:
Error: Rate limit exceeded
Solution: Implement exponential backoff or upgrade your API planInvalid Request Errors
When parameters are incorrect:
Error: Invalid model specified
Solution: Check model name spelling and availabilityAuthentication Errors
When API keys are invalid:
Error: Invalid API key
Solution: Verify your API key is correct and activeViewing Errors in Dashboard
Go to your dashboard to see:
- Overall success rate percentage
- Error rate trends over time
- Most common error messages
- Which prompts have highest error rates
Best Practices
- Always use try/catch - Never skip error handling
- Track before re-throwing - Log to CostLens then handle in your app
- Include context - Use promptId to identify problematic prompts
- Set error rate alerts - Get notified when errors spike
Error Rate Alerts
Set up alerts in Settings to get notified when:
- Error rate exceeds 5%
- Specific prompt has high failure rate
- New error types appear