Sub topic 16
Prompting, retrieval, or fine-tuning: choosing the fix
This chapter assumes you have read the chapters on prompting, evaluation, and retrieval-augmented generation (RAG). You should be comfortable with test cases for an LLM feature.
Start with the failure, not the technique
When an LLM feature gives bad answers, the cause decides the fix. Look at your failing test cases and ask what is actually missing.
| Symptom | Likely cause | First fix |
|---|---|---|
| Wrong format, tone, or label set | The instructions are unclear | Prompting |
| Wrong or invented facts about your data | The model lacks the knowledge | Retrieval |
| Right content, but style or format is inconsistent across thousands of cases | The behavior is hard to describe in words | Fine-tuning |
Try the cheapest fix first. Rewriting a prompt takes minutes and is easy to undo. Retrieval needs an index you must keep current. Fine-tuning means training a model on example input and output pairs. It needs good data, and you must repeat it when requirements change.
The fixes combine. A common design uses retrieval for facts and a good prompt for format. Fine-tuning does not reliably teach new facts. It is better for consistent style, format, or a narrow task where you want a smaller model to match a larger one.
Managing cost and latency
Providers usually bill per token, with separate prices for input and output. Output tokens often cost more and are produced one at a time, so they also dominate latency. Long retrieved context raises input cost and delays the first token.
def cost_usd(input_tokens, output_tokens, in_price, out_price):
# Prices are dollars per million tokens.
return (input_tokens * in_price + output_tokens * out_price) / 1_000_000
# Example prices: $3 input, $15 output per million tokens.
per_call = cost_usd(3000, 300, 3.0, 15.0)
print(f"{per_call:.4f} per call, {per_call * 100_000:.2f} per 100k calls")This prints 0.0135 per call and 1350.00 per 100,000 calls. Calculate this before you commit to a design.
Common levers, with their trade-offs:
- Shorter prompts and fewer retrieved chunks. Cheaper and faster, but too little context can hurt accuracy. Re-run your tests after each cut.
- A smaller model for easy steps. Classification often works on a small model. Reserve the large model for hard cases.
- Caching. Reuse answers to repeated requests, or use the provider's prompt caching for a long shared prefix.
- Capping output length. Ask for a label or short JSON rather than an essay.
- Streaming. It does not cut total time, but users see text sooner.
Measure latency at the 95th percentile, not just the average. A few slow calls shape what users remember.
Exercise
Your support bot invents refund rules that are not in your policy documents. The tone and format are fine. Which fix do you try first, and why?
Show answer
Retrieval. The failure is missing knowledge, not unclear instructions or inconsistent style. Retrieve the policy text and instruct the model to answer only from it. Fine-tuning would bake in rules that can go stale.