Skip to main content
Prompt Engineering Tutorial
CHAPTER 07 Beginner

Few-Shot Prompting

Updated: May 14, 2026
20 min read

# CHAPTER 7

Few-Shot Prompting

1. Introduction

In the previous chapter, we learned that Zero-Shot prompting is great for basic tasks. But what if you want the AI to learn a brand-new task, mimic a highly specific writing style, or output data in a weird, custom format? Telling the AI what to do isn't enough; you have to *show* it. In this chapter, we will master Few-Shot Prompting, the most reliable technique for forcing an LLM to adopt specific patterns.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Define Few-Shot Prompting.
  • Understand how examples override an LLM's default behaviors.
  • Write a structured Few-Shot prompt for pattern recognition.
  • Apply Few-Shot techniques to classification and formatting tasks.

3. Beginner-Friendly Explanation

Imagine hiring an assistant to sort your company's emails. You have a bizarre, custom filing system. You tell the assistant: *"Sort the emails."* (This is Zero-Shot). They fail, because your system is weird. So, you show them examples. You say: *"If the email says 'Invoice', put it in the Blue Folder."* *"If the email says 'Complaint', put it in the Red Folder."* *"Now, here is a new email that says 'Late Fee'. Where does it go?"* Because you provided a few examples (shots), the assistant instantly recognizes the pattern and puts the late fee in the Blue Folder. This is Few-Shot Prompting: teaching the AI a custom pattern by showing it 2 or 3 examples before giving it the actual task.

4. How Few-Shot Overrides the AI

An LLM has built-in defaults. If you ask an LLM to write a joke, it will write a standard, slightly cheesy Dad joke. If you want dark, sarcastic jokes, you use Few-Shot prompting. By placing 3 examples of dark, sarcastic jokes in the prompt, the AI's "Attention Mechanism" mathematically aligns with that specific tone. The examples *override* the AI's default generic tone, forcing it into your desired pattern.

5. Formatting a Few-Shot Prompt

A professional Few-Shot prompt follows a strict structure:
  1. 1. Instruction: What to do.
  1. 2. Examples: The "Shots" (Input -> Output patterns).
  1. 3. The Target Task: The actual question you want answered.

6. Prompt Example: Custom Classification

Here is a Few-Shot prompt teaching an AI a completely made-up language classification system.
text
12345678910111213141516
# INSTRUCTION
Translate the English word into "Corporate Speak". Follow the pattern below.

# EXAMPLES
Input: "Talk"
Output: "Touch base"

Input: "Use"
Output: "Leverage"

Input: "Fix"
Output: "Remediate"

# TASK
Input: "Plan"
Output:

*Result:* The AI recognizes the pattern and outputs "Strategize" or "Synergize". Without the examples, the AI would have just translated "Plan" into a real language like Spanish.

7. Prompt Example: Strict JSON Formatting

Few-Shot is the absolute best way to ensure an AI doesn't break your code by outputting bad JSON.
text
12345678910111213
Extract the data. Use this EXACT format:

[Example 1]
Text: "John bought 3 apples."
JSON: {"name": "John", "item": "apples", "qty": 3}

[Example 2]
Text: "Sarah returned 1 laptop."
JSON: {"name": "Sarah", "item": "laptop", "qty": 1}

[Task]
Text: "Mike purchased 5 monitors."
JSON:

8. Python Example: Few-Shot in the API

In the OpenAI API, you can pass examples by simulating a conversation history in the messages array.
python
1234567891011121314151617181920
import openai
client = openai.OpenAI()

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "system", "content": "You classify text tone."},
        # Example 1
        {"role": "user", "content": "I am so mad right now!"},
        {"role": "assistant", "content": "Tone: Angry"},
        # Example 2
        {"role": "user", "content": "This is the best day ever!"},
        {"role": "assistant", "content": "Tone: Joyful"},
        # The actual task
        {"role": "user", "content": "I can't wait to see what happens next."}
    ]
)

print(response.choices[0].message.content)
# Output: Tone: Excited

9. Mini Project

The Sarcastic Bot: Write a Few-Shot prompt containing 2 examples that teaches the AI to respond to technical support questions with extreme, unhelpful sarcasm. Provide a 3rd question as the final task. *(Answer Example: Input: "My mouse won't work." Output: "Have you tried asking it nicely? Or maybe plugging it in." Input: "My screen is black." Output: "Congratulations, you found the power button. Now press it." Task: Input: "The Wi-Fi is slow.")*

10. Best Practices

  • Diversity of Examples: If all your examples are exactly the same, the AI will overfit. If you are teaching it to extract names, provide one example of a short name, one of a long name, and one of a name with a title (Dr. Smith) so it learns the *concept*, not just the exact word.

11. Common Mistakes

  • Providing Too Many Shots: Providing 2 to 5 examples is usually perfect. Providing 50 examples wastes massive amounts of tokens, increases your API costs, and clogs the AI's Context Window, often confusing the model more than it helps.

12. Exercises

  1. 1. Explain why teaching an AI to output a highly specific JSON format requires Few-Shot prompting rather than Zero-Shot prompting.

13. MCQs with Answers

Question 1

What defines a "Few-Shot" prompt?

Question 2

How does Few-Shot prompting affect an LLM's behavior?

14. Interview Questions

  • Q: In an enterprise application, you notice the LLM is consistently breaking your data pipeline because it formats its output unpredictably. How would you use Few-Shot prompting to fix this?
  • Q: Explain the structure of "simulating conversation history" in an API call to achieve a Few-Shot effect.

15. FAQs

Q: Is there such a thing as "One-Shot" prompting? A: Yes! One-Shot is simply providing exactly 1 example. It is often enough for simple formatting tasks, while Few-Shot (3-5 examples) is better for complex logical patterns.

16. Summary

In Chapter 7, we unlocked pattern recognition. Few-Shot prompting is the ultimate tool for controlling LLM output. By providing "Input -> Output" examples within the prompt, we teach the AI on the fly. Whether we are forcing the AI to speak in corporate jargon, adopt a sarcastic persona, or output perfectly structured JSON arrays, Few-Shot examples override the machine's generic defaults and force it to execute our exact vision.

17. Next Chapter Recommendation

Few-Shot teaches the AI *patterns*. But what if the task requires deep, multi-step math or logic? The AI will still fail. Proceed to Chapter 8: Chain-of-Thought Prompting to learn the magic phrase that makes AI smarter.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·