Cost Monitoring Implementation
Overviewβ
The Cost Monitoring system is now a single source of truth for all ecosystem costs, tracking expenses across all services in the Toto platform.
π Services Trackedβ
β Currently Implementedβ
-
AI/ML Services (Google Gemini) - Real-time
- Fetched from toto-ai-hub ModelSelectionService
- Tracks: Token usage, model costs, cost savings
- Status: β Real-time data
-
Firestore Database - Estimated
- Calculated from document counts
- Tracks: Reads, writes, storage costs
- Status: β οΈ Estimated (needs Cloud Billing API for accuracy)
-
SendGrid Email - Estimated
- Calculated from user count
- Tracks: Email volume, plan costs
- Status: β οΈ Estimated (needs SendGrid API integration)
-
Sentry Error Monitoring - Estimated
- Calculated from user count
- Tracks: Event volume, plan costs
- Status: β οΈ Estimated (needs Sentry API integration)
-
Twitter API - Fixed
- Fixed monthly cost ($100 for Basic plan)
- Status: β Fixed cost
β οΈ Placeholders (Need Implementation)β
-
Firebase App Hosting (Cloud Run)
- Status: β οΈ Placeholder (needs Cloud Billing API)
- Projects: toto-app, toto-bo, toto-ai-hub
-
Firebase Storage
- Status: β οΈ Placeholder (needs Storage usage metrics)
-
Stripe Payment Processing
- Status: β οΈ Not included (transaction fees, not direct costs)
-
Vertex AI Vector Search
- Status: β οΈ Not included (optional service)
π Architectureβ
API Endpointsβ
/api/cost-monitoring- AI-only costs (legacy)/api/cost-monitoring/comprehensive- All ecosystem costs (NEW)
Data Flowβ
toto-bo Dashboard
β
/api/cost-monitoring/comprehensive
β
βββββββββββββββββββββββββββββββββββββββ
β Cost Aggregation Service β
βββββββββββββββββββββββββββββββββββββββ€
β β’ AI Costs (toto-ai-hub) β
β β’ Firestore (estimated) β
β β’ SendGrid (estimated) β
β β’ Sentry (estimated) β
β β’ Twitter API (fixed) β
β β’ App Hosting (placeholder) β
β β’ Storage (placeholder) β
βββββββββββββββββββββββββββββββββββββββ
β
Unified Cost Dashboard
π Current Capabilitiesβ
What Works Nowβ
- β Real-time AI cost tracking
- β Estimated costs for Firestore, SendGrid, Sentry
- β Fixed costs for Twitter API
- β Category-based cost breakdown
- β Service-level cost tracking
- β Toggle between AI-only and comprehensive view
What Needs Enhancementβ
-
Cloud Billing API Integration
- Get real Firestore costs
- Get real Cloud Run costs
- Get real Storage costs
- Priority: High
-
Service API Integrations
- SendGrid API for actual email usage
- Sentry API for actual event counts
- Priority: Medium
-
Historical Data Storage
- Store daily/weekly/monthly snapshots
- Enable trend analysis
- Priority: Medium
-
Cost Alerts
- Budget alerts
- Anomaly detection
- Priority: Low
π§ Implementation Detailsβ
Cost Calculation Methodsβ
Real-time (AI)β
- Fetched directly from toto-ai-hub
- Uses ModelSelectionService analytics
- Most accurate
Estimated (Firestore, SendGrid, Sentry)β
- Based on document counts and user activity
- Uses pricing formulas from cost projections
- Reasonable estimates for planning
Fixed (Twitter API)β
- Known monthly subscription cost
- No calculation needed
Pricing Formulasβ
All pricing constants are defined in /api/cost-monitoring/comprehensive/route.ts:
const PRICING = {
cloudRun: { ... },
firestore: { ... },
storage: { ... },
sendgrid: { ... },
sentry: { ... },
twitter: { ... },
};
π Next Steps for Full Accuracyβ
Phase 1: Cloud Billing API (High Priority)β
- Set up Google Cloud Billing API access
- Create service account with billing viewer role
- Implement cost export queries
- Update comprehensive endpoint to use real data
Estimated effort: 2-3 days
Phase 2: Service API Integrations (Medium Priority)β
-
SendGrid API integration
- Track actual email sends
- Get plan usage
-
Sentry API integration
- Track actual event counts
- Get plan usage
Estimated effort: 1-2 days per service
Phase 3: Historical Tracking (Medium Priority)β
- Create Firestore collection for cost snapshots
- Schedule daily cost snapshots
- Build trend analysis UI
Estimated effort: 2-3 days
π Usageβ
Accessing the Dashboardβ
- Navigate to
/dashboard/cost-monitoringin toto-bo - Toggle between "All Costs" and "AI Only" views
- Select time range (24h, 7d, 30d, all)
- View comprehensive breakdown by service and category
Understanding the Dataβ
- Real-time: Data fetched live from services (AI costs)
- Estimated: Calculated from usage metrics (Firestore, SendGrid, Sentry)
- Fixed: Known subscription costs (Twitter API)
- Placeholder: Not yet implemented (App Hosting, Storage)
β οΈ Important Notesβ
- Estimated Costs: Some costs are estimated and should be used for planning, not exact billing
- Data Freshness: AI costs are real-time; other costs refresh every 5 minutes
- Accuracy: For production budgeting, integrate Cloud Billing API for accurate costs
- Missing Services: App Hosting and Storage costs require Cloud Billing API integration
π Related Documentationβ
- TECH_COST_PROJECTIONS.md - Detailed cost breakdowns
- AI_COSTS_DETAILED.md - AI cost explanations
- COST_SUMMARY.md - Quick reference
Last Updated: January 2025
Status: β
Comprehensive view implemented, Cloud Billing API integration pending