Overview

4 In-context learning, few-shot, and RAG

This chapter introduces in-context learning as a practical first step for adapting an LLM, showing how few-shot prompting can solve a support-ticket classification task without any training. The core idea is to use the prompt itself to steer the model by providing a clear instruction, a small set of labeled examples, and a query in a consistent schema. The examples should be diverse, representative of real production traffic, and ordered so the most relevant ones appear last, since recency can meaningfully affect results. The chapter also shows that prompt quality is not just about accuracy on a design set: you need held-out evaluation, run-to-run stability checks, and attention to brittleness before shipping anything to production.

From there, the chapter expands from few-shot to many-shot prompting for tasks whose input distributions are too broad for a tiny example bank. Long-context models make it possible to include dozens or even hundreds of demonstrations, which can raise accuracy when coverage is the main problem, especially for high-variability classification and structured extraction. But the extra examples are not free: they increase token cost, can suffer from lost-in-the-middle attention effects, and eventually hit diminishing returns. The chapter emphasizes diagnosing the failure mode first—coverage gap, reasoning bottleneck, or missing knowledge—because more examples only help in the first case. It also shows a lightweight way to assemble many-shot prompts by selecting examples through semantic similarity rather than manual picking.

When the model lacks needed facts rather than pattern coverage, the chapter recommends retrieval-augmented generation. RAG adds a retrieval step in front of the model so the prompt includes relevant source material at inference time, making it the right tool for fresh, private, or internal knowledge the base model does not already contain. The chapter breaks RAG into its main components—chunking, embeddings, vector indexing, retrieval, and prompt assembly—and explains how to evaluate retrieval quality separately from answer quality. It closes with a decision framework for choosing among prompting, RAG, and parameter-efficient fine-tuning: stay with prompting when it works, use RAG for knowledge gaps, and move to fine-tuning when prompting plateaus, the task needs a behavior change, or the economics and latency of repeated prompting no longer make sense.

The in-context learning ladder: zero-shot, one-shot, few-shot, many-shot, and RAG sit on a single rung of the adaptation continuum, with engineering cost climbing gradually within the rung before chapter 5's parameter-update techniques take over.
Few-shot prompt anatomy: a system instruction names the task, labeled examples set the input/output schema, and a trailing “Category:” marker shows the model where its answer goes.
Lost-in-the-middle U-shape (illustrative): accuracy is high when the relevant content sits near the start or end of the context window and drops in the middle, so put your highest-signal examples at the boundaries, not the trough.
Typical validation cycle: six steps from raw production data to a shippable prompt.
Minimal RAG architecture: the ingestion path (chunker, embedder, vector index) runs once at index time; the query path (retriever, prompt assembly, LLM) runs on every request, and both paths meet at the shared vector index.
Accuracy versus shot count on the ticket-classification task (illustrative): the first plateau near eight shots is where most teams stop, with a smaller second climb between fifty and one hundred shots, where many-shot prompting earns its place.
The graduation decision tree: start with prompting, add RAG when the diagnostic shows a knowledge gap, and graduate to parameter-efficient fine-tuning when prompting plateaus or per-request cost flips the economics.

Summary

  • A few-shot prompt with five to eight well-chosen examples often delivers most of the quality of fine-tuning on classification, summarization, and extraction tasks at the cost of inference tokens only.
  • Long-context models enable many-shot prompting with fifty to two hundred examples; this scales further than the diminishing-returns curve from earlier years suggests, but per-request cost grows linearly with shot count.
  • Held-out sets scale with the decision: 20 for a smoke test, 50 to resolve large gaps, and a few hundred to report small differences. Run-to-run variability measurement is what separates a brittle prompt from a production-ready one.
  • Retrieval-augmented generation closes knowledge gaps; fine-tuning closes behavior gaps; the diagnostic signal (right-document-but-wrong-format vs. wrong-fact) tells you which technique to use.
  • A minimal RAG pipeline (vector embeddings, fifty lines of NumPy retrieval) ships in days; production extensions add chunking strategy, reranking, query rewriting, and hybrid retrieval. Evaluate retrieval hit rate, answer faithfulness, and end-to-end accuracy separately. The three answer different debugging questions.
  • The prompting-to-fine-tuning crossover is set by per-request cost and accuracy ceiling; RAG sits as a third cost lane that wins when the gap is knowledge-bound, and parameter-efficient fine-tuning becomes the right tool when the gap is behavior-bound, and volume is high.

FAQ

What is in-context learning (ICL) in this chapter?In-context learning means conditioning a model on examples inside the prompt so it follows the pattern of those examples, without updating model weights. It is the prompt-based adaptation approach used throughout the chapter.
How is prompt engineering related to in-context learning?Prompt engineering is the work of designing the instruction, choosing the examples, and formatting the schema. In this chapter, it is essentially the practical side of in-context learning.
What are the main parts of a few-shot prompt?A few-shot prompt has three parts in a fixed order: a system instruction, a sequence of labeled examples, and the query to classify. The chapter emphasizes keeping this structure consistent.
What rules should be followed when selecting few-shot examples?The examples should be diverse, representative of production traffic, and not cherry-picked. They should cover the input distribution, match real-world formatting, and avoid over-representing easy cases or a single category.
Why does example order matter in few-shot prompting?Example order matters because models attend more strongly to recent context. The chapter recommends placing the most relevant example last, especially when you have a similarity signal for the incoming query.
When does many-shot prompting become useful?Many-shot prompting is most useful when eight examples cannot cover the input distribution, such as tasks with many categories, rare classes, or high surface variability. It can improve accuracy when the gap is due to coverage, not capability.
When is many-shot prompting not the right fix?It is not the right fix when the problem is a reasoning bottleneck or a pre-training knowledge gap. If the model needs better reasoning, stronger models or fine-tuning may help; if it lacks the facts, RAG is the right tool.
What is RAG and why is it included in this chapter?RAG stands for retrieval-augmented generation. It is included here because it is still in-context learning: retrieved documents are added to the prompt at inference time, without changing model weights.
How do you tell a knowledge gap from a behavior gap?A knowledge gap means the model lacks the needed facts, so retrieval helps. A behavior gap means the model knows the facts but answers in the wrong format, tone, or structure, which points more toward fine-tuning or schema enforcement.
When should you stop prompting and move to fine-tuning?You should graduate when several prompt iterations no longer improve held-out accuracy, run-to-run variability is too high, the per-request cost becomes too large, or the task needs behavior changes that prompting and retrieval cannot solve. At that point, parameter-efficient fine-tuning is usually the next step.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • LLM Customization and Fine-Tuning ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • LLM Customization and Fine-Tuning ebook for free