Skip to content
JavaAgentic

Type at least two characters. Try “RAG”, “pgvector” or “tool calling”.

Fine-Tuning LLMs: LoRA & QLoRA

Understand fine-tuning for engineers: LoRA and QLoRA, instruction tuning, RLHF and DPO, and the crucial decision of when to fine-tune versus when retrieval or prompting is the better tool.

Advanced4 min readUpdated
On this page

Fine-tuning is powerful, frequently reached for too early, and often the wrong tool. This tutorial explains the techniques an engineer should understand — LoRA, QLoRA, alignment methods — and, most importantly, the decision of when to fine-tune versus when retrieval or prompting solves the problem better and cheaper.

Key Takeaways

  • Facts → retrieval. Behaviour → fine-tuning. Most needs are knowledge needs.
  • LoRA trains small adapters instead of all weights — cheap, swappable.
  • QLoRA adds quantization so you can fine-tune large models on modest hardware.
  • Most application teams never need to fine-tune — try prompting and RAG first.

The decision that matters most

Before any technique, the real question: should you fine-tune at all? The answer follows one rule.

The fine-tuning decision: knowledge needs retrieval; behaviour needs fine-tuning only if prompting fails.

Full fine-tuning vs parameter-efficient

Full fine-tuning updates every weight in the model. It is expensive — you need the compute and memory to train a model with billions of parameters — and produces a full-size model per task.

Parameter-efficient fine-tuning (PEFT) updates only a small number of new parameters, leaving the base model frozen. LoRA is the dominant PEFT method. For almost everyone, PEFT is the practical choice.

LoRA

LoRA (Low-Rank Adaptation) inserts small trainable matrices alongside the model's existing weights and trains only those. The base model is untouched. The result is a small adapter file — often a few megabytes against a multi-gigabyte model — that you load on top of the base at inference.

The benefits:

  • Cheap — far less compute and memory than full fine-tuning.
  • Swappable — different adapters for different tasks, on one base model.
  • Portable — the adapter is small and easy to store and version.

QLoRA

QLoRA adds quantization: the base model's weights are stored at lower precision (typically 4-bit) to slash memory use, while the LoRA adapters train at higher precision. This is what makes it possible to fine-tune a large model on a single consumer or modest cloud GPU, where full fine-tuning would need a cluster.

The trade-off is a small quality cost from running the base at reduced precision — usually acceptable, and a good deal for the memory saving. See model distillation & quantization for quantization in depth.

Instruction tuning and alignment

The models you use as assistants have been through more than base training:

  • Instruction tuning — training on examples of following instructions, turning a raw next-token predictor into something that responds to requests.
  • RLHF (Reinforcement Learning from Human Feedback) — training a reward model from human preference comparisons, then optimising the model against it. This is much of what makes a model helpful and harmless.
  • DPO (Direct Preference Optimization) — a simpler alternative to RLHF that optimises directly on preference pairs, achieving similar alignment with less machinery.

These are typically done by model providers, not application teams. You benefit from them; you rarely perform them.

When fine-tuning genuinely helps

Legitimate cases, once prompting has been tried and found wanting:

  • A consistent output format or style that prompting cannot reliably enforce at your volume.
  • A narrow, high-volume classification where a fine-tuned small model beats prompting a large one on both cost and accuracy.
  • A specialised domain language where the base model's register is persistently wrong.
  • Latency or cost — a fine-tuned small model can replace a prompted large one for a fixed task.

A practical path

  1. Prompt engineering first. Most problems dissolve here. Free, instant, no maintenance.
  2. RAG for anything knowledge-related. Updatable, citable, no training.
  3. Few-shot examples in the prompt for format consistency.
  4. Fine-tune only when the above genuinely fall short, and the case is behaviour, not facts.

Reaching step four should be the exception, backed by evidence that steps one to three were insufficient — not the default first move it is often treated as.

Next

Frequently Asked Questions

When should I fine-tune a model instead of using RAG?
Fine-tune to change behaviour — a consistent format, a specialised tone, a narrow classification skill. Use RAG to change knowledge — facts, policies, current data. The rule of thumb is: facts go in retrieval, behaviour goes in fine-tuning. Most production needs are knowledge needs, so most teams need RAG and never need fine-tuning.
What is LoRA?
Low-Rank Adaptation, a fine-tuning technique that trains small adapter matrices added to the model rather than updating all its weights. It makes fine-tuning far cheaper in compute and memory, and produces a small adapter file you can swap in, without touching the base model. It is the standard efficient fine-tuning method.
What is the difference between LoRA and QLoRA?
QLoRA is LoRA applied on top of a quantized base model — the base weights are stored at lower precision to save memory, while the LoRA adapters train in higher precision. It lets you fine-tune large models on modest hardware, at a small quality cost from the quantization. LoRA without the Q needs more memory but avoids that trade-off.
What are RLHF and DPO?
Both are methods for aligning a model with human preferences. RLHF (Reinforcement Learning from Human Feedback) trains a reward model from human comparisons and optimises against it. DPO (Direct Preference Optimization) achieves similar alignment more simply by optimising directly on preference pairs. They are how base models become the helpful assistants you interact with, and are usually beyond what an application team does themselves.

Related tutorials