Sub topic 2
What Few-Shot Prompting Is and Why It Works
This chapter assumes you know what a large language model is, can read short Python, and know the system, user, and assistant roles in chat prompts. It covers the core idea. Later chapters cover example design, selection, and testing.
From zero-shot to few-shot
A zero-shot prompt gives the model an instruction and no examples. It works when the task is common and the instruction is precise. It struggles when the label set is unusual, the output format is strict, or the task depends on a convention the instruction cannot easily state.
A few-shot prompt adds a small number of input and output pairs before the real input. The model reads the pairs, infers the pattern, and continues it. This is also called in-context learning. The model's weights do not change. The examples exist only in the prompt, and they are gone once the request ends.
Here is a few-shot prompt for sentiment classification:
Classify the sentiment of each review as positive, negative, or neutral.
Review: The battery lasts all day and charging is fast.
Sentiment: positive
Review: It stopped working after a week.
Sentiment: negative
Review: The package arrived on Tuesday.
Sentiment: neutral
Review: The screen is bright but the speakers are weak.
Sentiment:The prompt has an instruction, three labeled examples in an identical format, and an unfinished final entry. The model completes that entry.
Why it works
The model is trained to predict the next token. A prompt that repeats a pattern makes the pattern the most likely continuation. After three Review: and Sentiment: pairs, the likeliest text after the last Sentiment: is one of the labels.
Examples carry three kinds of information that instructions often carry badly:
- The task. The model sees what mapping from input to output you mean.
- The format. One lowercase word, no punctuation, no explanation. Showing this is easier than describing it.
- The style and boundaries. Where "neutral" ends and "negative" begins shows up in the examples, not in the instruction.
Building the prompt in code
In a chat API, each example becomes a user turn followed by an assistant turn that shows the desired answer.
SYSTEM = "Classify the sentiment of each review as positive, negative, or neutral. Answer with one word."
examples = [
("The battery lasts all day and charging is fast.", "positive"),
("It stopped working after a week.", "negative"),
("The package arrived on Tuesday.", "neutral"),
]
def build_messages(examples, query):
messages = [{"role": "system", "content": SYSTEM}]
for text, label in examples:
messages.append({"role": "user", "content": text})
messages.append({"role": "assistant", "content": label})
messages.append({"role": "user", "content": query})
return messages
if __name__ == "__main__":
for m in build_messages(examples, "The screen is bright but the speakers are weak."):
print(m["role"], "->", m["content"])The function keeps the examples as data. You can change, reorder, or swap them without rewriting the prompt.
Common mistakes at this stage
- Treating examples as training. Nothing is learned permanently. Every request must include the examples again, and you pay for those tokens each time.
- Inconsistent examples. If one example answers "Positive." and another answers "positive", the model has no single format to copy.
- Assuming more is better. Examples take up context and can push the model to copy surface features. Later chapters cover how many to use.
- Skipping the check. A few-shot prompt that looks right on three inputs can still fail on others. You need held-out data to know.
Exercise
A teammate says that adding three examples to a prompt "trains" the model on their task. What is wrong with this claim, and what practical consequence follows?
Show answer
The model's weights do not change. The examples only shape the output for the current request through the context. In practice, you must send the examples with every request, which adds token cost and latency, and the model forgets them as soon as the request ends.
Exercise
You have the instruction "Classify the sentiment" and a model that sometimes replies with a full sentence. Name one way few-shot examples can fix this.
Show answer
Add examples whose outputs are a single lowercase word, such as "positive". The model then continues the pattern and answers with one word.