Skip to main content

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​

  1. AI/ML Services (Google Gemini) - Real-time

    • Fetched from toto-ai-hub ModelSelectionService
    • Tracks: Token usage, model costs, cost savings
    • Status: βœ… Real-time data
  2. Firestore Database - Estimated

    • Calculated from document counts
    • Tracks: Reads, writes, storage costs
    • Status: ⚠️ Estimated (needs Cloud Billing API for accuracy)
  3. SendGrid Email - Estimated

    • Calculated from user count
    • Tracks: Email volume, plan costs
    • Status: ⚠️ Estimated (needs SendGrid API integration)
  4. Sentry Error Monitoring - Estimated

    • Calculated from user count
    • Tracks: Event volume, plan costs
    • Status: ⚠️ Estimated (needs Sentry API integration)
  5. Twitter API - Fixed

    • Fixed monthly cost ($100 for Basic plan)
    • Status: βœ… Fixed cost

⚠️ Placeholders (Need Implementation)​

  1. Firebase App Hosting (Cloud Run)

    • Status: ⚠️ Placeholder (needs Cloud Billing API)
    • Projects: toto-app, toto-bo, toto-ai-hub
  2. Firebase Storage

    • Status: ⚠️ Placeholder (needs Storage usage metrics)
  3. Stripe Payment Processing

    • Status: ⚠️ Not included (transaction fees, not direct costs)
  4. Vertex AI Vector Search

    • Status: ⚠️ Not included (optional service)

πŸ— Architecture​

API Endpoints​

  1. /api/cost-monitoring - AI-only costs (legacy)
  2. /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​

  1. Cloud Billing API Integration

    • Get real Firestore costs
    • Get real Cloud Run costs
    • Get real Storage costs
    • Priority: High
  2. Service API Integrations

    • SendGrid API for actual email usage
    • Sentry API for actual event counts
    • Priority: Medium
  3. Historical Data Storage

    • Store daily/weekly/monthly snapshots
    • Enable trend analysis
    • Priority: Medium
  4. 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)​

  1. Set up Google Cloud Billing API access
  2. Create service account with billing viewer role
  3. Implement cost export queries
  4. Update comprehensive endpoint to use real data

Estimated effort: 2-3 days

Phase 2: Service API Integrations (Medium Priority)​

  1. SendGrid API integration

    • Track actual email sends
    • Get plan usage
  2. Sentry API integration

    • Track actual event counts
    • Get plan usage

Estimated effort: 1-2 days per service

Phase 3: Historical Tracking (Medium Priority)​

  1. Create Firestore collection for cost snapshots
  2. Schedule daily cost snapshots
  3. Build trend analysis UI

Estimated effort: 2-3 days

πŸ“‹ Usage​

Accessing the Dashboard​

  1. Navigate to /dashboard/cost-monitoring in toto-bo
  2. Toggle between "All Costs" and "AI Only" views
  3. Select time range (24h, 7d, 30d, all)
  4. 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​

  1. Estimated Costs: Some costs are estimated and should be used for planning, not exact billing
  2. Data Freshness: AI costs are real-time; other costs refresh every 5 minutes
  3. Accuracy: For production budgeting, integrate Cloud Billing API for accurate costs
  4. Missing Services: App Hosting and Storage costs require Cloud Billing API integration

Last Updated: January 2025
Status: βœ… Comprehensive view implemented, Cloud Billing API integration pending