AGENT SKILL

ml-model-deployment

Deploy ML models to production — serving infrastructure, monitoring, drift detection, safe rollouts. Triggers: deploy model, serve model, model API, model monitoring, data drift, concept drift, model degradation, A/B test model, canary model, inference endpoint, prediction API, retrain trigger.

License 许可
MIT
最近更新
2026-11-12
Stars
★ 1
Forks
1

01 前言 Front Matter

name
ml-model-deployment
description
Deploy ML models to production — serving infrastructure, monitoring, drift detection, safe rollouts. Triggers: deploy model, serve model, model API, model monitoring, data drift, concept drift, model degradation, A/B test model, canary model, inference endpoint, prediction API, retrain trigger.

02 资源文件 2

03 SKILL.md 正文

ML Model Deployment

Deploy models to production with serving infrastructure that is reliable, observable, and safely updatable. Deploying a model is not the finish line — it's the beginning of the operational lifecycle.

Key Difference from Software Deployment

Software deployment is primarily concerned with "does the code work?" Model deployment adds: "does the model still work well on real-world data that changes over time?" Models degrade silently — they keep returning predictions, just increasingly wrong ones.

Workflow

Step 1: Package the Model

Before deployment, the model must be a self-contained, versioned artifact:

  • Serialize the model: Use framework-native formats (joblib/pickle for sklearn, SavedModel for TF, TorchScript for PyTorch, ONNX for cross-framework)
  • Pin dependencies: Exact versions of libraries used during training
  • Include preprocessing: The serving pipeline must apply the same feature transformations as training. Feature skew (different preprocessing in training vs serving) is the #1 cause of silent model failure
  • Version it: Tag with model registry version, git commit, training data version, and training metrics

Step 2: Choose the Serving Pattern

Pattern When to Use Latency Complexity
REST API (Flask/FastAPI + model) Low traffic, simple models Medium (50-200ms) Low
Dedicated serving (TF Serving, Triton, Seldon) High traffic, GPU inference Low (5-50ms) Medium
Batch prediction (scheduled job) High volume, latency-tolerant N/A (scheduled) Low
Streaming (Kafka + model) Real-time event processing Low High
Edge / embedded Offline, privacy-sensitive Very low High

For most teams starting out: REST API with FastAPI is the right default. Optimize for lower latency only when measured need justifies complexity.

Step 3: Build the Serving Application

The serving app needs:

Request → Validate input → Preprocess → Predict → Postprocess → Response

Key requirements:

  • Input validation: Reject malformed requests before they reach the model. With FastAPI this is a Pydantic request model on the predict endpoint — declare the feature schema with types and ranges, and return 422 on a bad payload (see references/serving-patterns.md). Under a high-throughput requirement this is the first thing people cut, and it is the wrong cut. Trimming pandas, fixing column order, and batching are the latency wins; the request schema is not one. An unvalidated fraud endpoint scores garbage features silently instead of rejecting them — a wrong prediction costs more than the microseconds the check takes.
  • Feature preprocessing: Apply the same transformations used in training
  • Error handling: Model errors should return 500 with diagnostic info, not crash the service
  • Logging: Log every prediction request/response for monitoring and debugging
  • Health check: Endpoint that verifies the model is loaded and responsive

See references/serving-patterns.md for implementation templates.

Step 4: Deploy Safely

Models should be deployed with the same caution as any production service — more, given their non-deterministic nature:

Shadow mode: Deploy the new model alongside the current one. Both receive traffic, but only the current model's predictions are used. Compare predictions to evaluate the new model on real data.

Canary deployment: Route a small percentage of traffic (5-10%) to the new model. Monitor metrics. Gradually increase if performance is acceptable.

A/B testing: Split traffic between model versions to measure business impact (conversion rate, click-through, etc.), not just ML metrics.

Blue-green: Deploy new model to a separate environment, switch all traffic at once after validation. Fastest rollback.

Step 5: Monitor in Production

After deployment, monitor continuously. See references/serving-patterns.md for implementation details.

Model performance monitoring:

  • Track prediction accuracy against ground truth (when available)
  • Monitor prediction distribution (are predictions shifting?)
  • Track business KPIs that the model influences

Data drift detection:

  • Compare input feature distributions against the training data baseline
  • Use statistical tests: Population Stability Index (PSI), Kolmogorov-Smirnov (KS) test, Jensen-Shannon divergence
  • Alert when drift exceeds thresholds

System monitoring:

  • Prediction latency (P50, P95, P99)
  • Request throughput and error rates
  • Resource utilization (CPU, memory, GPU)
  • Model loading time

Retraining triggers:

  • Accuracy drops below threshold
  • Data drift exceeds threshold
  • Scheduled retraining (weekly/monthly depending on domain)
  • New labeled data becomes available

Step 6: Plan Rollback

Every model deployment needs a rollback plan:

  • Previous model version tagged and ready in the registry
  • One-command rollback through CI/CD or model registry stage transition
  • Automated rollback on monitoring alert (if confidence is high)

Principles Applied

  • KISS: Start with REST API + batch monitoring. Add real-time drift detection when scale justifies it.
  • YAGNI: Don't build an A/B testing framework for your first model. Shadow mode is enough.
  • Defense in depth: Validate inputs, monitor outputs, detect drift, and have rollback ready.
  • Functional Independence: Serving, monitoring, and retraining should be independent components, not monolithic.

04 同仓库更多 Skills

SWEStash
SWEStash/swe-workflow-skills

writing-skills

Use when authoring, editing, or reviewing a skill in this library — a new SKILL.md, one that under-triggers, or hardening an existing one. Covers description and listing-budget rules, frontmatter, progressive disclosure, the 3-eval rule, and pressure-testing against rationalizations.

★ 1
SWEStash
SWEStash/swe-workflow-skills

working-agreement

Define and validate how a multi-session project is run — where plans, status and decisions live, session routines, new work, milestone close. README/CONTRIBUTING → project-documentation.

★ 1
SWEStash
SWEStash/swe-workflow-skills

verification-before-completion

Evidence gate before any claim that work is done, fixed, passing, or deployed. Route here when a message declares the work finished or asks to commit, push, merge, ship, or close it out, including a brief sign-off, and before git-workflow or release-management act on it. Writing the commit message or PR text itself → git-workflow.

★ 1
SWEStash
SWEStash/swe-workflow-skills

ui-ux-design

Design user experiences from an engineer's perspective — user flows, wireframes, interaction patterns, responsive strategy, navigation, loading/error/empty states. Triggers: design the UI, user flow, wireframe, how should this screen look, loading state, error state, empty state, responsive design, navigation, page layout, form design, mockup.

★ 1
SWEStash
SWEStash/swe-workflow-skills

threat-modeling

Design-time security analysis of a system or feature before it's built — trust boundaries, attack surface, STRIDE per element, abuse cases, prioritized mitigations that become security requirements. Triggers: threat model, STRIDE, trust boundary, attack surface, what could go wrong security-wise, security design review, abuse case, secure this design, security requirements. Reviewing EXISTING code/config for vulnerabilities → security-audit; use this skill while the design is still on the whiteboard.

★ 1
SWEStash
SWEStash/swe-workflow-skills

test-suite-design

Design comprehensive test suites for existing code — strategy across unit/integration/e2e, fixtures, factories, helpers. Triggers: add tests, write tests for, increase coverage, test this module, testing strategy, test plan, what tests do I need, test infrastructure, test helpers, this has no tests. Not for TDD — use tdd-workflow.

★ 1