Topic 4 of 4 · 5 min read
Reviewing agent-written ML code
Goal: Review an agent-written training script and find the leakage, split or metric error that inflates its score.
A coding agent optimises for a script that runs and reports a good number, so when you review its ML code, check how the data was split, what the model could see, and what the metric really measures before you believe the score. Agents rarely lie on purpose, but a leaky pipeline gives them a great score and nothing flags it.
Check yourself · 1 of 3
An agent's churn model reports 0.99 AUC on its first run. What is the best first review step?
Why it matters
An agent given "build a churn model and report the score" will iterate until the metric looks right. If the easiest path to a higher number is leakage, a score of 0.99 AUC arrives with no error. A reviewer who only checks that the code runs ships a model that fails on real traffic.
How to review it
- Find the split first. Where is
train_test_splitor the cross-validation call? Is it before any fitting step? Is it random when the data is time-ordered or has repeat customers? - Check what fit on what. Scalers, imputers, encoders, feature selectors and target encoders must be fitted on training rows only. In scikit-learn this means they belong inside a
Pipeline. - Read the feature list. Any column created after the outcome, such as
cancellation_reasonordays_since_last_loginmeasured after churn, leaks the answer. - Check the metric. Accuracy on a 3% positive class is meaningless. Look for precision, recall, PR-AUC, and for which threshold was used and where it was chosen.
- Look at the test set's use. If hyper-parameters were tuned on it, it is a validation set.
- Ask for a baseline. A dummy model and a simple logistic regression show whether the fancy score is plausible.
Before and after
# agent version: leaks test rows into the scaler
X = StandardScaler().fit_transform(df[features])
X_tr, X_te, y_tr, y_te = train_test_split(X, df["churned"], test_size=0.2)
model = GradientBoostingClassifier().fit(X_tr, y_tr)
print("accuracy", model.score(X_te, y_te))
# reviewed version
X_tr, X_te, y_tr, y_te = train_test_split(
df[features], df["churned"], test_size=0.2, stratify=df["churned"], random_state=0
)
pipe = make_pipeline(StandardScaler(), GradientBoostingClassifier())
pipe.fit(X_tr, y_tr)
print("PR-AUC", average_precision_score(y_te, pipe.predict_proba(X_te)[:, 1]))
The first version scales with statistics from the test rows, which is small leakage here but a serious one for target encoding or feature selection. It also reports accuracy on an imbalanced target.
What goes wrong
- Trusting the headline number. A score that is too good is a reason to look for leakage.
- Random splits on time data. The model trains on the future and tests on the past.
- Duplicated entities across splits. The same customer in both sets lets the model memorise.
- Silent edits. The agent changes the metric, drops hard rows or re-runs with other seeds until the number rises.
Examples
The perfect fraud model
An agent reports 0.998 AUC for a fraud model. The feature list includes chargeback_flag, a field set weeks after the transaction when the bank reverses it. At prediction time that field does not exist. Removing it drops AUC to 0.86, which is the real figure to plan around.
Same customer, both sides
A script splits 80,000 orders at random, but each customer has about 10 orders. The model learns customer habits and scores well on the test split. Splitting by customer (GroupShuffleSplit on customer_id) shows the score on new customers, which is what production sees.
Key points
- An agent optimises the reported score, so a suspiciously high number is a cue to look for leakage, not to celebrate.
- Find the split first and check that every fitted step, including scalers and encoders, sees training rows only.
- Features created after the outcome leak the answer; read the feature list against the prediction time.
- Choose the metric for the data: accuracy on a rare positive class says almost nothing.
- Use time-ordered or group-aware splits when rows are not independent, and ask for a dummy baseline.
Common mistakes
- Approving because the script runs end to end and prints a high score.
- Checking the model code but not the preprocessing that happens before the split.
- Accepting accuracy as the headline metric on a heavily imbalanced target.
- Not asking whether the agent tuned settings on the test set or re-ran with other seeds.
Check yourself · 2 of 3
Where must a StandardScaler be fitted to avoid leakage?
Check yourself · 3 of 3
A model predicts a rare event that occurs in 2% of rows and the agent reports 98% accuracy. Why is the figure weak evidence?
Practice items, not counted towards your mastery.
Start here
- Sentence Transformers documentation (opens in a new tab)DocsFree
How to encode sentences into embeddings and use them for classification, search and clustering.
- SetFit: few-shot text classification (Hugging Face docs) (opens in a new tab)DocsFree
Train a text classifier from a handful of labelled examples on top of sentence embeddings.
- ChatGPT Outperforms Crowd-Workers for Text-Annotation Tasks (opens in a new tab)PaperFree
A study comparing an LLM's labels with crowd-worker labels, and the cost and accuracy of each.
More for this topic
- scikit-learn: common pitfalls and recommended practices (opens in a new tab)ArticleFree
Explains data leakage, why preprocessing belongs inside a Pipeline, and inconsistent preprocessing.
- Cross-validation: evaluating estimator performance (scikit-learn) (opens in a new tab)DocsFree
How to split data, use group-aware splits and keep a held-out test set.