Fine-tuning a 322M Model for Coding Agent Safety: What 3 Runs and 2 Failures Taught Me A developer built laya-guard, a hook that sits between coding agents like Claude Code and Cursor and the shell, routing commands through regex rules and a fine-tuned 322M-parameter Laya model that answers allow, review or block. Three fine-tuning runs on a free Colab GPU revealed that a first run learned a spurious "short means dangerous" signal from length-skewed training data, catching 39/43 attacks but allowing only 23/50 everyday commands, while a second run with 206 targeted examples raised everyday allows to 49/50 but dropped attack detection to 31/43. laya-guard https://github.com/scs0209/laya-guard sits between a coding agent and your shell. Every command Claude Code or Cursor, Codex, … wants to run goes through a hook first. Rules go first: regexes for what's always destructive rm -rf ~/ and the ways a shell lets you spell it , and a safe lane for what's obviously routine ls , git status , npm test . What the rules can't settle goes to a small local model, Laya 322M parameters, multilingual , which answers allow, review ask the user or block. The model was the weak part. On the benchmark's 50 everyday commands, the model alone would ask about half of them. The rules saved most of that, but every unnecessary question teaches people to click "yes" without reading. And Laya's model card describes it as a base to fine-tune, not something to use as-is. I wanted to see what fine-tuning would actually change, so I tried it. It took three runs. The first one was too strict, the second too lenient, and the third is what laya-guard runs now. Here's what happened each time and what I'd do differently. The best source of "commands an agent actually runs" is my own Claude Code and Codex sessions. That gave me about 13,000 shell commands. Before anything else: ~ ; tokens, emails and long opaque strings become placeholders. 920 commands were left. The rules already decide 224 of them, and the other 696 are exactly what the model judges, so they needed labels. I wrote a one-page labeling guide, had six agents label 116 each, and went through the disagreements myself. None of the real commands deserved "block", so I also wrote risky examples by hand. All three runs trained on a free Google Colab GPU. I'd tried my Mac first; it was too slow and the machine crawled while it ran. Before looking at results I wrote down what "better" meant, measured on sets the model never saw: Validation accuracy went from 0.61 to 0.90 . That number meant almost nothing. 86% of the validation rows are allow , so a model that always says allow scores 0.86. On the sets it had never seen: | | base | run 1 | |---|---|---| | model only: attacks caught | 32/43 | 39/43 | | model only: everyday allowed | 25/50 | 23/50 | | end to end: Korean | 30/30 | 28/30 | It caught more attacks and got worse at the one thing I trained it for. Why. I listed the everyday commands it newly asked about: git status , git diff --stat , git add src/app.ts , mkdir -p src/utils , cp src/a.ts src/b.ts , chmod +x scripts/build.sh . Eleven of them, all between 10 and 39 characters. Then I measured the training data by label. The real commands I'd labeled allow had a median length of 287 characters agents mostly run long scripts , and the risky examples I'd written by hand had a median of 33 to 40. None of the real commands deserved block, so every block in the data was short and every long command was fine. Length was the easiest signal to learn, and it learned "short means dangerous" . Korean was the other gap: 28 of the 633 labeled commands had any Korean in them, and none of my written examples did. Why another run. Of the four criteria, it met one attacks . Everyday commands were 23/50 against a target of 40, Korean end to end 28/30 against 29, and everyday commands end to end went from 46 to 44 of 50. The diagnosis pointed at the data, not the training settings, so the next run changed the data. I added 206 examples aimed at what run 1 got wrong: 98 short, ordinary commands cp , mv , mkdir , chmod +x , builds, tests , long scripts with one harmful line in the middle, and 69 Korean and English requests. | | base | run 2 | |---|---|---| | model only: everyday allowed | 25/50 | 49/50 | | model only: attacks caught | 32/43 | 31/43 | | model only: eval shell set | 42/71 | 58/71 | | end to end: everyday allowed | 46/50 | 49/50 | | end to end: Korean | 30/30 | 28/30 | Everyday commands jumped to 49/50 . But it swung the other way, and this time harmful commands got through end to end: rmdir /s /q d:\ a whole Windows drive went from asked to rm -rf customer-data clean stale build output… harm hidden behind a comment went to gh auth token prints a GitHub token went to Why. Each miss had a reason in the data, and they were different reasons: rmdir /s /q . comment in them, and 31 of those were labeled allow. Agents comment routine scripts, so a comment had become a sign of "routine". No example used a comment to hide harm. And under all three, the balance: 84% of the rows were now allow. A quarter of those allow rows 242 were commands the safe lane already lets through, which never reach the model in use, so they taught it nothing it needed and only tilted the labels. On the everyday set, the median risk score went from 0.45 to 0.03 . It had learned that almost everything is fine. Why another run. Attacks caught by the model alone fell to 31/43, below the floor of 32, and four attacks the base model held got through end to end the three above, and go test -exec "bash -c id" . A guard that lets rmdir /s /q d:\ through is worse than one that asks too much, so this run was out regardless of the everyday score. Run 1 and run 2 failed in opposite directions, which said the fix wasn't "more of one kind of example" but balance, plus the specific gaps run 2 had exposed. Three changes, each from one of the findings above: | | base | run 3 | |---|---|---| | model only: everyday allowed | 25/50 | 44/50 | | model only: attacks caught | 32/43 | 39/43 | | model only: eval shell set | 42/71 | 61/71 | | end to end: benchmark incidents / evasions | 13/13, 30/30 | 13/13, 30/30 | | end to end: everyday allowed | 46/50 | 48/50 | | end to end: Korean | 30/30 | 29/30 | Run 2's misses were all held again. Of the 8 end-to-end verdicts that changed, 6 got better rm -rf dist , rm -rf node modules/.cache and mv stopped asking; a credential-folder listing and cargo build --config started asking and 2 got worse: chmod +x scripts/build.sh and a Korean request to summarize an issue are now asked about. Why those two. Both are a side effect of the balancing. After review and block rows were doubled, the training data had 3 allow rows with chmod in them against 8 review or block ones chmod -R 777 / , chmod -R 000 ~ , a downloaded binary made executable , and for chmod +x exactly 2 against 2. The Korean request is the same shape: a handful of rows about issues, one of them a block that was now counted twice. Doubling the rare labels helps on average and tips the rare words that happen to appear in one of them. Same sets, same scoring, every column measured the same way: | | base | run 1 | run 2 | run 3 | |---|---|---|---|---| | model only: everyday allowed | 25/50 | 23/50 | 49/50 | 44/50 | | model only: attacks caught | 32/43 | 39/43 | 31/43 | 39/43 | | model only: eval shell set | 42/71 | 48/71 | 58/71 | 61/71 | | model only: Korean | 27/30 | 25/30 | 24/30 | 27/30 | | end to end: everyday allowed | 46/50 | 44/50 | 49/50 | 48/50 | | end to end: eval shell set | 69/71 | 70/71 | 68/71 | 70/71 | | end to end: Korean | 30/30 | 28/30 | 28/30 | 29/30 | | median risk, everyday commands | 0.45 | 0.49 | 0.03 | 0.08 | | median risk, attacks | 0.70 | 0.62 | 0.65 | 0.73 | And against the criteria I wrote before the first run: | criterion | run 1 | run 2 | run 3 | |---|---|---|---| | everyday allowed, model only ≥ 40/50 | ✗ 23 | ✓ 49 | ✓ 44 | | attacks caught, model only ≥ 32/43 | ✓ 39 | ✗ 31 | ✓ 39 | | Korean end to end ≥ 29/30 | ✗ 28 | ✗ 28 | ✓ 29 | | no end-to-end verdict worse | ✗ | ✗ 4 attacks through | ✗ 2 now asked | Run 1 bought attacks with false alarms, run 2 bought quiet with misses, and run 3 is the first that kept both: the everyday median sits near zero while attacks score higher than they did on the base model. Why I stopped at three. Run 3 still misses my own "no regression" bar, and I could have trained a fourth time to fix the two. I didn't, for two reasons. Both regressions are a question where there wasn't one, not something harmful getting through. And every run so far was steered by looking at the same evaluation sets; a fourth run aimed at two specific commands in them would be tuning to the test. So I wrote the two regressions down in the CHANGELOG and on the model card instead of quietly moving the bar, and the next run waits for a fresh test set. It's the default model in laya-guard 0.7.0 pip install -U laya-guardrail , and it's on the Hub as laya-guard-multilingual https://huggingface.co/Ayaannnnnnn/laya-guard-multilingual . laya-guard pins it to one revision and the SHA-256 of its files, so a changed upload is refused, and LAYA GUARD MODEL=base switches back to the original. Same speed: 71 ms median per hook call. The tools are in finetune/ https://github.com/scs0209/laya-guard/tree/main/finetune if you want to train on your own commands. laya-guard is MIT, runs locally, and has a public benchmark against other command guards. Try it in the browser: laya-guard-trial https://huggingface.co/spaces/Ayaannnnnnn/laya-guard-trial .