
Fine-Tuning LLMs
A Practical Python Guide to Customizing Open Models
By Shane Larson
About This Book
The training run finished at 2 a.m. Loss curve looked beautiful — smooth, monotonic, textbook. The engineer ran a couple of prompts, saw the model answer in the exact house format they'd trained for, and shipped it to a staging endpoint feeling like a wizard. The next morning someone in QA asked it to add two order totals. It confidently returned a number that was off by eleven dollars. Then it did it again, differently, on a second try. Somewhere in eight hours of fine-tuning, a model that could do grade-school arithmetic had quietly forgotten how.
That failure mode has a name — catastrophic forgetting — and it is not exotic. It is the ordinary tax you pay for pointing a large model at a small, narrow dataset without understanding what you're doing. The frustrating part is that the loss curve never warned anyone. It went down the whole time. The model got better at the thing it was measured on and worse at everything else, and nothing in the standard tutorial pipeline surfaced the trade until it hit a human in production.
This book is about not being that engineer. It's a start-to-finish Python guide to fine-tuning open-weight language models, written for people who have a real problem, a rented GPU, and a low tolerance for magic they can't explain.
The Argument
There are three ways to make a general-purpose model behave the way you need: write a better prompt, retrieve the right context at inference time, or change the weights. Most of the noise online treats these as tribes to belong to. They're not. They're tools with different costs, and choosing wrong is expensive in both directions — people fine-tune problems that a paragraph of prompt would have solved, and people bolt three retrieval systems onto a task whose real issue was that the model never learned their format.
So the book opens where the tutorials don't: with an honest decision framework. Before a single GPU-hour is spent, you work through whether fine-tuning is even the right move — and the framework is built to talk you out of it as readily as into it. When it is the right move, the rest of the book is the how: picking a base model and actually reading its license, constructing and cleaning a dataset, understanding LoRA and QLoRA well enough to set their knobs deliberately, running instruction tuning with the chat formatting correct, and measuring whether the result is genuinely better or just better at fooling you.
One project runs through every chapter so nothing stays abstract. You build a customer-support model for a fictional company called Meridian Tools, and you take it the whole distance — from "should we even do this?" through dataset design, training on a single consumer-grade GPU, evaluation, quantization, and deployment behind an API. Real memory numbers. Real cost math. No cluster, no research lab, no employer-scale budget assumed anywhere.
What's Inside
- The decision first. A concrete fine-tune vs. RAG vs. prompt framework with criteria you can actually apply to your own problem — including the cases where the correct answer is "close the laptop and rewrite the prompt."
- Datasets get three chapters, because datasets are the job. Sourcing examples, cleaning them, formatting them for instruction tuning, and splitting them without quietly leaking your test set into training — the unglamorous 80% that decides whether any of the rest matters.
- LoRA and QLoRA explained before they're applied. Enough of the mechanism that you configure adapters and quantization with intent instead of pasting someone else's hyperparameters and hoping.
- Chat templates and loss masking, the silent formatting errors that ruin a fine-tune without ever raising an exception — and how to catch them before you waste a run.
- A full evaluation chapter — the one most guides skip entirely. Before-and-after comparison, LLM-as-judge with its own failure modes, and regression tests so a fix in one place doesn't break three others.
- Failure modes taught as curriculum, not footnotes: catastrophic forgetting, overfitting to a tiny set, chat-template mismatch, and the benchmark self-deception that makes a broken model look fixed.
- The whole thing on one rented GPU, with the actual VRAM figures and dollar estimates for an 8B-class open instruct model under QLoRA, so budget stops being a mystery.
- Shipping, honestly — quantizing for serving, standing up an API, and the licensing, provenance, and safety checks that separate a demo from something you'd let a customer touch.
Why I Wrote This
I've spent most of my career as a solutions and enterprise architect, which mostly means I've watched good teams burn weeks on the wrong tool because nobody made them justify the choice up front. Fine-tuning attracts exactly that mistake. It's the impressive-sounding option, so it becomes the default, and people reach for it before they've asked whether the problem actually needs new weights.
I wanted the book I couldn't find when I started: one that treats the decision to fine-tune as seriously as the mechanics, spends real pages on the dataset work everyone skips, and refuses to declare victory without measuring. I also wanted every example to run on hardware a normal person can rent by the hour, because "it works on our internal cluster" helps no one. This is the practitioner's version — what I'd tell an engineer sitting next to me who had a real problem and a weekend.
Frequently Asked Questions
Do I need to read The Fundamentals of Training an LLM first?
No, but it's the natural on-ramp. That book covers how training works from the ground up in Python and PyTorch; this one assumes you understand the concept and want to customize an existing open model in practice. If you're comfortable with the idea of gradients and a training loop, you can start here.
Does this assume I know the underlying ML math?
No. You need working Python and general software fluency. The book explains LoRA, quantization, and evaluation in operational terms — what the settings do and how to choose them — without requiring you to derive anything. There's no linear algebra homework.
What libraries and stack does it use?
Python with the Hugging Face ecosystem — transformers, PEFT, TRL, and bitsandbytes — doing QLoRA on an 8B-class open instruct model. The code is meant to be run, adapted, and reused, not just read.
What hardware do I actually need?
A single rented consumer-grade GPU. The book gives real VRAM requirements and per-hour cost estimates rather than hand-waving, and the entire worked project is scoped to fit on that budget. No multi-GPU setup, no data-center access.
Is this really just about one toy example?
The Meridian Tools support model is the through-line, but every technique generalizes. The point of threading one project end to end is that you see how the decisions connect — dataset choices constrain evaluation, evaluation exposes failure modes — instead of collecting disconnected snippets.
When does the book tell me not to fine-tune?
Often, and early. The opening framework exists specifically to catch the cases where retrieval or a better prompt wins on cost and maintenance. Knowing when to walk away is treated as a skill, not a disclaimer.
If You Liked This, You Might Like
- The Fundamentals of Training an LLM — The prequel: how training works from first principles in Python and PyTorch, before you specialize a model with the techniques in this book.
- Build Your Own AI Agent From Scratch — Once your fine-tuned model exists, this is how you give it tools, memory, and reasoning as part of a working system.
- Retrieval Augmented Generation with Node.js — The other side of the decision framework, built out in full: when retrieval is the right answer instead of new weights.
- Building a Multi-Agent Orchestrator in Node.js — Where a customized model becomes one specialist among several coordinated by an orchestration layer.
A fine-tuned model is only worth the GPU time if you can prove it's better and trust it in front of a user. This is the path from that first uncertain decision to a model you'd actually deploy.
Included with Kindle Unlimited — read it free if you're a member.
New AI & Technology releases and free chapters — no spam, unsubscribe anytime.



