Generative AI & LLM

Module 15 of 16

Module 15: Production Deployment

3 min read560 words
What you'll learn
Design HTTP APIs for LLM features with streaming, auth, and versioningApply caching strategies that preserve correctnessImplement rate limiting and fair usage controlsMonitor latency, errors, and cost in productionPackage services with Docker and ship minimal cloud-ready artifacts

Duration: 5 hours | Difficulty: Advanced | Prerequisites: Modules 03–08, basic DevOps

Learning Objectives

By the end of this module, you will be able to:

  • Design HTTP APIs for LLM features with streaming, auth, and versioning
  • Apply caching strategies that preserve correctness
  • Implement rate limiting and fair usage controls
  • Monitor latency, errors, and cost in production
  • Package services with Docker and ship minimal cloud-ready artifacts

1. API Design for AI Services

FastAPI LLM Service

ChoiceRecommendation
POST /v1/chatVersion in path
StreamingStreamingResponse or SSE
AuthAPI keys + rotation; OAuth for humans
IdempotencyIdempotency-Key header for billable ops

Contract tips: document max_tokens defaults, timeout behavior, and error JSON shape—clients will bake in assumptions fast.

Try This! Write an OpenAPI snippet before code—forces you to name error cases.

2. Caching Strategies

Semantic Caching

Hash or embed prompts; return prior answer if similar enough. Great for FAQs; risky for personalized or regulated answers.

Cache typeRisk
Exact keyStale but safe logically
SemanticNear-duplicate collisions

Concept: Cache policies with TTLs and user-segment keys—never cross-tenant.

3. Rate Limiting

Token bucket or leaky bucket per user, tenant, and global. Pair with queue for burst smoothing.

SignalAction
429 stormBackoff + circuit breaker
Single hot keyFair-share throttle

4. Monitoring and Observability

Custom Metrics Collector

Track: p50/p95 latency, tokens in/out, dollars/request, error codes, tool-call success rate.

LangSmith Integration

Trace chains in dev/staging; mirror critical spans to your APM (OpenTelemetry) in prod if vendor-approved.

DashboardQuestion it answers
CostWhich route burns budget?
ErrorsProvider vs app fault?
QualityEval scores trending?

Fun Fact: The first production incident is always timeout-related—set client and server deadlines.

5. Cost Optimization

LeverDetail
Model routingSmall LLM for triage
Prompt compressionDrop boilerplate
CacheOnly where safe
Batch offline jobsCheaper throughput
Anti-patternSymptom
Always max_tokens=4096You pay for unused completion headroom
No per-tenant budgetOne customer drains the pool
Silent retries3× cost on flaky paths

Try This! Add a temporary middleware that logs estimated USD per request—share the histogram in your next retro.

6. Deployment with Docker

Key Example: Minimal multi-stage pattern—slim base image, non-root user, env-injected secrets.

dockerfile
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY app ./app
ENV PYTHONUNBUFFERED=1
USER nobody
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8080"]

Pair with docker compose for local API + vector DB; in cloud, map secrets to the orchestrator’s secret store—not baked into layers.

Key Takeaway

Production LLM services are normal microservices plus token economics—monitor dollars like CPU.

Practice Exercises

Exercise 1: API Gateway (Beginner)

Kong/NGINX rate limit + key auth in front of stub LLM.

Exercise 2: Semantic Cache (Intermediate)

Embeddings + Redis; tune similarity threshold.

Exercise 3: Cost Dashboard (Intermediate)

Export Prometheus metrics from FastAPI.

Exercise 4: Auto-Scaling Strategy (Advanced)

HPA on GPU nodes vs queue workers.

Exercise 5: Full Production Stack (Advanced)

Terraform + CI + canary flag for prompt version.

Mini-Project: Production LLM Service

FastAPI + streaming + Redis cache + structured logs + Dockerfile + README runbook (timeouts, rollback).

Key Takeaways

Key Takeaway

  • Version APIs and prompts together.
  • Cache only when correctness allows; segment by tenant.
  • Rate limits protect wallets and neighbors.
  • Metrics must include tokens and $ alongside latency.
  • Docker + secret injection is table stakes for reproducible deploys.

Resources for Further Learning

← Previous: Guardrails | Next: Capstone Guide →