What Is Few-Shot Prompting?

Few-shot prompting means including a small number of input/output examples directly in the prompt to show the model what you expect. The model learns the pattern from these examples without any weight updates — it is purely in-context learning.

Zero-Shot vs One-Shot vs Few-Shot

ApproachExamples providedBest for
Zero-shot0Simple, well-defined tasks with clear instructions
One-shot1Format demonstration; model just needs to see the schema
Few-shot3–8Complex patterns, edge cases, nuanced classification

When Few-Shot Helps Most

  • Classification: Show examples of each label to eliminate ambiguity.
  • Structured output formatting: Demonstrate exact JSON/CSV/XML schema.
  • Information extraction: Show what to pull from unstructured text.
  • Reasoning tasks: Examples illustrate the depth of reasoning expected.

Choosing Good Examples

  • Diversity: Cover different sub-cases, not just the easy majority.
  • Quality: Every example must be correct — bad examples teach bad patterns.
  • Coverage: Include at least one edge-case example if edge cases matter.
  • Recency: For time-sensitive tasks, prefer recent examples.

Structuring Examples

system_prompt = """
Classify customer feedback as: positive, negative, or neutral.

Examples:
Input: "The product is amazing, exceeded my expectations!"
Output: positive

Input: "Delivery was late and packaging was damaged."
Output: negative

Input: "Item arrived. Works as described."
Output: neutral

Now classify the following:
"""

Negative Examples

Explicitly showing what NOT to do prevents common hallucinations:

# Bad — don't do this:
# Input: "Great product!"
# Output: {"sentiment": "positive", "confidence": 0.95, "explanation": "The user..."}

# Good — do this:
# Input: "Great product!"
# Output: positive

Dynamic Few-Shot Selection

For large example libraries, select the most relevant examples at runtime using semantic similarity:

from openclaw import Agent
from openclaw.examples import ExampleSelector

selector = ExampleSelector.from_yaml("prompts/examples.yaml", max_examples=5)

agent = Agent(model="gpt-4o-mini")

def classify(text: str) -> str:
    examples = selector.select(query=text)
    prompt = build_prompt(text, examples=examples)
    return agent.run(prompt).output

Few-Shot vs Fine-Tuning

Both approaches teach the model a pattern, but they differ in cost, flexibility, and speed. Few-shot is the right starting point for most tasks; fine-tuning makes sense only when you need consistently high quality across millions of requests or when the pattern cannot be conveyed in the context window.

DimensionFew-ShotFine-Tuning
Setup timeMinutes — write examples in promptDays — collect data, train, evaluate
Cost per requestHigher (more input tokens)Lower (shorter prompt needed)
FlexibilityChange examples any timeRequires re-training to change
Context limitConstrained by context windowNot affected
Best forPrototyping, low volume, varied tasksHigh volume, stable tasks, tight latency

Common Few-Shot Pitfalls

  • Label imbalance. Using only positive examples for a classification task biases the model toward that class. Always cover every label in roughly equal proportion.
  • Order effects. Models give extra weight to examples near the end of the prompt. Place your most representative examples last, hardest examples first.
  • Schema drift. If examples use slightly different output schemas (e.g., one uses sentiment and another uses label), the model will hallucinate randomly between them. Keep schemas identical.
  • Too many examples. Adding more than 8–10 examples rarely improves quality and inflates token cost. Run an ablation: measure quality at 3, 5, and 8 examples.
  • Copying real user data into examples. Scrub PII before using production samples as few-shot examples — they are sent to the model on every request.

Few-Shot for Structured Output

JSON / CSV output is one of the most valuable few-shot use-cases. Demonstrating the exact schema in examples is far more reliable than describing the schema in prose.

system_prompt = """
Extract order information as JSON. Return only the JSON object.

Examples:
Input: "I need 3 red apples delivered tomorrow morning."
Output: {"item": "apples", "quantity": 3, "colour": "red", "delivery": "tomorrow morning"}

Input: "Please send two blue pens urgently."
Output: {"item": "pens", "quantity": 2, "colour": "blue", "delivery": "urgent"}

Input: "Order 10 white A4 sheets for Friday delivery."
Output: {"item": "A4 sheets", "quantity": 10, "colour": "white", "delivery": "Friday"}
"""

# Combine with the OpenAI response_format parameter for guaranteed JSON
response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "system", "content": system_prompt},
              {"role": "user", "content": user_input}],
    response_format={"type": "json_object"},
)