Monitoring and rollback
Monitor signals, not assumptions
Monitoring tracks input quality, request failures, latency, throughput, model versions and eventual prediction quality. Data drift is a distribution change over time; training-serving skew is a mismatch between training and serving data or transformations. Concept drift concerns a changed relationship between inputs and targets. A distribution shift is not by itself evidence that accuracy fell. For the inspection classifier use two bins: counts below 4 and counts at least 4. The baseline has 80 and 20 items, the current window 40 and 60. Total variation distance is half the sum of absolute differences in normalized bin probabilities, here 0.4 or 2/5. It is between zero and one; proportional counts such as 8:2 and 80:20 yield zero. The hypothetical alert threshold 1/4 is a teaching decision, not a universal rule. Fraction represents rational numbers exactly in Python. Histograms must share bin definitions and have nonzero totals; this code uses lists of integer counts and refuses malformed inputs.
from fractions import Fraction
def distribution_shift(baseline, current): # Nonnegative integer counts, matching bins, at least one observation. if (not baseline or len(baseline) != len(current) or any(type(x) is not int or x < 0 for x in baseline + current) or not sum(baseline) or not sum(current)): raise ValueError("invalid histogram") return sum(abs(Fraction(a, sum(baseline)) - Fraction(b, sum(current))) for a, b in zip(baseline, current)) / 2
# Counts of feature values below 4 and at least 4; no labels yet.shift = distribution_shift([80, 20], [40, 60])print(shift)print(shift >= Fraction(1, 4))print(distribution_shift([8, 2], [80, 20]))Investigate before interpreting drift
Compare matched time windows and record sample sizes, feature version, bin definitions and seasonality. Separate malformed-input rates, missing-feature rates, traffic composition, latency percentiles and error rates. A different mix of warehouses could explain the shift with unchanged per-warehouse accuracy. Conversely, accuracy may fall while input histograms look stable. Track delayed labels and join predictions to eventual outcomes using authorized record IDs, exact model versions and timestamps. Compare quality on representative labeled samples and relevant slices; report uncertainty and label coverage. Missing labels mean quality unknown, not zero or perfect. For a high-risk workflow, use human review or safe manual routing while investigating. Never trigger unconditional retraining merely because a drift alert fired.
Define rollback and review gates
A rollback returns serving to a previously validated compatible release, including preprocessing and configuration, not just weights. A previous artifact may have been withdrawn for privacy or security; it must not be restored blindly. In the local Boolean gate, a contract violation, incident or measured quality failure takes priority over approvals. If a compatible reviewed rollback is available, request rollback; otherwise pause and escalate. Without labels or with drift, investigate. Without approval, await review. Only healthy, reviewed evidence qualifies for the next stage. The incident flag represents a predefined severity threshold; the rollback-ready flag summarizes separate checks, not an actual authorization. The third example is eligible, not deployed. A real controller must use authenticated current evidence and independently enforce policy.
def rollout_decision(contract_ok, incident, labels_ready, quality_ok, drift_alert, approved, rollback_ready): # All inputs are trusted Boolean flags. No deployment is performed. if not contract_ok or incident or (labels_ready and not quality_ok): return "request_rollback" if rollback_ready else "pause_and_escalate" if drift_alert or not labels_ready: return "investigate" if not approved: return "await_review" return "eligible_for_next_stage"
print(rollout_decision(True, False, False, False, True, False, True))print(rollout_decision(True, False, True, False, False, True, True))print(rollout_decision(True, False, True, True, False, True, True))Make recovery observable
Keep immutable deployment records and an audit trail of alias changes, who approved them, rollout fraction, feature contract and monitored metrics. Before rollout, rehearse routing back to a compatible stable version with sufficient capacity; if it is unsafe, stop automated decisions and escalate to the owner. During rollback, stop further exposure, preserve minimal incident evidence, verify model and preprocessing versions, check health and delayed quality signals, reconcile any already-produced batch outputs, and communicate to impacted owners. Restoring an alias cannot undo earlier decisions. A canary needs representative traffic and comparable labels; shadow predictions must remain non-actionable. Actual retraining, registry transactions, concurrent deployment orchestration and cloud monitoring are outside this local lab.
Practice: investigate an alert
Use baseline [80, 20] and current [40, 60]. Calculate the distance and explain why labels are necessary for an accuracy claim. Set labels_ready to False with approval True: predict investigate. Supply a verified quality failure with rollback_ready False: predict pause_and_escalate. Then construct a healthy labeled case with approval False and record the missing review. Write an incident note containing window IDs, exact versions, contract, counts, evidence gaps, rollback eligibility, owner and next review time. Reference ref-ai-monitoring-and-rollback. Next chapter introduces prompt and model observability for LLM services.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Monitoring and rollback quiz
Assess experiment traceability, deployment gates, drift and rollback.
What does the 0.4 total variation distance demonstrate?
Why normalize histogram counts?
When labels are missing and no critical incident exists, what does the gate return?
What must be checked before rollback?
What if verified quality fails and no safe rollback is available?