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:
- Take a picture
- Put it somewhere they can see it (like Instagram or a website)
- 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:
- Prepare: Make sure your code is ready
- Host: Choose where to put it (Netlify, Vercel, etc.)
- Configure: Set up environment variables and settings
- Deploy: Push your code and it goes live!
- 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
Option 1: Netlify (Recommended for Static Sites)
Best for: Next.js, React, static sites
Steps:
- Push code to GitHub
- Go to netlify.com โ "Add new site" โ "Import from GitHub"
- Select your repository
- Configure build:
- Build command:
npm run build - Publish directory:
.next
- Build command:
- Add environment variables in Site settings
- 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:
- Push code to GitHub
- Go to vercel.com โ "Add New Project"
- Import your GitHub repository
- Vercel auto-detects Next.js
- Add environment variables
- 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:
- Push code to GitHub
- Go to railway.app โ "New Project"
- Connect GitHub repository
- Railway detects Python
- Add environment variables
- 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:
- Push code to GitHub
- Go to render.com โ "New Web Service"
- Connect repository
- Configure build and start commands
- Add environment variables
- 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:
- Create a requirements.txt file
- Write a README.md explaining your project
- Create a .env.example file
- Make sure your code is organized
- Test everything locally
Extended Exercise: Full Deployment
- Prepare: Clean up code, add documentation
- Test: Ensure everything works locally
- Push: Commit and push to GitHub
- Deploy: Choose platform and deploy
- Configure: Set environment variables
- Verify: Test deployed version
- 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
- Test Locally First: Never deploy untested code
- Use .env.example: Show what variables are needed
- Secure Secrets: Never commit API keys
- Add Error Handling: Graceful failures prevent crashes
- Monitor Logs: Track what's happening
- Set Up Health Checks: Know if your agent is working
- Document Everything: Make deployment reproducible
- 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
Netlify Deployment Guide
Complete documentation for deploying web apps on Netlify
Railway App Deployment
Instructions for deploying Python or Node apps (useful for agentic AI projects)
๐ Challenge for GitHub
Deploy your first agent! Follow these steps:
- Prepare your project
- Push to GitHub
- Deploy on Netlify or Vercel
- Share your live URL
- Update your portfolio
Advanced Challenge:
- Set up CI/CD pipeline
- Add health checks
- Implement monitoring
- Set up error alerts
- Document the entire process
Document your deployment process and share it on GitHub!
๐ Next Steps
Continue your learning journey:
- Next Module: Safety & Ethics - Deploy responsibly
- Or Explore: Showcase - Share your deployed projects
- 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!