Skip to content
mentoor

Fine-tune with LoRA, then prove it helped

For engineers

Data, LoRA, preference tuning, distillation and the eval set that shows the gain.

The one-sentence idea: a fine-tune only counts as a win when an eval set built before training, scored with a metric that matches the business decision, shows the tuned model beating the untuned one on the cases that matter.

The case

Tidewater Parcel gets about 1,200 support tickets a day. A frontier model API reads each ticket and returns JSON: a category, an urgency and a needs_human flag. It works, but it takes two seconds per ticket and costs too much at this volume. Ravi, an engineer, is asked to move the job to a small open-weight model of about 8 billion parameters, tuned on Tidewater's own tickets, and to show it is good enough. One rule comes from support: do not miss urgent tickets, such as a melted fridge pack holding insulin.

The plan has six steps. The order matters, because each step protects the next one.

Step by step

1. Build the eval set first. Pull 400 real, redacted tickets from the logs. Label them with agents and a written label guide, cover each category, and add slices for urgent, non-English and odd inputs. Freeze the set and record its hash. Never add cases because the tuned model got them wrong. Keep those in a separate regression list.

2. Prepare training data. Convert tickets to chat rows where the assistant turn is exactly the JSON the code parses, with loss on the assistant tokens only. Remove personal data and secrets from every split. Dedupe, and fix rows that carry conflicting labels. A few thousand clean rows beat a large noisy set.

3. Distil from a teacher. Unlabelled tickets can be labelled by the frontier model, which is output distillation: the student learns from the teacher's written answers. Filter the labels and spot-check a sample. Check the provider's terms before you train on its outputs. The teacher's mistakes become the student's lessons, so never use teacher labels as the eval answer key.

4. Run LoRA or QLoRA. On one 24 GB GPU, a 4-bit base with an adapter fits. Pick the rank and target modules, set the learning rate and watch eval loss each epoch. Keep the checkpoint where eval loss is lowest, not the last one.

5. Tune behaviour with preferences, only if needed. If replies sound curt, collect chosen and rejected pairs for the same ticket and run DPO. Check the pairs for hidden shortcuts such as length, and write any AI labeller's rubric so that it rewards solving the problem, not apologising the most.

6. Prove it. Score three models on the same frozen set: the untuned base, the tuned student and, for context, the teacher. Report urgent recall and precision, not accuracy alone, and add an interval, because 60 urgent cases move easily. Choose the threshold from the precision and recall trade-off and the business limits. Check that probabilities are calibrated before you use them for routing.

Before and after

A weak report says: "Accuracy went from 91% to 97%. Ship." Six percent of tickets are urgent, so a rule that never flags anything already scores 94%. A strong report says: "Urgent recall rose from 0.61 to 0.88 on 400 frozen real tickets, with the interval shown, at 0.4 s and a fraction of the teacher's cost, and general checks did not regress."

What goes wrong

  • Leaks. Scoring on training rows, or adding failed cases to the eval set, makes the score meaningless.
  • Wrong yardstick. Accuracy on imbalanced data, F1 when the business set a recall floor, or agreement with the teacher instead of the truth.
  • Overfitting. Training loss falls while eval loss rises, and the final checkpoint is worse than an earlier one.
  • Thin evidence. One run on a few dozen positives, reported as a significant gain.
  • Data risk. Personal data and secrets in training rows can come back out of the tuned model.

A tuned model also needs a regression check on the abilities you did not train, since an adapter can shift behaviour outside the task.

Steps

  1. Freeze a real eval set before any training

    Skill: evals-datasets

    Ravi pulls 400 redacted tickets from the last 90 days, labels them with agents and a written guide, and makes sure urgent, non-English and odd inputs each have enough cases to score alone. He freezes the file and records its hash. Cases the tuned model fails later go to a separate regression list, never into this set.

    Frozen eval set v1
    400 cases, labelled by agents.
    SliceCasesSource
    Where is my parcel140Logs, last 90 days
    Address change and billing110Logs, last 90 days
    Damaged parcel60Logs, last 90 days
    Perishable or medical urgent40Logs and agent escalations
    Non-English50Logs, last 90 days
  2. Clean the training rows

    Skill: train-sft-data

    Each row is a chat with the system instruction, the ticket and an assistant turn holding exactly the JSON the code parses. Personal data and secrets are replaced with placeholders in every split. Duplicates with conflicting labels are settled with the label guide, and loss is masked to the assistant tokens.

    One cleaned training row

    data/train_rows.jsonl

    {"messages": [  {"role": "system", "content": "Return JSON only: category, urgency, needs_human."},  {"role": "user", "content": "Ticket: Parcel [ORDER] arrived with the fridge pack melted. It holds my insulin."},  {"role": "assistant", "content": "{\"category\": \"damaged\", \"urgency\": \"urgent\", \"needs_human\": true}"}]}
  3. Label extra tickets with a teacher and filter

    Skill: train-distill

    Unlabelled tickets are labelled by the frontier model, then deduplicated and spot-checked by an agent. Tidewater confirms the provider's terms allow training on these outputs. The student learns from teacher text, and the eval set keeps its agent labels so it can still show where the teacher and student are wrong.

  4. Run QLoRA and keep the best checkpoint

    Skill: train-lora

    A 4-bit 8B base with a rank-16 adapter on the attention projections fits on one 24 GB GPU. Eval loss is logged each epoch. It bottoms out at epoch 2, so Ravi keeps that checkpoint, then runs a small general-ability regression check as well as the triage eval.

    Run log

    runs/triage-qlora-02/log.txt

    epoch  train_loss  eval_loss1      1.18        1.042      0.69        0.853      0.36        0.91best checkpoint: epoch 2
  5. Fix curt replies with preference pairs

    Skill: train-preference

    Support finds the drafted replies curt. Agents rank two replies to the same ticket, and Ravi checks that the chosen replies are not simply longer. A DPO pass on balanced pairs then nudges the tone, with the same eval rerun afterwards to confirm triage did not slip.

  6. Prove the gain on the frozen set

    Skill: ml-metrics

    The base model, the tuned student and the teacher are scored on the same 400 cases. Ravi reports urgent recall and precision with an interval, picks the threshold from the recall floor, and checks that probabilities are calibrated before auto-escalating anything.

    Frozen eval results
    Urgent class, 40 urgent cases among 400.
    ModelUrgent recallUrgent precisionLatency
    Base 8B, untuned0.610.700.4 s
    Tuned student0.880.790.4 s
    Teacher API0.910.832.1 s

Skills this scenario uses

Where to study