Spreadsheet and terminal showing a supervised fine-tuning dataset being reviewed

SFT Playbook for LLM Apps: Data, Mixes, and Checks

Supervised fine-tuning is the unglamorous step that decides whether your tuned model is useful: you show the model prompt-and-answer pairs and it learns to produce answers like yours. This SFT playbook is written for people shipping LLM features, so it spends most of its words on the dataset and the checks around it, and very few on optimizer settings.

The reasoning is simple. In practice the trainer is the easy part; libraries such as the Hugging Face TRL SFT trainer give you a loop that works on day one. What separates a tuned model that helps from one that quietly hurts is the dataset design, the loss masking, the mix of old and new behavior, and the evidence you collect.

Fewer, better examples win more often than you expect

The LIMA paper (Zhou et al.) fine-tuned a large base model on only about a thousand carefully curated examples and got surprisingly strong instruction-following. The authors framed it as the superficial alignment hypothesis: a base model already has most of its knowledge, and SFT mostly teaches it which style of answer to produce.

For app builders that framing is useful and a little dangerous. Useful, because it says a few hundred excellent examples can fix format and tone. Dangerous, because it can tempt you into expecting SFT to inject facts. It will not do that reliably. Facts belong in retrieval; the decision guide for fine-tuning, RAG, and agents covers the split.

Design the dataset like an API contract

Treat each training row as a unit test for behavior you want. Before collecting anything, write one sentence per behavior: “Always returns valid JSON matching schema X,” “Declines to give legal advice and offers escalation,” “Answers in the customer’s language.” Every row should exercise at least one of those sentences.

A workable row has four parts:

  • The exact production prompt shape: same system message, same tool definitions, same retrieved-context wrapper if you use RAG.
  • A target completion that you would be happy to see verbatim in production.
  • Metadata such as source, annotator, date, and intended behavior tag, kept outside the training text but stored with the row.
  • A split label assigned once and never changed: train, validation, or held-out test.

Where do rows come from? Ranked by trustworthiness: expert-written answers, production responses that a human reviewed or edited, validated outputs from a stronger model, and finally raw model outputs with no review. The last category is the riskiest, and synthetic data for SFT explains the filters it needs.

Build a mix, not a pile

If you train only on your narrow task, the model may get better at that task while getting worse at nearly everything else. This is often called catastrophic forgetting. Research on LoRA suggests adapters forget less than full fine-tuning, but “less” is not “none.”

A sensible starting mix:

SliceShare of training rowsPurpose
Core task examples60-70%The behavior you are paying to add
Hard cases and edge inputs10-20%Empty inputs, contradictory context, adversarial phrasing
Refusal and escalation examples5-10%Teaches when not to answer
General replay data10-20%Keeps broad instruction-following from decaying

These shares are a starting point, not a law. The point is to decide the mix deliberately, record it, and test each slice separately afterward. Mixes with no replay slice are the first thing to inspect when a tuned model “got dumb” on unrelated prompts.

Masking, templates, and the bugs that look like model failures

Three mechanical details cause a surprising share of disappointing SFT runs.

Chat template mismatch. Every instruct model expects a specific layout of role tags and special tokens. Train with one layout and serve with another and the model sees a format it never learned. Always render training rows with the tokenizer’s own chat template, and render production prompts the same way.

Loss on the wrong tokens. If the loss covers the prompt as well as the answer, the model spends capacity learning to predict your system prompt. Recent TRL versions offer options to restrict loss to assistant tokens; check the documentation for the version you run and verify it by inspecting a decoded batch with the labels shown.

Missing end-of-turn token. If the stop token is absent from training targets, the model learns to keep going. The symptom is rambling answers or the model inventing the next user turn.

A thirty-second habit catches all three: print one fully rendered training example, decode the labels that count toward loss, and read them with your own eyes.

Hyperparameters in proportion

Optimizer settings matter, but less than the above. Defaults that rarely embarrass you with adapter-based SFT: a learning rate near 1e-4 to 2e-4 for LoRA adapters (far lower, around 1e-5 to 2e-5, for full fine-tuning), a cosine or linear decay schedule with brief warmup, two to three epochs for small sets, and an effective batch size reached through gradient accumulation. For adapter specifics see the LoRA fine-tuning guide and the memory-saving variant in QLoRA fine-tuning.

Do not tune hyperparameters until the dataset has passed review. A learning-rate sweep over a flawed dataset just finds the learning rate that memorizes the flaws fastest.

The checks that belong in every SFT run

Add these gates as code, not as good intentions.

  1. Dataset lint. Reject rows with empty targets, duplicated prompts, mismatched JSON, or token counts above your cap.
  2. Leakage check. Hash prompts and drop any that also appear in validation or held-out sets, including near-duplicates after normalization.
  3. Label audit. Sample 50 rows per behavior tag and have a second person grade them. If the reviewers disagree often, your spec is unclear, not your model.
  4. Overfit watch. Track validation loss and a task metric. If the metric stops rising while training loss keeps falling, stop.
  5. Before/after report. Score baseline and tuned model on the same held-out set, with slices. The method is in eval before and after fine-tuning.
  6. Regression suite. Run a small set of general prompts you must not break and compare outputs across versions.

Treat the resulting numbers as release evidence: store them with the adapter, and tie them into the same release process described in the LLMOps production guide.

When SFT is the wrong tool

SFT teaches imitation. If your examples are inconsistent, the model learns inconsistency. If “good” is a matter of ranking two outputs rather than writing one perfect answer, preference tuning may fit better, and DPO explained for builders shows where that starts to pay off. If the model fails because the retriever returned the wrong passage, no amount of SFT repairs that, and the RAGAS evaluation guide is the better starting point. For the official method reference, the PEFT documentation covers adapter configuration end to end.

A small first project that teaches the loop

If this is your first SFT run, pick a task with an objective check: converting messy support notes into a fixed JSON ticket. Write 300 examples by hand or from reviewed production data, hold out 60 for testing, and run the baseline with your best prompt first. The tuned model should improve valid-JSON rate and field accuracy measurably; if it does not, you learned the problem is elsewhere before you spent a week on infrastructure.

FAQ: SFT for LLM apps

What does SFT stand for and what does it do?

SFT means supervised fine-tuning: training a model on prompt-and-response pairs so it imitates the responses. It is best at teaching format, tone, and task habits, and weaker at adding reliable new facts.

How many examples do I need for SFT?

Narrow, well-defined behavior can improve with a few hundred clean examples. Broader style or domain shifts typically need thousands. Quality and consistency matter more than raw count, and a held-out test set is non-negotiable.

Should I include the system prompt in training data?

Yes, if production uses one. Training rows should match the exact prompt shape at inference, including system message, tool definitions, and context wrappers. Mismatch between training and serving formats is a common cause of weak results.

How do I prevent the model from forgetting general skills?

Mix in replay data from general instruction sets, keep adapters small, avoid too many epochs, and run a regression suite of general prompts after each run. Compare outputs across versions rather than trusting a single metric.

Is SFT enough or do I also need preference tuning?

SFT is enough for many apps, especially format and style problems. Add preference tuning only when you can collect reliable chosen-versus-rejected pairs and SFT has plateaued on quality dimensions that ranking can capture.

Can I train on my production logs?

Often yes, after review. Filter out private data you are not permitted to use, remove failed or low-quality responses, deduplicate, and prefer logs where a human accepted or corrected the output. Check your privacy commitments and applicable policies first.

Related reading

About Mudasir Syed

Check Also

Network operations room with a world map showing data center regions for cloud AI services

Data Residency for LLM Apps: Regions, Vendors, and Contracts

Your security reviewer asks a deceptively short question: “Where does the customer’s data go when …

Leave a Reply