AI engineering glossary · Prompt context engineering
What is few-shot prompting?
Few-shot prompting (also called Few-shot learning prompt, N-shot prompting): Including a small number of input-output examples (usually 2 to 8) in the prompt so the model copies the pattern for a new input. It teaches format, tone and edge cases without retraining. The quality, variety and order of the examples strongly affect the result.
Few-shot prompting: an example
To tag support tickets, the prompt lists five tickets with labels such as 'refund' or 'delivery delay', then adds a sixth ticket without a label.
Few-shot prompting in Python
A short example you can run (Python 3.11). The output below is what it really prints.
examples = [("The delivery was quick", "positive"), ("Item arrived broken", "negative")]
query = "Customer care solved it in minutes"
messages = [{"role": "system", "content": "Label the sentiment: positive or negative."}]
for text, label in examples:
messages += [{"role": "user", "content": text}, {"role": "assistant", "content": label}]
messages.append({"role": "user", "content": query})
for m in messages:
print(f"{m['role']:9s} {m['content']}")Output
system Label the sentiment: positive or negative. user The delivery was quick assistant positive user Item arrived broken assistant negative user Customer care solved it in minutes
Related terms
More Prompt context engineering terms
- Frequency penalty
- Function calling
- GBNF grammar
- Generated knowledge prompting
- Grammar-constrained decoding
- Grounding
- In-context learning
- Instruction following
Where you learn it
Few-shot prompting is part of the AI Engineering Program, which teaches it with notes, a hands-on lab and an exam. The first lesson is free.