AI engineering path · Shipping with confidence · Topic 12 of 12

RAG, fine-tuning or long context?

Not started

When an LLM feature falls short, there are three broad ways to improve it beyond rewriting the prompt: give the model more of the right information at request time (long context or retrieval), or change the model itself (fine-tuning). They solve different problems. A helpful first question is whether the model is missing **knowledge** (facts it cannot know) or missing **behaviour** (it knows enough but responds in the wrong way).

Why it matters: Teams often reach for fine-tuning when the real gap is missing knowledge, or build a retrieval pipeline when the documents would simply fit in the prompt. Each choice has a different cost to build, run and keep up to date.

  1. 1Missing knowledge points to the context

    If the model lacks facts (your docs, today's data), put them in the prompt, either directly or through retrieval.

  2. 2Wrong behaviour points to examples

    If it knows enough but answers in the wrong format, style or decision pattern, improve prompts and examples first; fine-tune only when that plateaus.

  3. 3Long context is the simple baseline

    If the material fits comfortably and requests are not too frequent, sending it whole avoids building a retrieval system, especially with prompt caching.

  4. 4Let the evals decide

    Try the cheapest option first and measure; move to the next only when the eval shows a gap it would close.

1

How it works

The explanation, with examples you can run.

The options

Better prompts and examples. Always first. Clear instructions, a few good examples and structured output solve more problems than people expect, cost nothing to build and change in minutes.

Long context. Put the whole document, codebase section or conversation into the prompt. Simple, no index to maintain, and the model sees everything at once, which helps tasks that need the whole picture (summarising a contract, comparing two reports). The costs: every request pays for all those tokens, latency grows with input size, the window has a hard limit, and models may use details in the middle of very long inputs less reliably. Prompt caching softens the cost when the same material is reused across many requests.

Retrieval (RAG). Index a large or changing body of documents and fetch only the relevant passages per request. Scales to far more material than any window, keeps facts current by re-indexing, supports per-user permissions and citations. The costs: a pipeline to build and evaluate (chunking, embeddings, search, re-ranking), and answers can only be as good as what retrieval finds.

Fine-tuning. Train a model further on your examples so that a behaviour becomes the default: a house format, a classification scheme, a tone, a domain's way of reasoning, or a smaller model imitating a larger one on one narrow task. Parameter-efficient methods such as LoRA make this cheaper by training a small set of added weights. The costs: you need a good dataset (often hundreds to thousands of high-quality examples), a training and evaluation process, and retraining when the base model or the target behaviour changes. It is a poor store for facts: hard to update or delete, no citations.

A comparison

NeedLong contextRetrievalFine-tuning
Facts from your documentsGood if they fitGood at any sizePoor
Facts that change oftenGoodGood (re-index)Poor (retrain)
Citations to sourcesPossibleGoodNo
Consistent format or style at volumeSome help via examplesNoGood
Shorter prompts, smaller modelNoPartlyOften
Effort to build and maintainLowMedium to highMedium to high

These combine. A common production setup is retrieval for knowledge, a well-tested prompt for behaviour, and, at high volume, a fine-tuned smaller model for a narrow step such as classification or extraction.

Deciding in practice

  1. Write the eval set first, so each option is measured on the same cases.
  2. Try prompt improvements, then long context if the material fits. Measure quality, cost per request and latency.
  3. If the material does not fit, is too costly to send every time, changes often, or needs per-user permissions, build retrieval and evaluate retrieval and answers separately.
  4. If quality plateaus on format, style or a narrow decision, and you have the volume to justify it, try fine-tuning against the same eval, and keep the prompt-only version as the baseline.

Re-run the comparison when models change. Context windows, caching prices and base-model quality have moved quickly, so a choice that was right a year ago may not be the cheapest or best today.

2

Check yourself

3 questions. Get them all right to finish this topic.

  1. 1.An assistant must answer questions about a 3,000-page product manual that changes weekly. What is the usual first choice?
  2. 2.Your extraction prompt works, but needs a very long list of examples and still misses your house format 10% of the time on thousands of daily requests. What is a reasonable next step?
  3. 3.Why is fine-tuning usually a poor way to teach a model new facts?

0 of 3 right so far. Get all three to finish this topic.