MLOps Observability
Goal
To implement a "Glass Box" system where every result is Reproducible, every asset has Lineage, and system health is Monitored, Alerted on, and Explained.
Prerequisites
- Language: Python 3.14
- Context: Production monitoring and debugging.
- Platform Suggestion: MLflow 3.15, SHAP, Evidently, ...
Instructions
1. Guarantee Reproducibility
Consistency is key. For instance:
- Randomness: Set seeds for
random, numpy, torch, tensorflow.
- Dependencies:
uv.lock is the reproducibility mechanism for Python. It records the exact resolved version and hash of every direct and transitive dependency, and uv sync --frozen installs exactly that — the same set on a laptop, in CI, and in the image.
- Tools:
mise.lock does the same job for the binaries that are not Python packages (dprint, gitleaks, trivy, actionlint, zizmor, ...), recording version, URL, and checksum per platform. Commit both lockfiles; between them, "works on my machine" stops being a category of bug.
- Builds:
mise run build is a plain uv build producing a wheel and an sdist. Its reproducibility comes from the locked inputs above, not from a build flag — do not expect uv build to pin anything by itself.
- Environment: Ship the same locked set into a
docker image (uv sync --frozen), so the runtime matches what was tested.
- Code: Track the git commit hash for every run, and fail the pipeline on a dirty working tree so a run can always be traced back to a commit.
2. Track Data Lineage
Know the origin of your data. For instance:
- Datasets: Create MLflow Datasets with
mlflow.data.from_pandas.
- Logging: Log inputs to MLflow context with
mlflow.log_input.
- Store: Keep tracking and registry on a SQL backend (
sqlite:///mlflow.db locally, Postgres or a tracking server in production). Lineage queries are relational queries; the deprecated file store cannot answer them and does not support the model registry at all.
- Versioning: Version data files (e.g.,
data/v1.csv) or use DVC.
- Transformations: Log preprocessing parameters mapping data versions to model versions.
3. Monitoring & Drift Detection
Watch for silent failures. For instance:
- Validation: Gate models against quality thresholds with
mlflow.validate_evaluation_results (MLflow 3).
- Drift: Use
evidently to compare reference (training) vs current (production) data.
- Detect Data Drift (input distribution changes) and Concept Drift (relationship changes).
- System: Enable MLflow System Metrics (
log_system_metrics=True) for CPU/GPU.
4. Alerting
Don't stare at dashboards. For instance:
- Local: Use
plyer for desktop notifications during long training runs.
- Production: Use
PagerDuty (critical) or Slack (warnings).
- Thresholds: Use Static (fixed value) or Dynamic (anomaly detection) rules.
- Action: Alerts must link to a dashboard or playbook.
5. Explainability (XAI)
Trust but verify. For instance:
- Global: Use Feature Importance (e.g., Random Forest) to understand overall logic.
- Local: Use
SHAP values to explain individual predictions.
- Artifacts: Save explanations (plots/tables) as MLflow artifacts.
6. Infrastructure & Costs
Optimize resources. For instance:
- Tags: Tag runs with
project, env, user.
- Costs: Log
run_time and instance type to estimate ROI.
7. Observability of the Repository Itself
The pipeline that produces the model needs the same treatment.
- One Gate:
mise run all (format -> check -> test -> build) is the signal that a change is releasable. CI runs that exact task, so a green pipeline and a green laptop mean the same thing.
- Static Guarantees: Ruff 0.16 and
ty 0.0.69 run inside mise run check, alongside the pip-audit, gitleaks, and trivy scans — quality signals you get on every commit, not once a quarter.
- Written Down:
AGENTS.md records the commands, the definition of done, and the conventions, so an AI assistant debugging a production incident reads the same runbook a human does.
Self-Correction Checklist
1---2name: mlops-observability3description: Make an ML system a glass box with reproducible runs, MLflow dataset lineage, drift monitoring, alerting, and SHAP explanations. Use when a deployed model needs traceability, monitoring, alerting, or explanation.4license: MIT5---67# MLOps Observability89## Goal1011To implement a "Glass Box" system where every result is **Reproducible**, every asset has **Lineage**, and system health is **Monitored**, **Alerted** on, and **Explained**.1213## Prerequisites1415- **Language**: Python 3.1416- **Context**: Production monitoring and debugging.17- **Platform Suggestion**: MLflow 3.15, SHAP, Evidently, ...1819## Instructions2021### 1. Guarantee Reproducibility2223Consistency is key. For instance:24251. **Randomness**: Set seeds for `random`, `numpy`, `torch`, `tensorflow`.261. **Dependencies**: `uv.lock` is the reproducibility mechanism for Python. It records the exact resolved version and hash of every direct and transitive dependency, and `uv sync --frozen` installs exactly that — the same set on a laptop, in CI, and in the image.271. **Tools**: `mise.lock` does the same job for the binaries that are not Python packages (`dprint`, `gitleaks`, `trivy`, `actionlint`, `zizmor`, ...), recording version, URL, and checksum per platform. Commit both lockfiles; between them, "works on my machine" stops being a category of bug.281. **Builds**: `mise run build` is a plain `uv build` producing a wheel and an sdist. Its reproducibility comes from the locked inputs above, not from a build flag — do not expect `uv build` to pin anything by itself.291. **Environment**: Ship the same locked set into a `docker` image (`uv sync --frozen`), so the runtime matches what was tested.301. **Code**: Track the git commit hash for every run, and fail the pipeline on a dirty working tree so a run can always be traced back to a commit.3132### 2. Track Data Lineage3334Know the origin of your data. For instance:35361. **Datasets**: Create MLflow Datasets with `mlflow.data.from_pandas`.371. **Logging**: Log inputs to MLflow context with `mlflow.log_input`.381. **Store**: Keep tracking and registry on a SQL backend (`sqlite:///mlflow.db` locally, Postgres or a tracking server in production). Lineage queries are relational queries; the deprecated file store cannot answer them and does not support the model registry at all.391. **Versioning**: Version data files (e.g., `data/v1.csv`) or use DVC.401. **Transformations**: Log preprocessing parameters mapping data versions to model versions.4142### 3. Monitoring & Drift Detection4344Watch for silent failures. For instance:45461. **Validation**: Gate models against quality thresholds with `mlflow.validate_evaluation_results` (MLflow 3).471. **Drift**: Use `evidently` to compare `reference` (training) vs `current` (production) data.48 - Detect Data Drift (input distribution changes) and Concept Drift (relationship changes).491. **System**: Enable MLflow System Metrics (`log_system_metrics=True`) for CPU/GPU.5051### 4. Alerting5253Don't stare at dashboards. For instance:54551. **Local**: Use `plyer` for desktop notifications during long training runs.561. **Production**: Use `PagerDuty` (critical) or `Slack` (warnings).571. **Thresholds**: Use Static (fixed value) or Dynamic (anomaly detection) rules.581. **Action**: Alerts must link to a dashboard or playbook.5960### 5. Explainability (XAI)6162Trust but verify. For instance:63641. **Global**: Use Feature Importance (e.g., Random Forest) to understand overall logic.651. **Local**: Use `SHAP` values to explain _individual_ predictions.661. **Artifacts**: Save explanations (plots/tables) as MLflow artifacts.6768### 6. Infrastructure & Costs6970Optimize resources. For instance:71721. **Tags**: Tag runs with `project`, `env`, `user`.731. **Costs**: Log `run_time` and instance type to estimate ROI.7475### 7. Observability of the Repository Itself7677The pipeline that produces the model needs the same treatment.78791. **One Gate**: `mise run all` (format -> check -> test -> build) is the signal that a change is releasable. CI runs that exact task, so a green pipeline and a green laptop mean the same thing.801. **Static Guarantees**: Ruff 0.16 and `ty` 0.0.69 run inside `mise run check`, alongside the `pip-audit`, `gitleaks`, and `trivy` scans — quality signals you get on every commit, not once a quarter.811. **Written Down**: `AGENTS.md` records the commands, the definition of done, and the conventions, so an AI assistant debugging a production incident reads the same runbook a human does.8283## Self-Correction Checklist8485- [ ] **Seeds**: Are random seeds fixed?86- [ ] **Lockfiles**: Are `uv.lock` and `mise.lock` committed, and does the image install with `--frozen`?87- [ ] **Inputs**: Are input datasets logged to MLflow, on a SQL-backed store?88- [ ] **System Metrics**: Is `log_system_metrics` enabled?89- [ ] **Explanations**: Are SHAP values generated and stored as artifacts?90- [ ] **Alerts**: Are thresholds defined for failures?91- [ ] **Gate**: Does `mise run all` pass, and does CI run that same task?