Fine-Tune a Self-Improving Local Coding Agent
A five-module ladder from your first LoRA to an agent that trains on its own verified successes. Apple Silicon primary, Jetson Thor for scale.
TUTOR WITH THEFOCUS.AI
Copy this prompt into Claude, ChatGPT, or any external AI assistant. It points the assistant to the course instructions and links it to your student profile to track your progress and customize observations.
You are not enrolled yet. Enroll to generate a Student ID to track lesson completions and store learning notes.
Fine-Tune a Self-Improving Local Coding Agent
A five-module ladder, Apple Silicon primary, Jetson Thor for scale.
5 parts · 16 chapters · Will Schenk
The Constraints (fixed)
- The model is fine-tuned. Not just prompted, not just retrieved-into.
- The model is local. Weights on disk, inference on your hardware.
Everything in this course serves those two. Retrieval, tool design, and context management appear only where they generate training data or gate a deliverable.
What You’ll Build
By the end of this course you’ll have:
- Calibrated intuition for LoRA hyperparameters — rank, alpha, steps, dataset size — built where you can see the failure modes
- A repeatable
data → train → fuse → servepipeline that runs in minutes - An agent harness that logs every trajectory in a trainable format
- A fine-tuned small model that emits valid tool calls where the base model can’t
- A verifier: a binary, mechanical, uncheatable pass/fail scoreboard on real coding tasks
- A flywheel: an agent that attempts tasks, keeps only verified successes, retrains on them, and scores higher each round — with no teacher in the loop
Hardware Split
| Box | Role | Toolchain |
|---|---|---|
| M4 MacBook, 64GB | Iteration, dataset work, all of Parts 1–4 | MLX (mlx_lm.lora, mflux) |
| Jetson AGX Thor, 128GB | Scale runs, Nemotron, final serving | Unsloth / CUDA, Ollama |
Model choice on the Mac: Gemma 3. Nemotron’s hybrid Mamba2-Transformer architecture is still maturing in mlx-lm — it runs for inference but fights you for training. Save it for the Thor. Gemma 3’s ladder (270M → 1B → 4B → 12B → 27B) is exactly the ramp this course wants.
The Spine
Each module produces an artifact the next one consumes. Nothing is decorative.
1. mflux LoRA → intuition about the knobs
2. Gemma 1B toy tune → a working train→fuse→serve pipeline
3. Trajectory logger → the training data format
4. Verifier → an objective scoreboard
5. Flywheel → agent trains on its own verified successes
The verifier from Part 4 does double duty: it measures whether tuning helped, and it filters the data in Part 5. That’s why it isn’t optional.
Course Structure
| Part | Title | Chapters | What You Build |
|---|---|---|---|
| Part 1 | LoRA With Your Eyes Open | 01–03 | An image LoRA and a contact sheet of every hyperparameter failure mode |
| Part 2 | The Pipeline, Proven | 04–06 | A data → train → fuse → serve loop on Gemma 3 1B |
| Part 3 | Trajectories | 07–09 | A single-tool agent, a trajectory logger, and your first real fine-tune |
| Part 4 | The Verifier | 10–12 | Six sandboxed tools, a held-out eval set, and a baseline table |
| Part 5 | The Flywheel | 13–16 | Rejection sampling → DPO → GRPO → scale up and ship |
Time Budget
| Part | Calendar | Where the time actually goes |
|---|---|---|
| 1 | 1 day | Waiting on training. Read the mflux source meanwhile. |
| 2 | 1 day | Building the Makefile. Worth it. |
| 3 | 2–3 days | Harness design + teacher trace generation |
| 4 | 3–5 days | Writing eval tasks. Nothing else. This is the bottleneck and it’s unavoidable. |
| 5 | Ongoing | Compute, mostly unattended |
Parts 1–2 are a weekend. Part 4 is where the real work is. Part 5 is where the interesting part starts.
The Three Things That Break Projects Like This
1. Skipping the verifier. Without it you’re tuning on vibes and cannot distinguish improvement from noise. It’s also the filter in Part 5 — no verifier, no flywheel. Build it before you need it.
2. Loss masking. Train on tool outputs and the model learns to hallucinate observations rather than fetch them. It will look fine on loss and be useless in the loop. Check it in Part 2, verify it again in Part 3.
3. Trying to fine-tune knowledge into weights. Codebase facts belong in context. Behavior belongs in weights. Confusing these produces a model that confidently describes an API you deleted last month. The fine-tune is teaching the loop — research, tool use, reporting — and the loop is what small models are actually missing.
Let’s Go
Part 1: LoRA With Your Eyes Open →
01 lora eyes open
LoRA's knobs are identical in image and text training — but in images, you can see when it goes wrong. Set up mflux and read the source.
Build a 15–20 image Dreambooth dataset with a trigger token and run your first mflux training from a JSON config.
Sweep rank, steps, dataset size, target modules, and adapter scale. Build the contact sheet — the artifact you'll refer back to all course.
02 the pipeline
Set up mlx-lm and generate a chat-format JSONL dataset for a behavior the base model definitively does not have.
Run mlx_lm.lora, then pass the three checks: overfit sanity, loss masking, and adapter vs. merged. The loss masking check is the highest-leverage lesson in the course.
Serve the tuned model over the OpenAI-compatible API and wrap the entire loop in a Makefile you'll use for the rest of the course.
03 trajectories
Two hundred lines, one tool. Build the smallest possible agent loop and understand why behavior — not knowledge — is what you're training.
Wrap the harness so every run emits one JSONL trajectory. The schema decisions here — outcome field, masking, logging failures — are load-bearing for Parts 4 and 5.
Run the harness against Claude to generate distillation data, tune the 1B, and measure the metric that matters: tool-call validity rate.
04 the verifier
Expand from one tool to six — sandboxed — and understand why every additional tool multiplies the protocol surface your small model has to learn.
Write 20–50 held-out tasks — research tasks and executable tasks — and discover the infinite ground-truth task generator hiding in your git history.
Build eval.py, score every model you can run, and face the uncomfortable possibility the table exists to reveal.
05 the flywheel
The simplest self-improvement method that works: sample k rollouts per task, keep the verified winners, train on them, redeploy, score.
Rejection sampling throws away every failure. Recover that signal with preference pairs, then graduate to group-relative policy optimization.
Reflexion-style notes and skill accretion — learning mechanisms that run in parallel with training and cost zero GPU.
Move the base model up the Gemma ladder, rebuild the tune on the Jetson Thor under Unsloth/CUDA, and serve via Ollama. Same harness, one URL changed.