# Model Metering, Infrastructure Pricing & Capacity Forecasting

This blueprint is designed for **estimating, tracking, and forecasting cloud infrastructure and AI API consumption costs** across your SaaS application ecosystems. You can extend or copy this template for any full-stack React + Firebase or AI-integrated software you launch.

---

## 1. Real-World Unit Costing Ledger (Baseline Metrics)

Below are the actual production rates (as of Mid-2026) for the Google Cloud & Gemini developer API layers supporting this specific stack type.

### A. AI Intelligence Model Layer (Gemini API via Google GenAI SDK)
This is the single most variable operative expense area. Standardizing on the official `@google/genai` TypeScript SDK allows toggling between Flash and Pro engines based on target complexity:

| Engine | Ideal For | Price / 1M Input Tokens | Price / 1M Output Tokens | Free Tier Allowance |
| :--- | :--- | :--- | :--- | :--- |
| **Gemini 2.5 Flash** | Studio Chats, CRM logs, Basic Task boards | **$0.075** | **$0.30** | 15 RPM / 1,500 RPD |
| **Gemini 2.5 Pro** | Deep research, Lye SAP math, Legal checks | **$1.25** | **$5.00** | 2 RPM / 50 RPD |

*Ratio Assumption: A typical user message + 10-turn history has a context of `~3,500` input tokens and returns `~400` output tokens.*

---

### B. Database & Storage Layer (Firebase Firestore)
Firestore prices are calculated strictly on read, write, and delete operations. This modular design remains extremely cost-effective at small-to-medium scale:

| Database Item | Operation | Rate per 100,000 Operations | Monthly Free Tier Allowance |
| :--- | :--- | :--- | :--- |
| **Document Writes** | Adding inventory, saving formulas | **$0.18** | 20,000 writes / day |
| **Document Reads** | Real-time snapshots, syncing tabs | **$0.06** | 50,000 reads / day |
| **Document Deletes**| Clearing task boards, purging logs | **$0.02** | 20,000 deletes / day |
| **Database Storage**| Storing formulas & workspace state | **$0.18 per GB** | 1 GB total storage |

---

### C. Server Ingress & Delivery (Google Cloud Run + DNS)
Standard container hosting for full-stack build files behind an Nginx gateway:

| Infrastructure Item | Resource Metric | Production Rate | Free Tier Allowance |
| :--- | :--- | :--- | :--- |
| **Container Compute** | vCPU Hour | **$0.0240** | 180,000 vCPU-seconds |
| **Container Memory**  | GB Hour | **$0.0025** | 360,000 GiB-seconds |
| **Workspace Egress**  | Network Outbound | **$0.08 per GB** | 12 GB outbound / month |
| **Google Cloud Model Armor** | Inspection Requests | **$0.0005 per 1k requests** | 100,000 evaluations / month |
| **ShieldGemma Classifier** | Token Classification | **$0.025 per 1M tokens** | Included in Cloud Vertex Security Tier |

---

## 2. Dynamic SaaS Capacity Estimator Worksheet
Copy this worksheet to estimate the direct monthly production costs (COGS) of running individual server nodes under varying active artisan tier accounts.

### Define Current Workspace Variables
*Plug in your operational metrics below to calculate overall totals:*

```
[U] = Total active user base                             (e.g., 250 users)
[M] = Average chat messages sent per user, per day       (e.g., 12 messages)
[F] = Average daily recipe formulation edits             (e.g., 5 formulations)
```

### Step 1: Message Tokenomics & AI Expenses
*   **Daily Input Volume per User:** `[M] * 3,500 input tokens = 42,000 tokens`
*   **Daily Output Volume per User:** `[M] * 400 output tokens = 4,800 tokens`
*   **Daily Input Total (All Users):** `[I_day] = [U] * 42,000 tokens`
*   **Daily Output Total (All Users):** `[O_day] = [U] * 4,800 tokens`

$$\text{Monthly Flash API Cost} = \left( \frac{[I_{\text{day}}] \times 30}{1,000,000} \times \$0.075 \right) + \left( \frac{[O_{\text{day}}] \times 30}{1,000,000} \times \$0.30 \right)$$

---

### Step 2: Database Storage Operation Expenses
ArtisanOS syncs changes to Firestore via onSnapshot triggers. A single recipe entry, chat message, or inventory log counts as a collection operation.

*   **Daily Database Reads per User:** `([M] * 10) + ([F] * 5) = ~145 reads`
*   **Daily Database Writes per User:** `[M] + [F] = ~17 writes`
```
[R_tot] = [U] * 145 reads/day * 30 days = Monthly Total Reads
[W_tot] = [U] * 17 writes/day * 30 days = Monthly Total Writes
```

$$\text{Monthly DB Expense} = \left( \frac{[R_{\text{tot}}]}{100,000} \times \$0.06 \right) + \left( \frac{[W_{\text{tot}}]}{100,000} \times \$0.18 \right)$$

---

## 3. Cost-to-Pricing Forecast Simulation Tool

Below is a preset cost projection for standard tiers based on the actual subscription bundles implemented in the application.

### Projection Model A: Emerging Studio Node (50 Active Sandbox Tiers)
*   **Model selection:** Gemini 2.5 Flash only. Less frequent formulations modifications.
*   **Expected Model Tokens / Month:** 62,000,500 input / 7,000,000 output.
*   **Cost Breakdown:**
    *   AI Token API Expenses: **$6.75** (After free-tier deductions)
    *   Firestore Operation Reads & Writes: **$2.50** (Falls within free bounds)
    *   Google Cloud Run Resource Allocation: **$3.10**
    *   **Total Cloud COGS:** **$12.35 / Month**
    *   **SaaS Average Monthly User Subscription Income:** $29 * 5 = **$145.00**
    *   **Gross Margin Profitability:** **$132.65 ($91.4% Gross Margin)**

### Projection Model B: Enterprise Master Node (500 Active Workshop Tiers)
*   **Model selection:** Gemini 2.5 Flash for chat, 2.5 Pro for lye calculations validation.
*   **Expected Model Tokens / Month:** 650M input / 75M output.
*   **Cost Breakdown:**
    *   AI Token API Expenses (Flash + Pro mixture): **$215.00**
    *   Firestore Operation Reads & Writes: **$61.20**
    *   Google Cloud Run Resource Allocation (Scaling instances): **$42.50**
    *   **Total Cloud COGS:** **$318.70 / Month**
    *   **SaaS Average Monthly User Subscription Income:** $79 * 25 = **$1,975.00**
    *   **Gross Margin Profitability:** **$1,656.30 ($83.8% Gross Margin)**

---

## 4. Reusable Metadata Template (Use Across Any Application)
Copy and paste this section to create a developer metadata tracker in `.md` format whenever initiating new full-stack builds.

```markdown
# Application Cost Control Matrix: [<Application-Name>]

### Project Profiles
- **Target Audience:** [<Target-Hobbyists-or-Enterprise-Operators>]
- **Database Engine Selected:** [Firestore / Cloud SQL PostgreSQL]
- **Storage Strategy:** [Sovereign Client Cache / Firebase Real-Time Streams]

### Infrastructure Guardrails & Budgets
1. **Critical Outbound Cap:** Max $50.00 / month on testing cycles. Set hard cloud quotas inside Google Billing.
2. **Offline Fallback Safeguards (Free Off-Grid Mode):** 
   When months are tight financially, toggling the **"Off-Grid Safeguard"** inside the Subscription panel completely decouples the application from cloud Firestore clusters. Data is transferred directly into standard browser IndexedDB / localStorage. 
   - **Cloud COGS Monthly Price:** $0.00 (API queries are suspended).
   - **Local Storage Footprint:** ~2.5MB per 10,000 batches.
3. **Caching Layer:** Implement client-side `localStorage` caching of Gemini agent system instructions to avoid unnecessary server reads on router tab shifts.
```
