AWS-113 · Lesson 8 of 12 · 19 min

Customizing models: fine-tuning and continued pre-training

▶ Video coming soon A video version of this lesson is on the way.

A vendor demonstration had left Lorraine Beaudry, the CFO, with a question she brought to the eighth AI hour. "The salesperson said we should train our own model on all our records so it really understands veterinary work," she said. "Is that what we should be doing?"

Noor drew a ladder on the whiteboard. On the bottom rung she wrote PROMPT. Then RAG, FINE-TUNE, CONTINUED PRE-TRAINING and, at the top, PRE-TRAIN FROM SCRATCH. "Each rung costs more, takes longer and needs more data and skill than the one below," she said. "We climb only when the rung below has been tried and measured and is not good enough. So far, we have not needed to leave the second rung."

This lesson explains the ways a foundation model can be customized, what each changes, how to prepare data for them, and the cost trade-offs the exam guide asks about. It also explains why most business applications, including both of Cascade Ridge's, never need to climb very high.

Ladder diagram with five rungs from bottom to top: prompt engineering and in-context learning, retrieval-augmented generation, fine-tuning, continued pre-training, and pre-training from scratch. An arrow beside the ladder shows cost, time, data and skill increasing upward. Each rung notes what changes: the prompt, the context, the model weights for a task, the model weights for a domain, or a whole new model.
The customization ladder. Climb only when the lower rung has been tried and measured.

What each approach changes

The approaches differ in one fundamental way: whether they change the model's weights, the billions of numbers learned in training.

  • In-context learning (prompt engineering, including few-shot examples) changes nothing in the model. All the guidance lives in the prompt. It is quick, cheap to try and easy to change, but every request pays for the extra tokens.
  • Retrieval-augmented generation also changes nothing in the model. It adds retrieved knowledge to the prompt, which suits facts that change and must be cited.
  • Fine-tuning continues training an existing foundation model on a smaller, labelled dataset of examples, changing its weights so it gets better at a specific task, style or format.
  • Continued pre-training (sometimes called continuous pre-training) continues the model's original kind of training on a large amount of unlabelled text from a particular domain, so it absorbs that domain's vocabulary and patterns.
  • Pre-training from scratch builds a new foundation model from random weights on an enormous dataset. It costs the most by far and is done mainly by model providers and very large organizations.
  • Distillation trains a smaller "student" model to imitate the outputs of a larger "teacher" model on a task, producing a model that is cheaper and faster to run while keeping much of the teacher's quality for that task.

Fine-tuning

Fine-tuning is the customization most teams consider first once prompting is not enough. The exam guide names several forms of it.

  • Instruction tuning fine-tunes a model on examples of instructions paired with good responses, so it follows a particular kind of instruction better. Many base models are instruction-tuned by their providers before release; a business might instruction-tune further for its own tasks.
  • Domain adaptation fine-tunes a model on examples from a specialized field, such as veterinary medicine or law, so it handles that field's terms and conventions better.
  • Transfer learning is the general idea behind all of these: knowledge learned on one broad task is transferred and adapted to a new, narrower one, so far less data is needed than training from scratch.

A fine-tuning dataset is usually a set of prompt and completion pairs, often stored as JSON Lines, one example per line. For a hypothetical Night Notes fine-tune, each example would pair an invented call note with the summary a veterinarian had approved.

Python Example
import json

examples = [
    {"prompt": "Lab, 4 yrs, 30 kg. Ate 1 pk sugar-free gum ~30 min ago. Drooling, wobbly. Owner Priya.",
     "completion": "Patient: dog, Labrador, 4 years, 30 kg | Concern: ate one packet of sugar-free gum; now drooling and unsteady | Timing: about 30 minutes before the call | Actions taken by owner: none mentioned | Advice given: unclear"},
    {"prompt": "Cat 12y, not eating 2 days, hiding. No toxins known. Owner Marc.",
     "completion": "Patient: cat, 12 years | Concern: not eating for two days and hiding; no toxins known | Timing: started two days before the call | Actions taken by owner: none mentioned | Advice given: unclear"},
]
lines = [json.dumps(e) for e in examples]
print("\n".join(lines))
words = sum(len((e["prompt"] + " " + e["completion"]).split()) for e in examples)
print("Examples:", len(examples), " Total words:", words)
Output
{"prompt": "Lab, 4 yrs, 30 kg. Ate 1 pk sugar-free gum ~30 min ago. Drooling, wobbly. Owner Priya.", "completion": "Patient: dog, Labrador, 4 years, 30 kg | Concern: ate one packet of sugar-free gum; now drooling and unsteady | Timing: about 30 minutes before the call | Actions taken by owner: none mentioned | Advice given: unclear"}
{"prompt": "Cat 12y, not eating 2 days, hiding. No toxins known. Owner Marc.", "completion": "Patient: cat, 12 years | Concern: not eating for two days and hiding; no toxins known | Timing: started two days before the call | Actions taken by owner: none mentioned | Advice given: unclear"}
Examples: 2  Total words: 102

What this code does

It writes two example records for fine-tuning in JSON Lines format, one JSON object per line, each with a prompt (a raw call note) and the completion the model should learn to produce (the formatted summary). It prints the lines and counts the examples and total words. Real fine-tuning data uses the same shape, just with many more carefully reviewed examples.

On AWS, fine-tuning can be done in Amazon Bedrock for models that support it, by pointing a customization job at training and validation data in Amazon S3, or in Amazon SageMaker AI, which gives full control over the training process and hosting. A customized model on Bedrock is a separate model resource that the application calls instead of the base model, and depending on the model it may need purchased capacity, such as Provisioned Throughput, to serve requests. That ongoing hosting cost is easy to overlook when comparing options.

Continued pre-training

Continued pre-training uses unlabelled text: no prompts and answers, just a large volume of documents from a domain. A provider of veterinary software might continue pre-training a model on many thousands of textbooks, journal articles and anonymized records so the model learns the field's language deeply. The cost is high because the volume is large, and the result still usually needs instruction tuning or good prompting to follow instructions well.

For Cascade Ridge, continued pre-training made no sense. Its own text, 140 SOPs and some years of notes, was far too small to shift a large model's knowledge, and much of it contained personal information that Hannah would not allow into training without a full privacy review and de-identification.

Preparing data for fine-tuning

The exam guide lists the qualities that matter when preparing data to fine-tune a model. Each one is a place where projects go wrong.

  • Curation. Choose examples that are correct and representative of the task, and remove duplicates, errors and examples that teach the wrong behaviour. A few hundred excellent examples can beat thousands of careless ones.
  • Governance. Know where every example came from, whether you have the right to use it, and whether it contains personal or confidential information. Record who approved the dataset, and protect it with the same access controls as the source data.
  • Size. Enough examples to cover the variety of real inputs. The right number depends on the task and model; providers publish guidance, and teams usually start small and measure.
  • Labelling. Each example needs the right output, usually written or checked by experts. For Night Notes, that would mean veterinarians approving each summary, which is slow and expensive.
  • Representativeness. The examples should reflect the real mix of inputs: cats and dogs, short and long notes, typos, different clinics. A dataset of only easy cases produces a model that fails on hard ones. Representativeness also matters for fairness, which AWS-114 covers.

Reinforcement learning from human feedback (RLHF) is a related technique used mostly by model providers. People compare or rate model responses, those preferences train a reward model, and the foundation model is then tuned to produce responses people prefer, such as ones that are more helpful and less harmful. It is one reason modern models follow instructions and decline harmful requests as well as they do.

Cost trade-offs

The exam guide asks you to explain the cost trade-offs between pre-training, fine-tuning, in-context learning, RAG and distillation. Lorraine wanted the comparison in numbers, so Jonah built a rough model for Night Notes at 3,000 summaries a month. Every figure below is illustrative, chosen to show the shape of the trade-off rather than real prices.

Python Example
# Illustrative monthly cost comparison for 3,000 Night Notes summaries (all figures invented).
requests = 3000
options = {
    # name: (one-time setup cost, monthly fixed cost, cost per request)
    "Prompt with 1 example":      (0,     0,    0.0040),
    "Prompt with 5 examples":     (0,     0,    0.0075),
    "Fine-tuned model":           (2500,  1800, 0.0030),
    "Distilled smaller model":    (1200,  900,  0.0012),
}
for name, (setup, fixed, per) in options.items():
    monthly = fixed + per * requests
    year = setup + 12 * monthly
    print(f"{name:26} monthly {monthly:8.2f}  first year {year:9.2f}  (illustrative USD)")
Output
Prompt with 1 example      monthly    12.00  first year    144.00  (illustrative USD)
Prompt with 5 examples     monthly    22.50  first year    270.00  (illustrative USD)
Fine-tuned model           monthly  1809.00  first year  24208.00  (illustrative USD)
Distilled smaller model    monthly   903.60  first year  12043.20  (illustrative USD)

What this code does

Four ways of building a summary feature are compared using made-up costs: two prompting options and two customized models, each with a setup cost, a monthly fixed cost and a cost per request. For 3,000 requests a month it works out the monthly and first-year cost of each. The output shows that at this volume the simple prompts are far cheaper than training a custom model.

At this volume, the simple prompt was cheapest by a wide margin. The fine-tuned model's per-request cost was lower, but its fixed hosting cost swamped the saving at 3,000 requests a month. The picture would change at much higher volumes, which is exactly why the trade-off depends on the workload. Noor summarized the pattern on the whiteboard:

Customization trade-offs. Real costs depend on the model, data and volume.
ApproachUpfront cost and effortOngoing costBest for
In-context learningLowestExtra tokens on every requestQuick improvements, changing requirements
RAGLow to moderate (ingestion, vector store)Vector store plus retrieved tokensCurrent, private facts with citations
Fine-tuningModerate to high (data, labelling, training)Often hosting capacity for the custom modelConsistent style, format or specialized task behaviour
DistillationModerate (teacher outputs, training)Lower per request than the teacherHigh volume where a smaller model can match quality
Continued pre-trainingHigh (large unlabelled corpus)Hosting the custom modelDeep domain language at large scale
Pre-training from scratchHighest by farHosting and maintenanceModel providers and very large organizations

Fine-tuning and RAG solve different problems, and confusing them is the most common mistake. Fine-tuning teaches behaviour: a format, a tone, a way of handling a task. RAG supplies knowledge: facts that change and must be cited. A fine-tuned model does not reliably memorize facts, cannot cite them and must be retrained when they change. Many strong applications combine both: a model fine-tuned for format, answering from retrieved passages.

Common mistakes

  • MistakeFine-tuning a model to teach it facts that change often.

    InsteadUse RAG for changing facts; fine-tune for behaviour such as format and style.

  • MistakeClimbing to fine-tuning before measuring prompting and RAG.

    InsteadTry and measure the cheaper rungs first, and customize only when they fall short.

  • MistakeIgnoring the hosting cost of a custom model.

    InsteadInclude ongoing capacity for the customized model in the comparison, not just training.

  • MistakeTraining on personal data without review.

    InsteadApply data governance: rights, privacy review, de-identification and access controls.

Importing and hosting customized models

Not every customized model is trained on Bedrock. Some teams fine-tune an open-weight model in SageMaker AI or elsewhere and then bring it into Bedrock, where a model import feature lets supported model types be served through the same API as other Bedrock models. Others keep the customized model on a SageMaker AI endpoint. Either way, the team becomes responsible for evaluating the customized model as carefully as any new model, and for repeating that evaluation whenever it is retrained. A customized model is a new model, with its own strengths and failure modes.

Worked problem

Answering the vendor

Lorraine asks whether Cascade Ridge should pre-train its own model on all its records. Using the ladder, what is the answer?

Show solution
  1. Pre-training from scratch needs enormous data and budget; Cascade Ridge has neither, and model providers already offer capable foundation models.
  2. Continued pre-training needs a large unlabelled domain corpus; the company's text is small and full of personal information.
  3. Fine-tuning might later help Night Notes follow its format, but prompting already meets the format on 39 of 40 test notes.
  4. Front Desk Helper needs current policy facts with sources, which RAG provides and fine-tuning does not.

Answer: No. Stay on the prompting and RAG rungs, keep measuring, and consider fine-tuning or distillation only if evaluation shows a gap that cheaper methods cannot close.

Lorraine wrote one word on her notepad, LADDER, and underlined it. "I'll use that the next time a vendor calls," she said.

Summary and key terms

  • Foundation models can be customized in increasing order of cost and effort: in-context learning and RAG, which change only the prompt; fine-tuning, which changes the weights using labelled examples for a task, including instruction tuning, domain adaptation and transfer learning; continued pre-training on large unlabelled domain text; and pre-training from scratch.
  • Distillation trains a smaller student model to imitate a larger teacher, cutting run costs.
  • Data for fine-tuning needs curation, governance, enough size, accurate labels and representativeness; RLHF tunes models toward responses people prefer.
  • Customized models on Bedrock may need purchased capacity such as Provisioned Throughput.
  • Fine-tuning teaches behaviour; RAG supplies changing knowledge with citations.
  • Try cheaper rungs first and measure.
Weights
The learned numbers inside a model that training adjusts.
In-context learning
Guiding a model with instructions and examples in the prompt, without changing its weights.
Fine-tuning
Further training a foundation model on labelled examples for a specific task.
Instruction tuning
Fine-tuning on instruction and response pairs to improve instruction following.
Continued pre-training
Further pre-training on unlabelled domain text to absorb its language.
Transfer learning
Adapting knowledge learned on one task to a related task.
Distillation
Training a smaller student model to imitate a larger teacher model.
RLHF
Reinforcement learning from human feedback: tuning a model toward responses people prefer.

Practice problems

Work each problem on paper first, then reveal the answer to check.

  1. Which customization uses unlabelled domain text?

    Show answer

    Continued pre-training.

  2. Should you fine-tune a model to know your current price list?

    Show answer

    No; use RAG for facts that change, and fine-tune for behaviour.

  3. What does distillation produce?

    Show answer

    A smaller, cheaper model that imitates a larger model on a task.

Lesson quiz · 5 questions

Score 60% or more to complete this lesson.

Take the quiz