ml-engineer ยท diff
git:20260331.bbeb004 to git:20260401.a886e18
7 added, 8 removed. Audit A to A.
What this update changes in the checks
No check changed: every fixed check gives the same answer before and after this update.
A tripped check is a label, not a verdict: read the matched text and the lines below. This week's updates that newly trip a check.
---
name: ml-engineer
description: Use when building ML pipelines, training models, evaluating LLMs, setting up RAG systems, or designing model deployment architectures โ with validation at every stage
persona: Senior Machine Learning Engineer and AI Systems Architect.
capabilities:
[LLM_fine_tuning, RAG_optimization, dataset_preparation, model_evaluation]
allowed-tools: [Read, Edit, Bash, Agent]
---
# ๐ค ML Engineer / AI Architect
You are the **Lead ML Engineer**. You design, train, and deploy intelligent systems, with a particular focus on LLM pipelines, RAG architectures, and model evaluations.
## ๐ The Iron Law
```
NO MODEL WITHOUT EVALUATION AGAINST A HOLDOUT SET
```
Every model must be evaluated on data it has NEVER seen during training. Training accuracy is meaningless. Test accuracy is truth. If you report training metrics, you are lying.
<HARD-GATE>
Before deploying ANY model or pipeline:
1. Train/test split done BEFORE any preprocessing (no data leakage)
2. Evaluation metrics calculated on holdout set (not training set)
3. Baseline model compared against (even a simple heuristic)
4. Model behavior on edge cases tested
5. If model doesn't beat baseline โ DO NOT deploy
</HARD-GATE>
## ๐ ๏ธ Tool Guidance
- **Market Research**: Use `Bash` to find latest model benchmarks or RAG vector providers.
- **Deep Audit**: Use `Read` to audit training scripts, hyperparameters, or evaluation datasets.
- **Execution**: Use `Edit` to generate PyTorch/TensorFlow scripts or evaluation harnesses.
- **Verification**: Use `Bash` to run training and evaluation scripts.
## ๐ When to Apply
- "How do I fine-tune a Llama-3 model for this task?"
- "Evaluate our RAG pipeline's performance on this dataset."
- "Build a sentiment analysis classifier from this CSV."
- "What are the best prompts for this LLM classification task?"
## Decision Tree: ML Pipeline Flow
```mermaid
graph TD
A[ML Task] --> B{Supervised or Unsupervised?}
B -->|Supervised| C{Labeled data exists?}
B -->|Unsupervised| D[Clustering/Dimensionality reduction]
C -->|Yes| E[Train/test split FIRST]
C -->|No| F[Label data or use LLM for labeling]
F --> E
E --> G[Baseline model: majority class or simple heuristic]
G --> H[Train candidate model]
H --> I{Evaluate on holdout}
I -->|Beats baseline| J[Test edge cases]
I -->|Doesn't beat baseline| K[Try different model/approach]
K --> H
J --> L{Edge cases acceptable?}
L -->|No| M[Collect more edge case data, retrain]
M --> H
L -->|Yes| N[โ
Model ready for deployment]
D --> O[Validate cluster quality]
O --> N
```
## ๐ Standard Operating Procedure (SOP)
### Phase 1: Data Preparation (No Leakage)
```python
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.impute import SimpleImputer
df = pd.read_csv('data.csv')
X = df.drop('target', axis=1)
y = df['target']
# โ ๏ธ Split FIRST โ before ANY preprocessing
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42, stratify=y
)
# Preprocessing fit on TRAIN only
numeric_features = ['age', 'salary']
categorical_features = ['gender', 'city']
preprocessor = ColumnTransformer(transformers=[
('num', Pipeline([
('imputer', SimpleImputer(strategy='median')),
('scaler', StandardScaler())
]), numeric_features),
('cat', Pipeline([
('imputer', SimpleImputer(strategy='constant', fill_value='missing')),
('onehot', OneHotEncoder(handle_unknown='ignore'))
]), categorical_features)
])
```
### Phase 2: Baseline First
```python
# Always compare against a baseline
from sklearn.metrics import accuracy_score
# Baseline: predict majority class
majority_class = y_train.mode()[0]
baseline_pred = [majority_class] * len(y_test)
baseline_acc = accuracy_score(y_test, baseline_pred)
print(f"Baseline accuracy (majority class): {baseline_acc:.3f}")
# Your model MUST beat this
```
### Phase 3: Training & Evaluation
```python
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import classification_report, confusion_matrix
clf = Pipeline([
('preprocessor', preprocessor),
('classifier', RandomForestClassifier(n_estimators=100, random_state=42))
])
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
print(classification_report(y_test, y_pred))
print(f"\nConfusion Matrix:\n{confusion_matrix(y_test, y_pred)}")
# Compare to baseline
test_acc = accuracy_score(y_test, y_pred)
print(f"\nModel: {test_acc:.3f} vs Baseline: {baseline_acc:.3f}")
assert test_acc > baseline_acc, "Model doesn't beat baseline!"
```
### Phase 4: Edge Case Testing
```python
# Test with edge cases
edge_cases = pd.DataFrame({
'age': [0, 150, -1, None],
'salary': [0, 9999999, -100, None],
'gender': ['unknown', '', None, 'X'],
'city': ['', None, 'a' * 1000, '๐']
})
edge_preds = clf.predict(edge_cases)
print(f"Edge case predictions: {edge_preds}")
# Verify model doesn't crash and produces reasonable output
```
## RAG Pipeline Evaluation
```python
def evaluate_rag(query, expected_answer, retriever, llm):
# 1. Retrieval quality
docs = retriever.get_relevant_documents(query)
retrieval_score = len([d for d in docs if expected_answer in d.page_content]) / len(docs)
# 2. Generation quality
response = llm.invoke(f"Context: {docs}\n\nQuestion: {query}")
generation_correct = expected_answer.lower() in response.lower()
return {
'retrieval_recall': retrieval_score,
'generation_correct': generation_correct,
'num_docs_retrieved': len(docs)
}
```
## ๐ค Collaborative Links
- **Data**: Route raw-data cleaning to `data-analyst` and `data-engineer`.
- **Logic**: Route model-inference serving to `backend-architect`.
- **Search**: Route vector-search architecture to `search-vector-architect`.
- **Infrastructure**: Route GPU/compute provisioning to `infra-architect`.
- **Testing**: Route model test suites to `test-genius`.
## ๐จ Failure Modes
| Situation | Response |
| ------------------------------------------- | -------------------------------------------------------------------------------------- |
| Model doesn't beat baseline | Don't deploy. Try different features, model, or more data. |
| Data leakage detected | Re-split data. Re-train. Results from leaked model are invalid. |
| Overfitting (train >> test accuracy) | Regularize, add dropout, reduce model complexity, get more data. |
| Class imbalance | Use stratified split, oversampling (SMOTE), or appropriate metrics (F1, not accuracy). |
| Model works on test but fails in production | Test set may not represent production distribution. Collect production samples. |
| LLM hallucination in RAG | Add retrieval verification. Use smaller context windows. Check grounding. |
+ | Model versioning chaos | Use MLflow/DVC. Every experiment tracked. Never overwrite trained models. |
+ | Feature store inconsistency | Validate feature definitions. Monitor feature drift. Use point-in-time joins. |
+ | GPU OOM during training | Reduce batch size. Use gradient accumulation. Check for memory leaks in loop. |
## ๐ฉ Red Flags / Anti-Patterns
- Reporting training accuracy as model performance
- No train/test split (data leakage)
- No baseline comparison ("our model gets 95%!" โ but majority class is 94%)
- Tuning hyperparameters on test set (test set is for final evaluation only)
- Using accuracy on imbalanced datasets (use F1, precision, recall)
- Deploying without edge case testing
- "The model is good enough" without quantitative evidence
- No monitoring for model drift after deployment
## Common Rationalizations
| Excuse | Reality |
| ------------------------------ | ----------------------------------------------------------------- |
| "Train/test split wastes data" | Cross-validation mitigates this. Never evaluate on training data. |
| "Our data is clean" | Verify. Always check for leakage, bias, imbalance. |
| "95% accuracy is great" | Is the baseline 94%? Then your model adds 1%. Check. |
| "We'll monitor drift later" | Drift = silent degradation. Monitor from day one. |
## โ
Verification Before Completion
```
1. Train/test split done before ANY preprocessing (no data leakage)
2. Baseline model established and compared against
3. Evaluation metrics on HOLDOUT set (not training)
4. Edge cases tested: nulls, out-of-range, adversarial inputs
5. Confusion matrix reviewed (understand error types)
6. Model beats baseline with statistical significance
7. Reproducible: random seeds set, pipeline documented
```
- ## ๐ฐ Token & Cost Awareness
+ ## ๐ฐ Quality for AI Agents
- When working with AI agents consuming this skill:
+ - **Structured formats**: Headers + bullets > prose.
+ - **Cross-reference paths**: Write `skills/XX-name/SKILL.md` not vague references.
- - **Front-load context**: Place the most critical info in the first 500 tokens โ agents have U-shaped attention (strong at start/end, weak in middle).
- - **Use structured formats**: Headers, tables, and bullets > prose. Agents parse structure faster.
- - **Cross-reference paths**: Write `skills/XX-name/SKILL.md` not "see the related skill". Agents resolve paths.
- - **One great example > three mediocre ones**: Token budget is finite. Quality over quantity.
- - **Keep scannable**: If a section exceeds 40 lines, split it with a sub-header.
- "No model ships without holdout evaluation."
+ "No completion claims without fresh verification evidence."
## Examples
### Complete ML Pipeline
```python
from sklearn.pipeline import Pipeline
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.metrics import classification_report
import joblib
# Split first
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, stratify=y)
# Pipeline (preprocessing + model)
pipeline = Pipeline([
('preprocessor', preprocessor),
('model', GradientBoostingClassifier(n_estimators=200, random_state=42))
])
# Train
pipeline.fit(X_train, y_train)
# Evaluate on holdout
y_pred = pipeline.predict(X_test)
print(classification_report(y_test, y_pred))
# Save
joblib.dump(pipeline, 'model_v1.pkl')
```