API Monitoring
The API Monitoring system provides comprehensive tracking, rate limit management, and intelligent alerting for all external API services used by TikTok Miner.Architecture
Core Components
APIUsageTracker
Tracks all API requests and calculates costs automatically. Location:lib/services/api-usage-tracker.ts
APIAlertManager
Monitors usage patterns and triggers alerts based on configurable rules. Location:lib/services/api-alert-manager.ts
Tracking API Usage
Basic Usage Tracking
lib/services/api-usage-tracker.ts:52-78
Automatic Cost Calculation
lib/services/api-usage-tracker.ts:214-238
Tracking Errors
Rate Limit Management
Configuring Rate Limits
prisma/schema.prisma:272-286
Checking Rate Limit Status
lib/services/api-usage-tracker.ts:118-147
Pre-Request Rate Limit Check
Usage in Time Windows
lib/services/api-usage-tracker.ts:80-116
Alert System
Alert Types
prisma/schema.prisma:321-327
Default Alert Configuration
lib/services/api-alert-manager.ts:25-36
Evaluating Alerts
lib/services/api-alert-manager.ts:77-98
Custom Alert Rules
Create custom alert rules with complex logic:lib/services/api-alert-manager.ts:38-71
Alert Resolution
lib/services/api-alert-manager.ts:402-414
Usage Statistics
Get Statistics for Time Period
lib/services/api-usage-tracker.ts:167-212
Cost Reports
Generate cost reports grouped by different dimensions:lib/services/api-usage-tracker.ts:301-346
Alert Statistics
lib/services/api-alert-manager.ts:435-478
Database Schema
ApiUsage Model
Stores all API request metrics:prisma/schema.prisma:249-270
ApiLimit Model
prisma/schema.prisma:272-286
ApiAlert Model
prisma/schema.prisma:305-319
Integration Examples
OpenAI Integration
Apify Integration
Monitoring Dashboard
Real-Time Monitoring
Best Practices
1. Always Track Requests
2. Check Rate Limits Proactively
3. Set Up Automated Alerts
4. Monitor Error Rates
FAQ: API Monitoring
FAQ: API Monitoring
Q: How long is usage data retained?A: By default, Q: How do I set up cost budgets?A: Use the Budget Management system, which integrates with API monitoring through
ApiUsage records are retained indefinitely for cost tracking and analytics. Consider archiving old data (>90 days) to a data warehouse if database size becomes an issue.Q: Can I set different rate limits for different users?A: Currently, rate limits are per platform/model. To implement per-user limits, extend the ApiLimit model to include a userId field and modify the rate limit checking logic.Q: How are alerts sent?A: Alerts are stored in the database and logged. Implement notification channels (email, Slack, webhook) in the sendNotifications method at lib/services/api-alert-manager.ts:352-364.Q: What happens if rate limit checking fails?A: Rate limit checking is best-effort. If it fails, the request proceeds normally but the error is logged. Always track the actual request result for accurate metrics.Q: Can I retroactively calculate costs?A: Yes, if you have tokensUsed but not cost in existing records:CostAllocation records linked to ApiUsage.Next Steps
- Set up Budget Management for cost control
- Configure alert notifications for your team
- Build custom dashboards with the monitoring APIs
- Integrate monitoring into your CLI Tools