Few-Shot Prompting
Show the model two or three worked examples inside the prompt, then ask your real question. It copies the pattern instead of guessing the format.
Why Does This Exist?
You ask for names and get a paragraph. You ask again and get JSON. A third try returns a neat list with a friendly intro you now have to strip. When that output feeds code, each surprise shape is a crash. You'll feel it when the pipeline stops.
Zero-shot prompting leaves the shape up to the model. Long instructions help a little, but models don't keep a negative rule once the task gets interesting. Few-shot prompting flips the job. You stop describing the shape and start showing it, with two or three input-output pairs placed before your real question. If they match, the model continues the run instead of inventing a new one.
This page teaches that showing move. The reason showing works is in-context learning. When you need a hard guarantee instead of a strong nudge, read structured outputs. You only need one idea to follow along. The model predicts the next token from the tokens before it, so a prompt that repeats one shape makes any other shape unlikely. The full story lives in next-token prediction.
Think of It Like This
The expense sheet with two filled rows
You join a team and must file expenses. The handbook lists a dozen rules about dates, codes, and receipts. You skim it, then you open last month's sheet. Two filled rows sit there with the same columns, the same short codes, the totals on the right. You copy those rows. Your third row looks right because it looks like the other two.
Your prompt works the same way. The instruction is the header. Each example is a filled row: a sample input beside its sample output, same dividers, same order. Your real question is the blank third row, and the model fills it to match.
Where it stops: you understood why the codes exist. The model didn't. It matched a surface pattern, so if both example rows share an accident of length, name, or order, it copies that accident too.
How It Actually Works
Build one string, in fixed order: an instruction, then examples, then your question. Call the whole string , which means the full prompt you send. Call the instruction , which means the one line that states the shape. Call the count of examples , which means how many pairs you show, usually two or three. Call the -th sample input , which means one example question, and its sample answer , which means the output in the exact shape you want. Call your real input , which means the question you care about, and the reply , which means what the model writes next.
Then is followed by through , followed by with the output left blank. No weights change. The model predicts the next token from the run before it, so after two identical dividers and two identical output shapes, a chatty opener would break the run. Its chance sinks and the patterned continuation wins. That's the entire mechanism, and you can see why delimiter discipline matters. One stray format teaches a second pattern.
The shape of the prompt
Here is the prompt used through this page. Same labels, same divider, same order, then the new input with the output left open:
Extract the CEO. Reply as ORG | CEO.
Input: Apple is run by Tim Cook.Output: Apple | Tim Cook
Input: Satya Nadella runs Microsoft.Output: Microsoft | Satya Nadella
Input: Meta was founded by Mark Zuckerberg.Output:The expected finish is , which is Meta | Mark Zuckerberg. Four words, same divider, no intro. Short.
Worked example
Count the prompt above by splitting on whitespace, the way the snippet below does. is 8 words. The first pair is 12 words, the second pair is 10 words, and the final question block is 8 words. Add them: words in, with . The answer is 4 words out. That 38 to 4 ratio is the trade you're making. Every example buys reliability and spends context, latency, and cost, because the whole prompt travels on each call. You can feel the spend in context windows.
Do not reach for this when the examples won't fit. Long documents, dozens of labels, or pages of edge cases belong elsewhere. Use retrieval or a longer window for long inputs, structured outputs when you need a parsing guarantee, and when to fine-tune when the same pairs will be sent thousands of times. Few-shot rests on one assumption: the examples fit, they share one consistent shape, and the model is strong enough to continue a pattern. When that fails you get order effects, where the model favors the most recent example, or it latches onto a shared accident across your pairs. Vary the examples and test the order instead of adding more.
Code
The point here is assembly plus a quick size check. This builder lays out the same prompt and counts its words, so you can see the cost before you send it:
def format_example(text: str, label: str) -> str: return f"Input: {text}\nOutput: {label}"
def build_prompt(instruction: str, pairs: list[tuple[str, str]], query: str) -> str: blocks: list[str] = [instruction] for text, label in pairs: blocks.append(format_example(text, label)) blocks.append(f"Input: {query}\nOutput:") return "\n\n".join(blocks)
instruction: str = "Extract the CEO. Reply as ORG | CEO."pairs: list[tuple[str, str]] = [ ("Apple is run by Tim Cook.", "Apple | Tim Cook"), ("Satya Nadella runs Microsoft.", "Microsoft | Satya Nadella"),]query: str = "Meta was founded by Mark Zuckerberg."
prompt: str = build_prompt(instruction, pairs, query)print(len(pairs)) # -> 2print(len(prompt.split())) # -> 38print(query.split()[-1]) # -> Zuckerberg.Two pairs, 38 words in the full string, and the query ends on Zuckerberg. as written. Keep this builder around. When a prompt misbehaves, printing it shows the second pattern you taught by mistake.
Watch Out For
Every example shares a trait you did not mean to teach
Your demo works and production fails. The symptom is specific: the model handles inputs shaped like your examples and mishandles the rest. All your positives were short, all your negatives were long, or both CEO examples put the company first, so the model learned length or position instead of the task.
Fix it by varying what should not matter. Mix short and long inputs, swap orders and names, and include one edge case per class. Test the examples on held-out inputs before you add more of them. More of the same deepens the wrong lesson.
The prompt keeps growing past the point of help
The symptom is cost without gain. Latency climbs, the bill climbs, and accuracy stalls or slides after a handful of pairs. The model starts echoing the most recent example, quirks included, because recency weighs heavily in a long run.
Fix it by cutting back to two or three tight, diverse pairs with clean dividers. Score one, two, and three examples on the same small set and keep the smallest count that holds. If the task truly needs many cases, that is a sign to retrieve examples per query or to fine-tune instead of stuffing the window.
The Quick Version
- Few-shot prompting shows two or three input-output pairs before the real question so the model copies their shape.
- The prompt is one string: instruction, then pairs, then the new input with the output left blank.
- No weights change in few-shot prompting; the repeated pattern makes a nonmatching reply unlikely.
- Each example spends context, latency, and cost on every call, so two tight pairs beat six loose ones.
- Vary the examples and test their order, since the model copies accidents as faithfully as intent.
- Chain-of-thought prompting shows worked reasoning steps before the answer, applying the few-shot idea to multi-step problems.