Back to Modules
๐Ÿš€

Deployment

Learn how to host and deploy your AI agents on various platforms

Deployment

๐Ÿ‘ถ Explained like I'm 5

You built an awesome LEGO castle! But it's just sitting in your room. To show your friends, you need to:

  1. Take a picture
  2. Put it somewhere they can see it (like Instagram or a website)
  3. Make sure it stays there

Deploying an AI agent is the same! You built something cool, now you need to put it online so others can use it.

Deployment = Going Live

Deployment makes your agent accessible to the world, running 24/7 on the internet.

โ“ Why we need this

Your agent is amazing, but:

  • It only runs on YOUR computer
  • Nobody else can use it
  • It stops when you close your laptop
  • You can't show it off!

Deployment fixes all of this by putting your agent on the internet where:

  • Anyone can access it
  • It runs 24/7
  • You can share it with the world
  • It becomes part of your portfolio!

๐Ÿง  How it works

Deployment has a few steps:

  1. Prepare: Make sure your code is ready
  2. Host: Choose where to put it (Netlify, Vercel, etc.)
  3. Configure: Set up environment variables and settings
  4. Deploy: Push your code and it goes live!
  5. Monitor: Make sure it keeps working

Think of it like moving into a new house - you pack everything up, move it, unpack, and make sure everything works!

๐Ÿ“š Deep Dive: Deployment Platforms

Best for: Next.js, React, static sites

Steps:

  1. Push code to GitHub
  2. Go to netlify.com โ†’ "Add new site" โ†’ "Import from GitHub"
  3. Select your repository
  4. Configure build:
    • Build command: npm run build
    • Publish directory: .next
  5. Add environment variables in Site settings
  6. Deploy!

Pros:

  • Free tier available
  • Automatic deployments on git push
  • Easy environment variable management
  • Great for static sites

Cons:

  • Limited server-side functionality
  • Cold starts for serverless functions

Option 2: Vercel (Best for Next.js)

Best for: Next.js applications

Steps:

  1. Push code to GitHub
  2. Go to vercel.com โ†’ "Add New Project"
  3. Import your GitHub repository
  4. Vercel auto-detects Next.js
  5. Add environment variables
  6. Deploy!

Pros:

  • Optimized for Next.js
  • Zero-config deployment
  • Excellent performance
  • Free tier available

Cons:

  • Best for Next.js (less optimal for other frameworks)

Option 3: Railway (For Python Agents)

Best for: Python-based agents, CrewAI projects

Steps:

  1. Push code to GitHub
  2. Go to railway.app โ†’ "New Project"
  3. Connect GitHub repository
  4. Railway detects Python
  5. Add environment variables
  6. Deploy!

Pros:

  • Great for Python applications
  • Easy database integration
  • Good for backend services
  • Free tier available

Cons:

  • Less optimized for frontend-only apps

Option 4: Render

Best for: Full-stack applications

Steps:

  1. Push code to GitHub
  2. Go to render.com โ†’ "New Web Service"
  3. Connect repository
  4. Configure build and start commands
  5. Add environment variables
  6. Deploy!

Pros:

  • Supports many frameworks
  • Free tier available
  • Good documentation

Cons:

  • Can be slower than Vercel/Netlify

๐Ÿงช Example

Here's the deployment process:

Step 1: Prepare
โ”œโ”€โ”€ Test your code locally
โ”œโ”€โ”€ Create requirements.txt
โ”œโ”€โ”€ Add .env.example
โ””โ”€โ”€ Write a README

Step 2: Push to GitHub
โ”œโ”€โ”€ git init
โ”œโ”€โ”€ git add .
โ”œโ”€โ”€ git commit
โ””โ”€โ”€ git push

Step 3: Deploy
โ”œโ”€โ”€ Go to Netlify/Vercel
โ”œโ”€โ”€ Connect GitHub repo
โ”œโ”€โ”€ Add environment variables
โ””โ”€โ”€ Deploy!

Step 4: Share
โ””โ”€โ”€ Get your live URL and share it!

Complete Deployment Example

1. Prepare Your Project:

# Create requirements.txt
pip freeze > requirements.txt

# Create .env.example
echo "OPENAI_API_KEY=your_key_here" > .env.example

# Create README.md
cat > README.md << EOF
# My AI Agent

Description of your agent here.

## Setup
1. Install dependencies: pip install -r requirements.txt
2. Copy .env.example to .env
3. Add your API keys
4. Run: python main.py
EOF

2. Push to GitHub:

git init
git add .
git commit -m "Initial commit"
git remote add origin https://github.com/yourusername/your-repo.git
git push -u origin main

3. Deploy to Vercel:

# Install Vercel CLI
npm i -g vercel

# Deploy
vercel

# Follow prompts:
# - Link to existing project? No
# - Project name? your-agent
# - Directory? ./
# - Override settings? No

4. Configure Environment Variables:

In Vercel dashboard:

  • Go to Project Settings โ†’ Environment Variables
  • Add: OPENAI_API_KEY = your_actual_key
  • Redeploy

๐ŸŽฏ Real-World Case Studies

Deploying a Research Agent to Production

๐Ÿ“‹ Scenario

A research agent works perfectly locally but fails when deployed. API calls timeout, environment variables aren't loading.

๐Ÿ’ก Solution

Fixed by: (1) Adding proper error handling for API timeouts, (2) Setting environment variables in platform dashboard (not just .env), (3) Adding retry logic for failed requests, (4) Implementing health checks, (5) Adding logging for debugging.

โœ… Outcome

Agent deployed successfully. 99.9% uptime. Proper error handling prevents crashes. Logging helps debug issues quickly.

๐ŸŽ“ Key Lessons

  • Environment variables must be set in platform, not just locally
  • Add timeout and retry logic for API calls
  • Health checks help monitor agent status
  • Logging is essential for debugging production issues

๐Ÿ›  Hands-on Task

Practice deployment! Even if you don't deploy yet, prepare your project:

  1. Create a requirements.txt file
  2. Write a README.md explaining your project
  3. Create a .env.example file
  4. Make sure your code is organized
  5. Test everything locally

Extended Exercise: Full Deployment

  1. Prepare: Clean up code, add documentation
  2. Test: Ensure everything works locally
  3. Push: Commit and push to GitHub
  4. Deploy: Choose platform and deploy
  5. Configure: Set environment variables
  6. Verify: Test deployed version
  7. Monitor: Check logs and performance

Deployment Tip

Always test locally first. Deploy to staging/test environment before production.

โœ… Checklist

Before deploying:

๐Ÿค” Mini Quiz

Why should you never commit your .env file to GitHub?

๐Ÿšจ Common Pitfalls & Solutions

Pitfall 1: Environment Variables Not Loading

Problem: API keys don't work after deployment.

Solution: Set environment variables in your platform's dashboard, not just .env file.

# โŒ Bad: Only in .env (local)
OPENAI_API_KEY=sk-...

# โœ… Good: Set in platform dashboard
# Vercel: Project Settings โ†’ Environment Variables
# Netlify: Site Settings โ†’ Environment Variables

Pitfall 2: Missing Dependencies

Problem: App crashes because dependencies aren't installed.

Solution: Ensure requirements.txt includes all dependencies.

# Generate requirements.txt
pip freeze > requirements.txt

# Or use pipreqs for cleaner output
pip install pipreqs
pipreqs . --force

Pitfall 3: API Timeouts

Problem: Long-running agent operations timeout.

Solution: Add timeout handling and consider async operations.

import asyncio
from crewai import Agent, Task, Crew

# Use async for long operations
async def run_agent():
    crew = Crew(agents=[...], tasks=[...])
    result = await asyncio.wait_for(
        crew.kickoff(),
        timeout=300  # 5 minutes
    )
    return result

Pitfall 4: Cold Starts

Problem: First request is slow after inactivity.

Solution: Use health checks or keep-alive pings.

Cold Starts

Serverless platforms have cold starts. Consider using scheduled pings or upgrade to always-on plan.

๐Ÿ’ก Best Practices

  1. Test Locally First: Never deploy untested code
  2. Use .env.example: Show what variables are needed
  3. Secure Secrets: Never commit API keys
  4. Add Error Handling: Graceful failures prevent crashes
  5. Monitor Logs: Track what's happening
  6. Set Up Health Checks: Know if your agent is working
  7. Document Everything: Make deployment reproducible
  8. Use CI/CD: Automate deployments

๐Ÿ”’ Security Considerations

1. API Key Management

# โœ… Good: Load from environment
import os
api_key = os.getenv('OPENAI_API_KEY')

# โŒ Bad: Hardcoded
api_key = "sk-1234567890"  # Never do this!

2. Input Validation

# โœ… Good: Validate inputs
def process_request(user_input: str):
    if len(user_input) > 1000:
        raise ValueError("Input too long")
    # Process safely

3. Rate Limiting

# โœ… Good: Limit requests
from functools import lru_cache
import time

@lru_cache(maxsize=100)
def cached_api_call(query):
    # Cache results to reduce API calls
    pass

๐Ÿ“Š Monitoring & Maintenance

Health Checks

# Add health check endpoint
@app.route('/health')
def health():
    return {'status': 'healthy', 'timestamp': time.time()}

Logging

import logging

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Log important events
logger.info("Agent started")
logger.error("API call failed", exc_info=True)

๐Ÿ”— Additional Resources

Vercel Deployment Guide

Official guide for deploying Next.js and other projects on Vercel

VercelDeployment

Netlify Deployment Guide

Complete documentation for deploying web apps on Netlify

NetlifyDeployment

Railway App Deployment

Instructions for deploying Python or Node apps (useful for agentic AI projects)

RailwayPython

๐Ÿš€ Challenge for GitHub

Deploy your first agent! Follow these steps:

  1. Prepare your project
  2. Push to GitHub
  3. Deploy on Netlify or Vercel
  4. Share your live URL
  5. Update your portfolio

Advanced Challenge:

  1. Set up CI/CD pipeline
  2. Add health checks
  3. Implement monitoring
  4. Set up error alerts
  5. Document the entire process

Document your deployment process and share it on GitHub!

๐ŸŽ“ Next Steps

Continue your learning journey:

  1. Next Module: Safety & Ethics - Deploy responsibly
  2. Or Explore: Showcase - Share your deployed projects
  3. Advanced: Monitoring & Logging - Keep agents running smoothly

Congratulations!

You now know how to deploy agents to production. Your agents can now help users around the world!