# 💰 Cost Analysis & Optimization Strategy

> **Status**: ✅ Active Strategy
> **Last Updated**: 2025-11-28

## 🎯 Overview

Managing API costs (Google Maps, OpenAI, Firebase) is critical for the viability of MyRoadTrip.
Without optimization, costs could reach **$17,000/month**.
With optimization, we target **$200-500/month** for the initial user base.

---

## 📊 Cost Breakdown (Unoptimized vs Optimized)

| Service | Unoptimized (100 users) | Optimized (100 users) | Reduction |
|---------|-------------------------|-----------------------|-----------|
| **Google Places** | $17,100/mo | $204/mo | **-98%** |
| **OpenAI** | $100/mo | $10/mo | **-90%** |
| **Firebase** | $50/mo | $15/mo | **-70%** |
| **Total** | **~$17,250/mo** | **~$230/mo** | **-98%** |

---

## 🛠️ Optimization Strategy

### 1. Google Maps & Places (The Biggest Cost)

*   **Internal Cache**: We cache Google Places details in Firestore. Before calling Google API, we check our cache.
    *   *Impact*: 60% reduction in calls.
*   **Quality Filter**: We only fetch details for POIs with rating > 4.0 and > 10 reviews.
    *   *Impact*: 80% of low-quality POIs ignored.
*   **Voluntary Discovery**: Users must explicitly click "Discover" to fetch details. No auto-fetching on map move.
    *   *Impact*: 50% reduction.
*   **Rate Limiting**: Free users limited to 10 discoveries/day.

### 2. OpenAI (AI Summaries)

*   **Batch Processing**: We summarize multiple POIs in a single prompt where possible.
*   **Caching**: AI summaries are stored in Firestore forever.
*   **Short Prompts**: Optimized prompts to reduce token usage.

### 3. Firebase (Database & Storage)

*   **Denormalization**: Store essential data in the `Plan` document to avoid joining queries.
*   **Image Optimization**: Resize images before upload to reduce storage and bandwidth.
*   **CDN**: Use caching headers for static assets.

---

## 🚨 Circuit Breakers & Monitoring

To prevent accidental bankruptcy, we implement strict limits:

### Budget Limits
*   **Daily Limit**: $50/day
*   **Monthly Limit**: $1000/month

### Automated Actions
1.  **Alert**: Email admin at 80% of budget.
2.  **Disable**: Stop "Discovery" feature at 100% of budget.
3.  **Emergency**: Switch to "Cache Only" mode if monthly limit exceeded.

### Implementation
See `apps/api/src/modules/monitoring/budgetMonitor.ts`.

---

## 📈 Monetization Alignment

*   **Free Tier**: 10 discoveries/day. Cost covered by marketing budget.
*   **Premium ($9.99)**: 50 discoveries/day.
*   **Pro ($29.99)**: Unlimited (fair use).

---

## 📝 Recommendations

1.  **Never enable auto-discovery** on map movement.
2.  **Always check cache** before API calls.
3.  **Monitor daily** usage in Google Cloud Console.

---

## 🧮 Appendix: Detailed Calculation Logic

*Reference from November 2025 Audit*

### Cost per POI Formula
The cost per POI is derived from Google Places (Basic Data) and OpenAI (GPT-4o-mini).

| Component | Cost | Notes |
|-----------|------|-------|
| **Google Places (Basic)** | **$0.017** | Includes name, address, geometry, rating. |
| **Google Places (Contact)** | +$0.015 | Optional. Phone, website, hours. |
| **Google Places (Photos)** | +$0.007 | Per photo downloaded (we store URLs only = $0). |
| **OpenAI Summary** | **$0.0001** | ~300 tokens with GPT-4o-mini. |
| **Total (Basic)** | **$0.0171** | **Used for projections.** |

### Monthly Projection Formula
```python
monthly_cost = (users * daily_discoveries * 30) * (1 - cache_hit_rate) * cost_per_poi
```

**Example (100 users, 10 discoveries/day):**
- **No Cache**: 30,000 calls * $0.017 = **$510/mo**
- **60% Cache**: 12,000 calls * $0.017 = **$204/mo**

