Monitoring & Analytics
Monitoring & Analytics
Section titled “Monitoring & Analytics”This guide covers how to effectively monitor and analyze your AI usage with UniCraft’s comprehensive monitoring and analytics features.
Overview
Section titled “Overview”UniCraft provides detailed monitoring and analytics to help you:
- Track usage patterns and costs
- Monitor performance metrics
- Identify optimization opportunities
- Set up alerts and notifications
- Generate reports and insights
Dashboard Overview
Section titled “Dashboard Overview”Key Metrics Dashboard
Section titled “Key Metrics Dashboard”The main dashboard displays essential metrics at a glance:
// Get dashboard metricsconst metrics = await unicraft.analytics.getDashboardMetrics({ time_range: "7d", group_by: "day",});
console.log("Total Requests:", metrics.total_requests);console.log("Total Cost:", metrics.total_cost);console.log("Average Response Time:", metrics.avg_response_time);console.log("Success Rate:", metrics.success_rate);Real-time Monitoring
Section titled “Real-time Monitoring”Monitor your AI usage in real-time:
// Subscribe to real-time metricsconst subscription = unicraft.analytics.subscribe({ metrics: ["requests", "cost", "response_time", "errors"], callback: (data) => { console.log("Real-time update:", data); },});Usage Analytics
Section titled “Usage Analytics”Request Patterns
Section titled “Request Patterns”Analyze your request patterns to understand usage:
// Get usage patternsconst patterns = await unicraft.analytics.getUsagePatterns({ start_date: "2024-01-01", end_date: "2024-01-31", group_by: "hour", metrics: ["requests", "tokens", "cost"],});
// Analyze peak usage timesconst peakHours = patterns.filter((p) => p.requests > average_requests * 1.5);console.log("Peak usage hours:", peakHours);Provider Comparison
Section titled “Provider Comparison”Compare performance across different providers:
// Compare providersconst comparison = await unicraft.analytics.compareProviders({ time_range: "30d", metrics: ["response_time", "cost", "success_rate", "availability"],});
console.log("Provider comparison:", comparison);Model Performance
Section titled “Model Performance”Track performance metrics for each model:
// Get model performanceconst modelPerformance = await unicraft.analytics.getModelPerformance({ time_range: "7d", models: ["gpt-4", "gpt-3.5-turbo", "claude-3-sonnet"],});
modelPerformance.forEach((model) => { console.log(`${model.name}:`); console.log(` Avg Response Time: ${model.avg_response_time}ms`); console.log(` Cost per Request: $${model.avg_cost}`); console.log(` Success Rate: ${model.success_rate}%`);});Cost Analytics
Section titled “Cost Analytics”Spending Trends
Section titled “Spending Trends”Monitor your spending patterns over time:
// Get cost trendsconst costTrends = await unicraft.analytics.getCostTrends({ start_date: "2024-01-01", end_date: "2024-01-31", group_by: "day", breakdown: ["provider", "model"],});
// Calculate daily averagesconst dailyAverage = costTrends.reduce((sum, day) => sum + day.cost, 0) / costTrends.length;console.log("Daily average cost:", dailyAverage);Cost Optimization Insights
Section titled “Cost Optimization Insights”Get recommendations for cost optimization:
// Get optimization recommendationsconst recommendations = await unicraft.analytics.getOptimizationRecommendations( { time_range: "30d", });
recommendations.forEach((rec) => { console.log(`Recommendation: ${rec.title}`); console.log(`Potential savings: $${rec.potential_savings}`); console.log(`Description: ${rec.description}`);});Budget Management
Section titled “Budget Management”Set up and monitor spending budgets:
// Create budgetconst budget = await unicraft.budgets.create({ name: "Monthly AI Budget", amount: 1000, period: "monthly", alerts: [0.5, 0.8, 0.9, 1.0], // Alert at 50%, 80%, 90%, and 100%});
// Get budget statusconst budgetStatus = await unicraft.budgets.getStatus(budget.id);console.log("Budget status:", budgetStatus);Performance Monitoring
Section titled “Performance Monitoring”Response Time Tracking
Section titled “Response Time Tracking”Monitor response times across providers and models:
// Get response time metricsconst responseTimes = await unicraft.analytics.getResponseTimes({ time_range: "7d", group_by: "provider", percentiles: [50, 90, 95, 99],});
responseTimes.forEach((provider) => { console.log(`${provider.name}:`); console.log(` P50: ${provider.p50}ms`); console.log(` P90: ${provider.p90}ms`); console.log(` P95: ${provider.p95}ms`); console.log(` P99: ${provider.p99}ms`);});Error Rate Monitoring
Section titled “Error Rate Monitoring”Track and analyze errors:
// Get error analysisconst errorAnalysis = await unicraft.analytics.getErrorAnalysis({ time_range: "7d", group_by: "error_type",});
errorAnalysis.forEach((error) => { console.log(`${error.type}: ${error.count} occurrences`); console.log(` Rate: ${error.rate}%`); console.log(` Trend: ${error.trend}`);});Availability Monitoring
Section titled “Availability Monitoring”Monitor provider availability:
// Get availability metricsconst availability = await unicraft.analytics.getAvailability({ time_range: "30d", providers: ["openai", "anthropic", "google"],});
availability.forEach((provider) => { console.log(`${provider.name}:`); console.log(` Uptime: ${provider.uptime}%`); console.log(` Downtime: ${provider.downtime} minutes`); console.log(` Incidents: ${provider.incidents}`);});Custom Dashboards
Section titled “Custom Dashboards”Creating Custom Dashboards
Section titled “Creating Custom Dashboards”Create custom dashboards for specific use cases:
// Create custom dashboardconst dashboard = await unicraft.dashboards.create({ name: "Production Monitoring", description: "Monitor production AI usage", widgets: [ { type: "line_chart", title: "Request Volume", metric: "requests", time_range: "24h", group_by: "hour", }, { type: "gauge", title: "Success Rate", metric: "success_rate", threshold: 0.95, time_range: "1h", }, { type: "bar_chart", title: "Cost by Provider", metric: "cost", group_by: "provider", time_range: "7d", }, ],});Dashboard Widgets
Section titled “Dashboard Widgets”Available widget types:
const widgetTypes = { line_chart: "Time series data visualization", bar_chart: "Categorical data comparison", pie_chart: "Proportional data representation", gauge: "Single metric with threshold", table: "Tabular data display", metric: "Single value display",};Alerts and Notifications
Section titled “Alerts and Notifications”Setting Up Alerts
Section titled “Setting Up Alerts”Configure alerts for important metrics:
// Create alertconst alert = await unicraft.alerts.create({ name: "High Error Rate", description: "Alert when error rate exceeds 5%", condition: "error_rate > 0.05", duration: "5m", channels: ["email", "slack"], recipients: ["admin@company.com", "#ai-alerts"],});
// Create cost alertconst costAlert = await unicraft.alerts.create({ name: "Budget Exceeded", description: "Alert when monthly budget is exceeded", condition: "monthly_cost > budget_amount", duration: "1m", channels: ["email", "webhook"], webhook_url: "https://your-app.com/webhooks/budget-alert",});Alert Channels
Section titled “Alert Channels”Configure different notification channels:
// Email notificationsconst emailChannel = { type: "email", recipients: ["admin@company.com", "team@company.com"], template: "alert_template",};
// Slack notificationsconst slackChannel = { type: "slack", webhook_url: "https://hooks.slack.com/services/...", channel: "#ai-alerts",};
// Webhook notificationsconst webhookChannel = { type: "webhook", url: "https://your-app.com/webhooks/unicraft-alerts", headers: { Authorization: "Bearer your-token", },};Reporting
Section titled “Reporting”Automated Reports
Section titled “Automated Reports”Schedule automated reports:
// Create scheduled reportconst report = await unicraft.reports.create({ name: "Weekly AI Usage Report", template: "weekly_usage", schedule: "0 9 * * 1", // Every Monday at 9 AM recipients: ["team@company.com"], format: "pdf", include_charts: true,});Custom Reports
Section titled “Custom Reports”Generate custom reports:
// Generate custom reportconst customReport = await unicraft.reports.generate({ name: "Q1 AI Analysis", start_date: "2024-01-01", end_date: "2024-03-31", sections: [ "usage_summary", "cost_analysis", "performance_metrics", "provider_comparison", ], format: "pdf", include_raw_data: true,});Export Options
Section titled “Export Options”Export data in various formats:
// Export usage dataconst usageData = await unicraft.analytics.export({ start_date: "2024-01-01", end_date: "2024-01-31", format: "csv", include_breakdown: true,});
// Export cost dataconst costData = await unicraft.analytics.export({ start_date: "2024-01-01", end_date: "2024-01-31", format: "json", group_by: "provider",});Integration
Section titled “Integration”Webhook Integration
Section titled “Webhook Integration”Set up webhooks for real-time notifications:
// Create webhookconst webhook = await unicraft.webhooks.create({ name: "Usage Notifications", url: "https://your-app.com/webhooks/unicraft", events: [ "request.completed", "error.occurred", "budget.exceeded", "provider.unavailable", ], secret: "your-webhook-secret",});Third-party Integrations
Section titled “Third-party Integrations”Integrate with popular monitoring tools:
// Grafana integrationconst grafanaConfig = { type: "grafana", url: "https://grafana.company.com", api_key: "your-grafana-api-key", dashboard_id: "unicraft-dashboard",};
// DataDog integrationconst datadogConfig = { type: "datadog", api_key: "your-datadog-api-key", app_key: "your-datadog-app-key", metrics: ["response_time", "cost", "success_rate"],};Best Practices
Section titled “Best Practices”Monitoring Strategy
Section titled “Monitoring Strategy”- Define Key Metrics: Identify the most important metrics for your use case
- Set Appropriate Thresholds: Configure alerts with realistic thresholds
- Regular Reviews: Schedule regular reviews of monitoring data
- Automate Responses: Set up automated responses for common issues
- Document Procedures: Document procedures for handling alerts
Performance Optimization
Section titled “Performance Optimization”- Baseline Establishment: Establish performance baselines
- Trend Analysis: Look for trends in performance data
- Capacity Planning: Use historical data for capacity planning
- Continuous Improvement: Regularly optimize based on monitoring insights
Cost Management
Section titled “Cost Management”- Budget Planning: Use historical data for budget planning
- Cost Allocation: Implement cost allocation by project or team
- Optimization Opportunities: Regularly identify optimization opportunities
- ROI Analysis: Analyze return on investment for AI usage
Troubleshooting
Section titled “Troubleshooting”Common Monitoring Issues
Section titled “Common Monitoring Issues”- Missing Data: Check data collection configuration
- Inaccurate Metrics: Verify metric calculation logic
- Alert Fatigue: Adjust alert thresholds and frequency
- Performance Impact: Monitor monitoring system performance
Debug Mode
Section titled “Debug Mode”Enable debug mode for troubleshooting:
// Enable debug modeconst debugMetrics = await unicraft.analytics.getMetrics({ time_range: "1h", debug: true,});
console.log("Debug info:", debugMetrics.debug);Next Steps
Section titled “Next Steps”After setting up monitoring:
- Review and optimize alert thresholds
- Set up automated reports
- Integrate with existing monitoring tools
- Train team on monitoring procedures
- Regularly review and update monitoring configuration