Best Practices Advanced
Running AI workloads on Firebase at scale requires careful attention to costs, security, model selection, and monitoring. This lesson covers the essential patterns for production Firebase AI applications.
Cost Management
| Strategy | Implementation | Impact |
|---|---|---|
| Use Flash models | Choose gemini-2.0-flash over gemini-2.0-pro for simple tasks |
10x cost reduction |
| Cache AI responses | Store results in Firestore for identical or similar queries | 50-90% savings on repeat queries |
| Set billing alerts | Configure budget alerts in Google Cloud Console | Prevents unexpected bills |
| Limit max tokens | Set maxOutputTokens on all model calls |
20-50% savings |
| Batch embeddings | Generate embeddings in bulk, not one at a time | Reduces function invocations |
Security Rules for AI
rules_version = '2'; service cloud.firestore { match /databases/{database}/documents { // AI-generated content: users can only read their own match /ai-responses/{responseId} { allow read: if request.auth != null && resource.data.userId == request.auth.uid; allow create: if false; // Only Cloud Functions can write } // Rate limit: max 10 AI requests per minute per user match /ai-requests/{requestId} { allow create: if request.auth != null && request.resource.data.userId == request.auth.uid; } } }
Model Selection Guide
| Task | Recommended Model | Why |
|---|---|---|
| Simple Q&A, classification | Gemini 2.0 Flash | Fastest, cheapest, good enough |
| Complex reasoning, analysis | Gemini 2.0 Pro | Better accuracy for hard tasks |
| Image/video understanding | Gemini 2.0 Flash (multimodal) | Built-in vision, fast processing |
| Text embeddings | text-embedding-004 | High quality, reasonable cost |
| On-device OCR/detection | ML Kit | Free, instant, offline |
Enable App Check
Protect your AI endpoints from abuse:
import { initializeAppCheck, ReCaptchaV3Provider } from 'firebase/app-check'; // Enable App Check to verify legitimate requests const appCheck = initializeAppCheck(app, { provider: new ReCaptchaV3Provider('your-recaptcha-site-key'), isTokenAutoRefreshEnabled: true, });
Monitoring Checklist
- Cloud Function execution time and error rate
- Vertex AI API usage and costs per day
- Firestore reads/writes from AI functions
- App Check rejection rate (indicates abuse attempts)
- Model response latency (p50, p95)
- User-reported AI quality issues
Production Architecture
Client App Firebase Google Cloud
+------------------+ +------------------+ +------------------+
| Firebase Auth | --> | App Check | -----> | Vertex AI |
| Vertex AI SDK | --> | Firestore | | Gemini Models |
| ML Kit (device) | | Cloud Functions | -----> | Embeddings API |
+------------------+ | Cloud Storage | | Vector Search |
+------------------+ +------------------+
Course Complete!
Congratulations! You have completed the Firebase + AI course. You can now build intelligent applications using Firebase with Vertex AI, Gemini, ML Kit, Cloud Functions, and Firestore vector search.
← Back to Course OverviewReady to Go Deeper?
Live instructor-led courses from our partners. Affiliate disclosure.
AI & ML Courses - 30% Off
Live instructor-led AI, machine learning, data science, and cloud courses for working professionals. Use code Limited30 at checkout.
EdurekaDataCamp - AI & Data Science
Hands-on Python, machine learning, and AI courses with interactive exercises and real projects.
DataCampedX - Top AI Courses
University-level AI courses from MIT, Harvard, Stanford. Earn certificates that employers recognize.
edX