Lesson 06 � Advanced Skill

DEPLOYMENT:
AGENTS KO PRODUCTION MEIN LAO.

Agent banana easy hai, deploy karna mushkil � production mein scaling, monitoring, security sab handle karna padta hai. Ek perfectly kaam karne wala agent bhi tab tak kaam nahi jab tak usko sahi tarah se deploy na kiya jaye. Yeh lesson aapko step-by-step guide karega ki agent ko live kaise karein.

? 22 min✓ Advanced✓ Prerequisite: Multi-Agent Systems

WHY: Agent deploy karna kyun zaroori hai?

Aapne agent banaya, test kiya, sab kuch sahi hai � lekin ab tak woh sirf aapke laptop pe hai. Duniya tak pahunchne ke liye use production mein deploy karna padega. Jaise dukaan banana aur chalana alag cheez hai, waise agent banana aur deploy karna alag hai. Deployed agent proper monitoring, security, aur scalability ke saath chalta hai.

API

REST ya GraphQL endpoint jo agent ko accessible banaye. External systems agent se communicate karenge through API calls � yeh agent ka entry point hai.

CONTAINERIZATION

Docker container mein agent package karna � consistent environment, easy deployment. Ek jagah kaam kare toh har jagah kaam karega. "It works on my machine" problem solved.

SCALING

Load balancing aur auto-scaling se multiple requests handle karna. Jab traffic badhe toh agents automatically badhen, jab kam ho toh ghatten � cost efficient aur performant.

MONITORING

Logs, metrics, aur alerts se agent ki health track karna. Pata chale ki agent sahi kaam kar raha hai ya nahi, errors kya hain, performance kaisi hai. Production mein debugging ka tarika.

Step 1: Agent ko API banao

Pehla step hai agent ko ek API endpoint dena. FastAPI use karenge kyunki woh fast hai, async support hai, aur auto-generated docs milte hain. Har request handle karega aur response dega.

python
# FastAPI agent deployment
from fastapi import FastAPI
from pydantic import BaseModel
import uvicorn

app = FastAPI(title="DSWallah Agent API")

# Request/Response models
class Query(BaseModel):
 message: str

class AgentResponse(BaseModel):
 response: str
 agent: str
 status: str = "success"

# Agent logic - yahan apna agent code dalo
def process_query(message: str) -> str:
 """Agent ka core logic"""
 if "hello" in message.lower():
 return "Namaste! DSWallah Agent aapki seva mein hai."
 elif "help" in message.lower():
 return "Main aapki madad kar sakta hoon � search, calculation, ya general queries."
 else:
 return f"Agent ne process kiya: {message}"

@app.post("/agent/chat", response_model=AgentResponse)
async def chat(query: Query):
 """Chat endpoint - agent se baat karo"""
 response = process_query(query.message)
 return AgentResponse(response=response, agent="DSWallah Bot")

@app.get("/agent/health")
async def health():
 """Health check - agent alive hai ya nahi"""
 return {"status": "healthy", "agents": 1, "uptime": "running"}

# Run with: uvicorn main:app --host 0.0.0.0 --port 8000
print("Agent API ready!")
print("Docs: http://localhost:8000/docs")
Key insight: FastAPI ke saath auto-generated Swagger docs milte hain at /docs � testing bahut easy ho jaati hai. Pydantic models se input validation automatic hoti hai.

Step 2: Docker mein package karo

Agent ko containerize karna matlab use ek standard environment mein package karna � dependencies, Python version, sab fixed. Dockerfile banao, image build karo, aur deploy karo.

dockerfile
# Dockerfile - Agent ko containerize karo
FROM python:3.9-slim

# Working directory set karo
WORKDIR /app

# Dependencies pehle copy karo (caching ke liye)
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Ab baaki code copy karo
COPY . .

# Expose port
EXPOSE 8000

# Command run karo
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
bash
# Requirements file
# requirements.txt
fastapi==0.104.1
uvicorn==0.24.0
pydantic==2.5.2

# Docker commands
# Image build karo
docker build -t dswallah-agent .

# Container run karo
docker run -p 8000:8000 dswallah-agent

# Test karo
curl http://localhost:8000/agent/health
curl -X POST http://localhost:8000/agent/chat \
 -H "Content-Type: application/json" \
 -d '{"message": "hello"}'

Step 3: Scaling setup karo

Production mein ek agent kaafi nahi hota � multiple instances chahiye load handle karne ke liye. Load balancer lagao, auto-scaling configure karo, aur horizontally scale karo.

python
# Scaling configuration example
# docker-compose.yml for multi-container setup

"""
version: '3.8'
services:
 agent:
 build: .
 deploy:
 replicas: 3 # 3 instances chalenge
 resources:
 limits:
 cpus: '0.5' # Har instance ko 0.5 CPU
 memory: 512M
 ports:
 - "8000-8002:8000" # Different ports
 environment:
 - WORKERS=2 # 2 worker processes
 
 nginx:
 image: nginx
 ports:
 - "80:80"
 volumes:
 - ./nginx.conf:/etc/nginx/nginx.conf
 depends_on:
 - agent
"""

# Nginx load balancer config
NGINX_CONF = """
upstream agents {
 server agent:8000;
 server agent:8001;
 server agent:8002;
}

server {
 listen 80;
 location / {
 proxy_pass http://agents;
 proxy_set_header Host $host;
 proxy_set_header X-Real-IP $remote_addr;
 }
}
"""

# Auto-scaling logic (simplified)
class AgentScaler:
 def __init__(self, min_instances=1, max_instances=10):
 self.min = min_instances
 self.max = max_instances
 self.current = min_instances
 self.threshold = 80 # CPU threshold percentage
 
 def check_load(self, cpu_usage):
 if cpu_usage > self.threshold and self.current < self.max:
 self.current += 1
 print(f"Scaling UP: {self.current} instances")
 elif cpu_usage < 30 and self.current > self.min:
 self.current -= 1
 print(f"Scaling DOWN: {self.current} instances")
 return self.current

scaler = AgentScaler()
print(f"Initial instances: {scaler.current}")
print(f"High load: {scaler.check_load(90)} instances")
print(f"Low load: {scaler.check_load(20)} instances")

Step 4: Monitoring lagao

Production agent ki health track karna zaroori hai � logs likho, metrics collect karo, aur alerts setup karo. Jab agent down ho ya slow ho, turant pata chale.

python
# Monitoring and logging setup
import logging
import time
from functools import wraps
from collections import defaultdict

# Logging setup
logging.basicConfig(
 level=logging.INFO,
 format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
 handlers=[
 logging.FileHandler('agent.log'),
 logging.StreamHandler()
 ]
)
logger = logging.getLogger("DSWallahAgent")

# Metrics collector
class Metrics:
 def __init__(self):
 self.request_count = 0
 self.error_count = 0
 self.response_times = []
 self.endpoint_stats = defaultdict(int)
 
 def record_request(self, endpoint, duration, success=True):
 self.request_count += 1
 self.endpoint_stats[endpoint] += 1
 self.response_times.append(duration)
 if not success:
 self.error_count += 1
 
 def get_stats(self):
 avg_time = sum(self.response_times) / len(self.response_times) if self.response_times else 0
 return {
 "total_requests": self.request_count,
 "total_errors": self.error_count,
 "avg_response_time": f"{avg_time:.3f}s",
 "error_rate": f"{(self.error_count/self.request_count*100):.1f}%" if self.request_count > 0 else "0%",
 "endpoints": dict(self.endpoint_stats)
 }

metrics = Metrics()

# Monitoring decorator
def monitor(func):
 @wraps(func)
 async def wrapper(*args, **kwargs):
 start = time.time()
 try:
 result = await func(*args, **kwargs)
 duration = time.time() - start
 metrics.record_request(func.__name__, duration, success=True)
 logger.info(f"{func.__name__} completed in {duration:.3f}s")
 return result
 except Exception as e:
 duration = time.time() - start
 metrics.record_request(func.__name__, duration, success=False)
 logger.error(f"{func.__name__} failed: {str(e)}")
 raise
 return wrapper

# Usage in FastAPI
# @app.get("/agent/health")
# @monitor
# async def health():
# return {"status": "healthy"}

# Print metrics
print("=== Agent Metrics ===")
print(metrics.get_stats())
Pro tip: Production mein Prometheus + Grafana ya Datadog use karo real-time dashboards ke liye. Cloud providers (AWS, Azure, GCP) bhi built-in monitoring dete hain � CloudWatch, Azure Monitor, etc.

Security: Agent ko safe rakho

Deployed agent vulnerable ho sakta hai � authentication lagao, rate limiting karo, input sanitize karo, aur secrets manage karo. Security deployment ka integral part hai.

python
# Security measures for deployed agent
from fastapi import HTTPException, Depends, Header
from fastapi.middleware.cors import CORSMiddleware
import os

# API Key authentication
API_KEY = os.getenv("AGENT_API_KEY", "dswallah-secret-key")

def verify_api_key(x_api_key: str = Header(...)):
 if x_api_key != API_KEY:
 raise HTTPException(status_code=403, detail="Invalid API Key")
 return x_api_key

# Rate limiting (simplified)
class RateLimiter:
 def __init__(self, max_requests=100, window=60):
 self.max = max_requests
 self.window = window
 self.requests = {}
 
 def check(self, client_id):
 now = time.time()
 if client_id not in self.requests:
 self.requests[client_id] = []
 
 # Purane requests hatao
 self.requests[client_id] = [
 r for r in self.requests[client_id] 
 if now - r < self.window
 ]
 
 if len(self.requests[client_id]) >= self.max:
 raise HTTPException(429, "Rate limit exceeded")
 
 self.requests[client_id].append(now)

limiter = RateLimiter(max_requests=50, window=60)

# Input sanitization
def sanitize_input(message: str) -> str:
 """Remove potentially harmful content"""
 dangerous = ["