Books/Deploying AI Apps/Deployment Cheat Sheet

    Deployment Cheat Sheet

    Deployment Cheat Sheet

    Your complete reference for deploying AI apps. Bookmark this page for quick access to commands, templates, and checklists.

    Platform Comparison Table

    FeatureVercelFirebaseNetlifyRailwayRender
    Best forNext.jsFirebase appsStatic/JamstackFull backendsFull-stack
    Free tierGenerousGenerousGenerous$5/mo creditLimited
    Server-sideServerlessCloud FunctionsServerlessFull serverFull server
    Deploy methodGit/CLICLIGit/CLIGitGit
    Custom domainsFreeFreeFreeFreeFree
    SSLAutoAutoAutoAutoAuto
    AI streamingNativeVia FunctionsVia FunctionsNativeNative
    Preview deploysAuto (GitHub)Manual channelsAuto (GitHub)AutoAuto
    Build timeFastMediumFastMediumSlow
    ScalingAutoAutoAutoManualAuto

    Quick Recommendations

    • Next.js + AI → Vercel
    • React + Firebase backend → Firebase Hosting
    • Static site → Netlify or Vercel
    • Python/Express API → Railway or Render
    • Full control needed → Railway

    Deployment Command Reference

    Vercel

    # Install
    npm install -g vercel
    
    # Login
    vercel login
    
    # Deploy (preview)
    vercel
    
    # Deploy (production)
    vercel --prod
    
    # Set environment variable
    vercel env add VARIABLE_NAME
    
    # List deployments
    vercel ls
    
    # Remove a deployment
    vercel rm deployment-url

    Firebase

    # Install
    npm install -g firebase-tools
    
    # Login
    firebase login
    
    # Initialize
    firebase init
    
    # Deploy everything
    firebase deploy
    
    # Deploy only hosting
    firebase deploy --only hosting
    
    # Deploy only functions
    firebase deploy --only functions
    
    # Deploy only Firestore rules
    firebase deploy --only firestore:rules
    
    # Preview channel
    firebase hosting:channel:deploy preview-name
    
    # Set function secret
    firebase functions:secrets:set SECRET_NAME
    
    # Start emulators
    firebase emulators:start
    
    # Switch project
    firebase use project-id

    Netlify

    # Install
    npm install -g netlify-cli
    
    # Login
    netlify login
    
    # Deploy (preview)
    netlify deploy
    
    # Deploy (production)
    netlify deploy --prod
    
    # Set environment variable
    netlify env:set VARIABLE_NAME value
    
    # Open site
    netlify open

    Railway

    # Install
    npm install -g @railway/cli
    
    # Login
    railway login
    
    # Link to project
    railway link
    
    # Deploy
    railway up
    
    # Set variable
    railway variables set KEY=value
    
    # Open dashboard
    railway open
    
    # View logs
    railway logs

    Environment Variable Checklist

    Required for Most AI Apps

    # AI API Keys (server-side only — never prefix with NEXT_PUBLIC_ or VITE_)
    OPENAI_API_KEY=sk-...
    ANTHROPIC_API_KEY=sk-ant-...
    GOOGLE_AI_API_KEY=...
    
    # Firebase (client-side — use public prefix)
    NEXT_PUBLIC_FIREBASE_API_KEY=...
    NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN=...
    NEXT_PUBLIC_FIREBASE_PROJECT_ID=...
    NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET=...
    NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID=...
    NEXT_PUBLIC_FIREBASE_APP_ID=...
    
    # App Configuration
    NEXT_PUBLIC_APP_URL=https://your-app.com
    NODE_ENV=production
    
    # Error Tracking
    NEXT_PUBLIC_SENTRY_DSN=...
    SENTRY_AUTH_TOKEN=...
    
    # Database (if not using Firebase)
    DATABASE_URL=...

    .env.example Template

    # AI APIs
    OPENAI_API_KEY=your-openai-key
    ANTHROPIC_API_KEY=your-anthropic-key
    
    # Firebase
    NEXT_PUBLIC_FIREBASE_API_KEY=your-firebase-api-key
    NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN=your-app.firebaseapp.com
    NEXT_PUBLIC_FIREBASE_PROJECT_ID=your-project-id
    NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET=your-app.firebasestorage.app
    NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID=your-sender-id
    NEXT_PUBLIC_FIREBASE_APP_ID=your-app-id
    
    # App
    NEXT_PUBLIC_APP_URL=http://localhost:3000
    
    # Monitoring (optional)
    NEXT_PUBLIC_SENTRY_DSN=your-sentry-dsn

    CI/CD Template

    Complete GitHub Actions Workflow

    # .github/workflows/ci-cd.yml
    name: CI/CD
    
    on:
      push:
        branches: [main]
      pull_request:
        branches: [main]
    
    jobs:
      test:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v4
          - uses: actions/setup-node@v4
            with:
              node-version: "20"
              cache: "npm"
          - run: npm ci
          - run: npm run lint
          - run: npx tsc --noEmit
          - run: npm test
          - run: npm run build
            env:
              NEXT_PUBLIC_APP_URL: https://example.com
    
      deploy:
        needs: test
        if: github.event_name == 'push' && github.ref == 'refs/heads/main'
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v4
          - uses: actions/setup-node@v4
            with:
              node-version: "20"
              cache: "npm"
          - run: npm ci
          - run: npm run build
            env:
              NEXT_PUBLIC_APP_URL: https://your-app.com
          # For Firebase:
          - uses: FirebaseExtended/action-hosting-deploy@v0
            with:
              repoToken: ${{ secrets.GITHUB_TOKEN }}
              firebaseServiceAccount: ${{ secrets.FIREBASE_SERVICE_ACCOUNT }}
              channelId: live
              projectId: your-project-id
          # For Vercel (alternative):
          # - uses: amondnet/vercel-action@v25
          #   with:
          #     vercel-token: ${{ secrets.VERCEL_TOKEN }}
          #     vercel-org-id: ${{ secrets.VERCEL_ORG_ID }}
          #     vercel-project-id: ${{ secrets.VERCEL_PROJECT_ID }}
          #     vercel-args: "--prod"

    Cost Optimization Checklist

    Before Launch

    ✅ Set spending limits on all AI API accounts
    ✅ Choose the cheapest model that meets quality needs
    ✅ Implement response caching (in-memory or database)
    ✅ Set max_tokens on all AI API calls
    ✅ Add rate limiting per user
    ✅ Test with production-like load to estimate costs
    

    Ongoing

    ✅ Review token usage weekly
    ✅ Monitor cost per user
    ✅ Check cache hit rate (target: 20%+)
    ✅ Watch for cost spikes in daily reports
    ✅ Evaluate newer, cheaper models as they release
    ✅ Trim prompt templates to reduce input tokens
    

    Cost-Saving Techniques

    TechniqueSavingsEffort
    Use smaller models for simple tasks50-95%Low
    Cache identical requests20-40%Low
    Reduce prompt length10-30%Medium
    Set max_tokensPrevents wasteLow
    Rate limit usersControls budgetLow
    Batch requests10-20%Medium
    Semantic caching30-50%High

    Pre-Deployment Checklist

    Code Quality

    ✅ All tests passing
    ✅ No TypeScript errors
    ✅ No linting warnings
    ✅ Build succeeds without errors
    ✅ No console.log statements in production code
    ✅ Error boundaries in place for React components
    

    Security

    ✅ All secrets in environment variables (not in code)
    ✅ .env in .gitignore
    ✅ AI API keys are server-side only
    ✅ Firestore security rules are NOT in test mode
    ✅ Storage security rules configured
    ✅ CORS configured correctly
    ✅ Rate limiting active
    ✅ Input validation on all user inputs
    

    Performance

    ✅ Images optimized (WebP, proper sizes)
    ✅ Caching headers set for static assets
    ✅ AI response caching implemented
    ✅ Loading states for AI operations
    ✅ Error handling for slow/failed AI calls
    ✅ Streaming enabled for long AI responses
    

    Monitoring

    ✅ Error tracking set up (Sentry or similar)
    ✅ AI API call logging active
    ✅ Cost tracking in place
    ✅ Health check endpoint available
    ✅ Alerts configured for error spikes and cost overruns
    

    AI Prompts for Deployment Help

    Initial Setup

    • "Help me deploy my [framework] app to [platform]. Walk me through each step."
    • "Configure my project for production deployment. Here's my tech stack: [list tech]."
    • "Set up environment variables for my AI app on [platform]. Here are the variables I need: [list them]."

    CI/CD

    • "Create a GitHub Actions workflow that tests and deploys my [framework] app to [platform]."
    • "My CI pipeline is failing with this error: [paste error]. How do I fix it?"
    • "Add preview deployments to my workflow so every PR gets a preview URL."

    Monitoring

    • "Set up error tracking with Sentry for my [framework] app."
    • "Create a logging wrapper for my AI API calls that tracks latency, tokens, and costs."
    • "Build a simple monitoring dashboard for my AI app's usage and costs."

    Cost Optimization

    • "My AI app costs $[X]/month. Here's how it makes API calls: [describe]. How can I reduce costs?"
    • "Implement caching for my AI API calls to reduce costs. Use [Firestore/Redis/in-memory]."
    • "Help me choose the right AI model for each feature in my app: [list features]."

    Troubleshooting

    • "My app works locally but not in production. Here's what happens: [describe]. Build logs: [paste]."
    • "Users are reporting slow AI responses. How do I diagnose and fix this?"
    • "My deployment keeps failing. Here's the error: [paste]. Here's my config: [paste]."
    • "I accidentally committed my API key to Git. What should I do?"

    The Complete Deployment Workflow

    1. DEVELOP locally with .env file
    2. TEST with npm run lint, tsc, npm test
    3. BUILD with npm run build
    4. CONFIGURE environment variables on your hosting platform
    5. DEPLOY to a preview environment first
    6. VERIFY the preview — test AI features, check error tracking
    7. DEPLOY to production
    8. MONITOR logs, errors, costs, and performance
    9. ITERATE — review monitoring data, optimize costs, improve reliability
    

    Quick Reference: Common Errors and Fixes

    ErrorLikely CauseFix
    "OPENAI_API_KEY is not defined"Env var not set on platformAdd it in platform settings
    "Module not found" in productionMissing dependencyCheck dependencies vs devDependencies
    404 on page refreshSPA rewrites not configuredAdd rewrite rule to hosting config
    CORS errorAPI domain not allowedAdd deployed domain to CORS config
    "Quota exceeded"Hit AI API rate limitImplement caching and rate limiting
    Build timeoutBuild takes too longIncrease timeout or optimize build
    "Permission denied" (Firestore)Security rules blockingUpdate Firestore rules
    Functions cold start slowFirst request after idleUse min instances setting

    Congratulations! You now know how to deploy, monitor, and scale AI-powered applications. The key takeaways:

    1. Start simple — Deploy to Vercel or Firebase with one command
    2. Automate early — Set up CI/CD before your first user
    3. Monitor everything — Especially AI API costs and latency
    4. Cache aggressively — It's the easiest way to cut costs
    5. Scale gradually — Start with cheaper models and upgrade as needed

    Happy deploying!


    🌐 www.genai-mentor.ai