Sub topic 5
Few-shot examples and output structure
This chapter assumes you have called a chat-style LLM API, can read Python, and know what a context window is. It covers the first tool in the prompting step: showing the model examples and fixing the shape of its output.
What few-shot prompting does
A few-shot prompt includes a small number of input and output pairs before the real input. The model reads them as part of the context and continues the pattern. Nothing about the model changes and no new data is added. This is the cheapest way to steer behavior, so you try it before RAG or fine-tuning.
Few-shot examples fix behavior gaps: label vocabulary, tone, length, and format. They do not fix knowledge gaps. Examples cannot teach the model your current refund policy.
Building the message list
In a chat API, each example becomes a user message followed by an assistant message. The system message states the task and the output format.
def build_messages(question, examples):
messages = [{
"role": "system",
"content": "Classify the support ticket as billing, bug, or other. Reply with one word.",
}]
for text, label in examples:
messages.append({"role": "user", "content": text})
messages.append({"role": "assistant", "content": label})
messages.append({"role": "user", "content": question})
return messages
examples = [
("I was charged twice this month.", "billing"),
("The export button crashes the app.", "bug"),
]
messages = build_messages("My invoice shows the wrong amount.", examples)The final user message is the real question. The model sees two completed turns and answers the third in the same style.
Choosing good examples
- Cover every label. If no example shows
other, the model may rarely use it. - Include a hard case, such as a ticket that mentions money but is really a bug.
- Keep examples varied in wording so the model learns the task, not one phrasing.
- Keep the label distribution balanced. The model tends to favor labels it saw most often, and the last example has extra pull.
- Start with 2 to 5 examples. Add more only if your evaluation set shows a gain, because each example costs tokens on every request.
Making the output structure reliable
Examples show the format, and the system message states it. Neither guarantees it. Treat the reply as untrusted input and validate it in code.
ALLOWED = {"billing", "bug", "other"}
def parse_label(reply):
label = reply.strip().lower().rstrip(".")
return label if label in ALLOWED else None
print(parse_label("Billing.")) # billing
print(parse_label("It is billing.")) # NoneWhen the result is None, retry once or send the ticket to a default queue. For richer output, ask for JSON with named fields. Many APIs also offer a structured-output or JSON mode, which constrains the reply to a schema. Use it if you have it, and still validate.
Common errors
- Format drift. The model adds a sentence before the label. Tighten the instruction ("Reply with one word") and check that your examples follow it exactly.
- Copying the examples. The model repeats an example's content. Make examples differ clearly from real inputs.
- Inconsistent examples. Two similar tickets with different labels teach noise.
- Context bloat. Many long examples crowd out the input and raise cost and latency.
- Testing on your examples. Keep the examples separate from your evaluation set, or your score will look better than it is.
Exercise
Your few-shot classifier returns "Sure! This looks like a billing issue." instead of "billing". Name two changes that address this and one thing you should add to the application code.
Show answer
Tighten the system instruction (for example, "Reply with exactly one word from the list") and make sure every example's assistant message is a bare label with no extra text. In the application code, add validation such as parse_label that checks the reply against the allowed set and handles failures with a retry or a default.