67.1 Observability Stack for AI Systems
┌─────────────────────────────────────────────┐
│ Application Observability │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
│ │ Metrics │ │ Logs │ │ Traces │ │
│ │(Prometheus/│(Structured│(Distributed│ │
│ │ CloudWatch)│ JSON) │ tracing) │ │
│ └──────────┘ └──────────┘ └──────────┘ │
│ │
│ AI-Specific Observability │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
│ │ LLM │ │ Agent │ │ Data │ │
│ │ Quality │ │ Behavior │ │ Drift │ │
│ │ Metrics │ │ Traces │ │ Detector │ │
│ │ │ │ │ │ │ │
│ │-Accuracy │ │-Steps │ │-Feature │ │
│ │-Latency │ │-Tools │ │ drift │ │
│ │-Cost │ │-Decisions│ │-Prediction│ │
│ │-Halluc. │ │-Errors │ │ drift │ │
│ └──────────┘ └──────────┘ └──────────┘ │
└─────────────────────────────────────────────┘
67.2 Types of Drift and Detection Methods
| Drift Type | What Changes | Detection Method | Typical Action |
|---|---|---|---|
| Data drift | Input feature distributions shift over time | KL divergence, Population Stability Index (PSI), Kolmogorov-Smirnov test | Investigate upstream data source before assuming model issue |
| Concept drift | The relationship between features and the true outcome changes | Track model performance (AUC, accuracy) on a rolling recent window against a stable baseline | Retrain model on recent data |
| Prediction drift | The model’s output distribution shifts even without confirmed data/concept drift | Chi-squared test or distributional comparison on predicted scores over time | Investigate both model and data; can be an early warning before concept drift is confirmed |
| Label/outcome drift | The distribution of true outcomes shifts | Track outcome rate trends over time | May be legitimate (external policy/environment change) rather than a model failure — investigate before reacting |
67.3 Drift Detection Implementation Example
# Drift detection using a managed model monitoring service
from model_monitoring import DefaultModelMonitor
monitor = DefaultModelMonitor(
role=role,
instance_count=1,
instance_type="ml.m5.xlarge",
max_runtime_in_seconds=1800
)
# Schedule weekly monitoring against a stored baseline
monitor.create_monitoring_schedule(
endpoint_input=endpoint_name,
output_s3_uri="s3://monitoring/results/",
statistics_s3_uri="s3://monitoring/baseline/",
constraints_s3_uri="s3://monitoring/constraints/",
schedule_cron_expression="cron(0 0 ? * SUN *)" # weekly
)
# Simplified PSI (Population Stability Index) calculation
import numpy as np
def population_stability_index(expected, actual, buckets=10):
breakpoints = np.linspace(0, 100, buckets + 1)
expected_percents, _ = np.histogram(expected, bins=np.percentile(expected, breakpoints))
actual_percents, _ = np.histogram(actual, bins=np.percentile(expected, breakpoints))
expected_percents = expected_percents / len(expected)
actual_percents = actual_percents / len(actual)
# avoid div-by-zero
expected_percents = np.where(expected_percents == 0, 1e-6, expected_percents)
actual_percents = np.where(actual_percents == 0, 1e-6, actual_percents)
psi = np.sum((actual_percents - expected_percents) *
np.log(actual_percents / expected_percents))
return psi
# PSI < 0.1 -> no significant shift
# PSI 0.1-0.2 -> moderate shift, monitor closely
# PSI > 0.2 -> significant shift, investigate/retrain
67.4 LLM/Agent-Specific Monitoring Metrics
| Metric | How to Track | Alert Threshold |
|---|---|---|
| Hallucination rate | LLM-as-judge on sampled outputs | >2% |
| Format compliance | JSON schema validation | <98% |
| Latency (P95) | Request timing | >5 seconds |
| Token usage | Per-request token count | >2x expected average |
| Cost per task | Aggregate token/tool cost | Above defined budget per task |
| Tool call failure rate | Tool execution monitoring | >5% |
| PII/sensitive data leakage | Regex + NER scan on outputs | Any occurrence (zero tolerance) |
67.5 Root-Cause Triage for Drift Alerts
Drift alert fires
│
▼
Is it data drift, concept drift, or prediction drift? (per table above)
│
┌───┼──────────────────────┐
▼ ▼ ▼
Data Concept Prediction
drift drift drift
│ │ │
▼ ▼ ▼
Check Compare recent Investigate both
upstream performance vs. data and model;
data stable baseline treat as early
pipeline (rolling window) warning
for bugs │
│ ▼
│ Confirmed
│ degradation?
│ ┌───┴───┐
│ No Yes
│ │ │
│ ▼ ▼
│ Continue Retrain with
│ monitoring recent data,
│ validate, deploy
▼
Fix pipeline,
no retrain needed
Interview Q&A — Section 67
Q1: How do you distinguish data drift from concept drift, and why does the distinction matter for the fix you apply? A: Data drift is a shift in the input feature distributions themselves (e.g., a new category of customer starts appearing, or feature values trend outside the historical range) — detected via distributional tests like PSI or KL divergence comparing recent inputs to a training-time baseline. Concept drift is a shift in the relationship between features and the true outcome (the same inputs now correlate with a different outcome than they used to) — detected by tracking actual model performance (AUC, accuracy) on a rolling recent window against a stable historical baseline, since distributional tests on inputs alone won’t catch it. The distinction matters because the fixes are different: data drift often traces back to an upstream pipeline bug or a genuine new population the model was never trained on and might just need investigation, not necessarily retraining; concept drift means the model’s learned relationships are now stale and retraining on recent data is the actual fix. Conflating the two leads to either unnecessary retraining cycles or missing a real pipeline bug by assuming the model just needs refreshing.
Q2: Design a drift detection and retraining trigger system for a production model that can’t tolerate silent performance decay. A: Weekly scheduled data-drift checks (PSI/KL divergence per key feature against the training baseline, alerting above a threshold like PSI > 0.2), continuous tracking of prediction distribution shifts as an early-warning signal, and rolling-window performance monitoring comparing recent actual-outcome-vs-prediction accuracy against the deployed baseline wherever ground truth becomes available with reasonable latency. Retraining triggers automatically on any of: performance drop beyond a defined threshold versus baseline, confirmed data or concept drift beyond threshold, or a scheduled cadence (e.g., quarterly) regardless of detected drift, as a backstop against slow, sub-threshold decay that individual checks might miss. Every retrain goes back through the same evaluation gate as the original model (Section 64) before promotion, so a retraining trigger never bypasses the quality bar that got the current model into production in the first place.
Q3: What LLM/agent-specific metrics do you monitor that a traditional ML monitoring stack wouldn’t cover, and why do they need dedicated tooling? A: Traditional ML monitoring is built around a fixed feature vector and a single prediction — it doesn’t naturally cover hallucination rate (requires sampling outputs and running them through an LLM-as-judge or faithfulness check), format compliance (schema validation on generative output, which is non-deterministic by nature), token usage and cost-per-task (variable per request in a way a classical model’s fixed compute cost never was), tool call failure rate and reasoning trace quality for agents, or PII/sensitive-content leakage scanning on free-text output. These require purpose-built AI observability tooling — LLM trace loggers, sampled judge evaluation pipelines, and content-safety scanners — layered on top of, not replacing, the standard infrastructure metrics/logs/traces stack.