DeploymentMLOpsProductionDevOps
Deploying AI Models to Production: A Complete Guide
212AY Team·2026-05-20·16 min
Deploying AI models to production is different from training them. This guide covers the entire deployment pipeline.
The Deployment Pipeline
- Model export: Convert to optimized format
- Containerization: Package with dependencies
- API development: Create inference endpoint
- Deployment: Deploy to cloud or on-premise
- Monitoring: Track performance and drift
- Scaling: Handle increasing load
- Horizontal scaling: Add more instances behind a load balancer
- Model quantization: Reduce model size for faster inference
- Batching: Process multiple requests together
- Caching: Cache results for common inputs
- Use spot instances for batch processing
- Cache frequently requested predictions
- Quantize models to reduce GPU memory
- Use model distillation for simpler tasks
- Containerized with Docker
- Deployed on AWS ECS with auto-scaling
- Processes 10,000+ transactions per minute
- 99.9% uptime with multi-AZ deployment
- Under 100ms latency per prediction
Step 1: Model Export
Export your trained model to an optimized format:
# PyTorch to TorchScript
import torch
model.eval()
example_input = torch.randn(1, 3, 224, 224)
traced_model = torch.jit.trace(model, example_input)
traced_model.save("model.pt")
# Or use ONNX for cross-platform
import torch.onnx
torch.onnx.export(model, example_input, "model.onnx")
Step 2: Containerization
FROM python:3.10-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY model.pt .
COPY app.py .
EXPOSE 8000
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]
Step 3: API Development (FastAPI)
from fastapi import FastAPI, File, UploadFile
import torch
from PIL import Image
import io
app = FastAPI()
model = torch.jit.load("model.pt")
model.eval()
@app.post("/predict")
async def predict(file: UploadFile = File(...)):
image = Image.open(io.BytesIO(await file.read()))
# Preprocess and predict
tensor = preprocess(image)
with torch.no_grad():
output = model(tensor)
return {"prediction": postprocess(output)}
@app.get("/health")
async def health():
return {"status": "healthy"}
Step 4: Deployment Options
Serverless: Use AWS Lambda, Google Cloud Run, or Vercel for lightweight models.
GPU instances: Use AWS SageMaker, GCP AI Platform, or dedicated GPU servers.
Edge deployment: Use TensorFlow Lite, ONNX Runtime, or CoreML for mobile.
Step 5: Monitoring
# Track key metrics
import time
import logging
def predict_with_monitoring(input_data):
start = time.time()
result = model.predict(input_data)
latency = time.time() - start
logging.info(f"Prediction: {result}, Latency: {latency:.3f}s")
# Check for data drift
check_drift(input_data)
return result
Step 6: Scaling
Cost Optimization
Real Example
A Moroccan fintech deployed an AI fraud detection model:
Next Steps
Our "Build with LLMs" programme teaches production deployment of AI applications.