AI From Zero · Prompt Engineering

Few-Shot Prompting and Examples

Learn how examples can show an AI assistant the pattern, format, classification, or style you want it to follow.

Estimated learning time: 18 minutes

What You'll Learn

By the end of this lesson, you will be able to:

- Explain how examples can guide an AI response.
- Distinguish zero-shot, one-shot, and few-shot prompting.
- Create useful input and output examples.
- Use examples to demonstrate a desired format or pattern.
- Use examples for classification tasks.
- Recognize the characteristics of a good example.
- Avoid ambiguous or inconsistent examples.
- Choose representative examples for a task.
- Understand when examples can be more useful than lengthy instructions.

What Is an Example in a Prompt?

An example shows the AI what a desired input and output can look like.

Instead of describing every detail of a pattern with instructions, you can sometimes demonstrate the pattern directly.

For example:

Input: The product arrived early.

Output: Positive

This demonstrates how a classification task should work.

Why Examples Can Help

Examples can make an intended pattern easier to understand.

They can demonstrate:

  • Format.
  • Structure.
  • Classification.
  • Writing style.
  • Level of detail.
  • Transformation rules.
  • Expected input and output relationships.

An example can sometimes communicate a pattern more clearly than a long verbal explanation.

Zero-Shot Prompting

Zero-shot prompting means asking the AI to perform a task without providing examples of the desired behavior.

For example:

Classify the following customer comment as Positive, Neutral, or Negative.

No example is provided.

One-Shot Prompting

One-shot prompting provides one example before asking the AI to perform the task.

For example:

Example: The delivery was excellent. → Positive

Now classify: The package arrived earlier than expected.

The example demonstrates the desired classification pattern.

Few-Shot Prompting

Few-shot prompting provides several examples before asking the AI to handle a new input.

For example:

Example 1: The service was excellent. → Positive

Example 2: The service was acceptable. → Neutral

Example 3: The service was disappointing. → Negative

Now classify: The service was helpful and friendly.

The examples demonstrate several possible categories.

Zero-Shot, One-Shot, and Few-Shot

The basic difference is the number of examples provided:

  • Zero-shot: No examples.
  • One-shot: One example.
  • Few-shot: Several examples.

The choice depends on how easy the task is to describe and how useful examples are likely to be.

Examples Demonstrate Patterns

The purpose of an example is usually not simply to provide an answer. It is to demonstrate a pattern that the AI should apply to new inputs.

For example, if every input is converted into a short summary, the examples demonstrate what that summary should look like.

Input and Output Examples

A useful example often contains both the input and the desired output.

For example:

Input: Meeting moved to Friday.

Output: Schedule Change

This clearly demonstrates the relationship between the input and output.

Classification Examples

Examples can be particularly useful for classification.

For example:

Input: I love the new interface.

Category: Positive

Input: The interface is acceptable.

Category: Neutral

Input: The interface is difficult to use.

Category: Negative

The AI can use these examples to understand the intended categories.

Format Examples

An example can show exactly how information should be formatted.

For example:

Input: John Smith, London, Manager

Output: Name: John Smith | City: London | Role: Manager

A new record can then be presented using the same pattern.

Writing Style Examples

Examples can demonstrate a writing style.

For example, if you want short professional customer responses, provide an example of the desired response.

The AI can use that example as a reference for the new response.

Tone Examples

Examples can also demonstrate tone.

For example, a friendly customer response might use simple language, acknowledgement, and a helpful closing.

Providing an example can communicate those characteristics more directly than simply saying make it friendly.

Structure Examples

Examples can demonstrate the structure of a response.

For example:

Title: Main issue

Summary: One short paragraph

Action: One recommended next step

A new response can follow the same structure.

Examples Can Complement Instructions

Examples do not always replace instructions.

You can combine an explicit instruction with examples.

For example:

Classify each customer comment as Positive, Neutral, or Negative. Use the examples below as guidance.

The instruction explains the task while the examples demonstrate the intended behavior.

Examples Can Complement Output Format

You can specify a format and provide an example of that format.

For example:

Return each record in this format: Name | City | Role.

Example: John Smith | London | Manager

The written instruction and example reinforce each other.

Good Examples Should Be Representative

An example should represent the type of task the AI will actually perform.

If you are classifying customer comments, examples should resemble real customer comments.

If you are formatting business records, examples should resemble the actual records.

Use Diverse Examples When Appropriate

Several examples should cover useful variations of the task.

For a classification task with three categories, it can be useful to show examples from all three categories.

This gives the AI a broader demonstration of the intended pattern.

Avoid Contradictory Examples

Examples should be consistent with the instructions and with each other.

If similar inputs receive completely different outputs without explanation, the AI may have difficulty identifying the intended pattern.

Avoid Ambiguous Examples

An ambiguous example may have several reasonable interpretations.

When teaching a classification pattern, choose examples where the intended category is reasonably clear.

Examples Should Match the Task

Do not provide examples that demonstrate a different task from the one you are asking the AI to perform.

For example, examples showing long summaries are not useful if you are asking for one sentence summaries.

Examples Should Match the Desired Output

If the final output should be concise, examples should also be concise.

If the final output should contain several fields, examples should demonstrate those fields.

Examples and Edge Cases

When a task contains unusual cases, examples can demonstrate how those cases should be handled.

For example, if an input does not contain enough information, an example can show whether the expected output should be Unknown, Needs Review, or another category.

Examples for Text Transformation

Examples can demonstrate transformations.

For example:

Input: Customer requested refund.

Output: Refund request

This shows how a longer phrase should be converted into a short label.

Examples for Summarization

You can demonstrate the level of detail expected from a summary.

For example:

Input: A long paragraph describing a product launch.

Output: Product launch planned for September with an initial focus on small businesses.

The example demonstrates a concise summary rather than a detailed rewrite.

Examples for Data Extraction

Examples can show which information should be extracted.

For example:

Input: John placed order 4582 on March 12 for 250 dollars.

Output: Customer: John | Order: 4582 | Date: March 12 | Amount: 250 dollars

This demonstrates the required fields and structure.

Examples for Classification

Classification examples should clearly show the relationship between an input and its category.

For example:

Fast deliveryPositive

Delivery was acceptableNeutral

Delivery was very lateNegative

A new comment can then be classified using the demonstrated pattern.

Examples for Rewriting

Examples can demonstrate how a sentence should be transformed.

For example:

Original: Send me the document soon.

Improved: Please send me the document by Friday afternoon.

This demonstrates a transformation from vague language to more specific language.

How Many Examples Should You Provide?

There is no universal number that works for every task.

Use enough examples to demonstrate the important pattern without adding unnecessary material.

A simple task may need one example.

A task with several categories or variations may benefit from several examples.

Quality Matters More Than Quantity

Ten poor examples are not necessarily better than three clear examples.

Choose examples that clearly demonstrate the behavior you want.

Examples and Prompt Length

Examples add information to a prompt.

If the task is simple, too many examples may add unnecessary complexity.

Use examples when they provide meaningful guidance.

Examples Can Be More Efficient Than Long Descriptions

Some patterns are difficult to describe in words.

In those situations, an example can communicate the desired behavior quickly.

For example, a particular data format may be easier to demonstrate than to describe in several paragraphs.

Examples Do Not Guarantee Correctness

An AI can still misunderstand examples or apply a pattern incorrectly.

Examples guide behavior; they do not guarantee that the final response is accurate.

Examples Can Contain Hidden Problems

If an example contains an error, the AI may reproduce the same pattern.

Therefore, review examples before using them in an important workflow.

Consistency Is Important

Use consistent labels, structures, and formatting across examples.

For example, if one example uses Positive, another should not unexpectedly use positive feedback if both mean the same category.

Examples and Instructions Should Agree

If an instruction says to produce three fields but the example contains five fields, the prompt contains conflicting guidance.

Make sure the written instructions and examples support the same intended behavior.

A Complete Few-Shot Example

Consider this prompt:

Classify each customer comment as Positive, Neutral, or Negative.

Example 1: The support team was very helpful. → Positive

Example 2: The support was acceptable. → Neutral

Example 3: The support team did not solve my problem. → Negative

Now classify: The support representative solved my issue quickly.

The examples demonstrate all three categories before the new input is presented.

A Complete Formatting Example

Suppose you want information extracted from customer records.

You can provide:

Input: Sarah Lee from Mumbai ordered product A for 500 dollars.

Output: Customer: Sarah Lee | City: Mumbai | Product: A | Amount: 500 dollars

Then provide another record and ask the AI to use the same format.

When Examples Are Especially Useful

Examples are particularly useful when:

  • The desired output follows a pattern.
  • The task involves classification.
  • The required format is unusual.
  • The desired writing style is difficult to describe.
  • The transformation rule is easier to demonstrate than explain.
  • The task contains several possible categories.

When Examples May Not Be Necessary

Examples may add little value when the task is already very clear.

For example:

Convert 10 kilometers to miles.

There is usually no need to provide several examples of unit conversion.

Choosing Between Instructions and Examples

Instructions and examples solve different problems.

Instructions explain what the AI should do.

Examples demonstrate what the desired behavior can look like.

For many tasks, the strongest prompt combines both.

Review Your Examples

Before using examples, ask:

  • Are they correct?
  • Are they representative?
  • Are they consistent?
  • Are they relevant to the task?
  • Do they demonstrate the desired output?
  • Do they cover important variations?

Improve a Few-Shot Prompt

If the AI is producing inconsistent results, review the examples.

You may need to:

  • Replace ambiguous examples.
  • Add an example for an important category.
  • Remove contradictory examples.
  • Make the output format more consistent.
  • Add a clearer instruction.

A Practical Framework

For a few-shot prompt, a useful structure is:

  1. State the task.
  2. Define important categories or rules.
  3. Provide representative examples.
  4. Provide the new input.
  5. Request the output in the demonstrated format.

What You Should Remember

  • Examples can demonstrate patterns that an AI should apply to new inputs.
  • Zero-shot means no examples, one-shot means one example, and few-shot means several examples.
  • Examples can demonstrate classification, formatting, style, structure, and transformation.
  • Good examples should be relevant, representative, clear, and consistent.
  • Examples should agree with the written instructions.
  • Quality matters more than simply increasing the number of examples.
  • Examples can complement instructions and output format requirements.
  • Examples do not guarantee accuracy.
  • Review examples carefully before using them in important tasks.

What Comes Next?

The next lesson will focus on combining prompt components to create complete prompts for more complex tasks.

Key Takeaways

  • Examples demonstrate desired patterns.
  • Zero-shot uses no examples.
  • One-shot uses one example.
  • Few-shot uses several examples.
  • Examples can guide classification, formatting, style, and transformation.
  • Representative and consistent examples are most useful.
  • Examples should agree with instructions.
  • Examples guide behavior but do not guarantee accuracy.

Try It Yourself

Create a few-shot classification prompt.

Choose a simple classification task such as classifying customer comments, support requests, or types of documents.

Define three categories.

Then create:

  • Three clear examples.
  • One new input that the AI must classify.
  • A clear instruction describing the task.
  • A consistent output format.

Run the prompt through an AI assistant and check whether the result follows the pattern demonstrated by your examples.

Test Your Knowledge

You've reached the end of this lesson.

Test what you've learned with the Few-Shot Prompting and Examples - Quiz.

Take the Quiz
← Using Constraints Effectively
Step-by-Step Reasoning and Structured Tasks →
Back to Course