Few-Shot Prompting

Few-Shot Prompting

In this step, you’ll learn how to teach by example: you’ll add a few compact “good” (and sometimes “bad”) samples to your prompt so the model mirrors the structure, tone, and depth you want. By the end, you’ll know how to pick effective examples, how many to include, and how to format them so you can reliably steer outputs without rewriting the whole prompt (or fine-tuning a model).

Problem

You may have noticed that the AI isn’t producing results in the format, tone, or level of detail you want. The structure may be off, or the content feels generic.

Your task is to add a few examples — both good and bad — directly into your prompt. By showing what success and failure look like, you’ll steer the output toward your standards without having to describe every rule explicitly.

What you need to know

Few-shot prompting means showing the AI a few examples of what “good” looks like before asking it to do the same thing.
Instead of describing quality in abstract terms, you demonstrate it.

Models learn patterns more effectively from examples than from rules.
When you demonstrate good vs bad documentation, you’re teaching tone, structure, and depth implicitly.

Example 1

In the example below, the prompt demonstrates what good and bad git commits look like:

Task: Write a high-quality commit message.

Good examples:
feat(ui): add search bar with debounce

  • Adds search input to header with 300ms debounce to reduce API load

  • Persists last query in localStorage

  • Updates empty state copy
    Closes #123

fix(auth): refresh access token on 401 responses

  • Intercepts 401, attempts refresh, retries original request once

  • Adds unit tests for interceptor logic
    Refs #456

docs(readme): clarify setup for env variables

  • Adds example .env

  • Notes required NODE_ENV values

Bad examples:
wip
fix stuff
update code and tests
final changes before release

Notice how this example demonstrates the language and structure of good and bad. In the good examples, the AI can also parse information about how to categorize different types of changes (ex. feat(ui), fix(auth), etc).

For examples like these, the more information you can encode, the better. There's an art to selecting examples that demonstrate the full spectrum of what you’re expecting from the AI.

Example 2

In this example, we’ll demonstrate how few-shot can be used for outputting JSON (or YAML, HTML, etc). Structured output like this is especially useful when you need to match a standardized format, or you’ll be pasting the output directly into a file.

The Task

Extract structured information from a sentence and return it as JSON.
For example:
"Schedule a meeting with Dana tomorrow at 3pm about Q4 planning."
→ We want the AI to return that as structured data.

❌ Without Few-Shot

Extract structured data from the sentence:
"Schedule a meeting with Dana tomorrow at 3pm about Q4 planning."

Return the result as JSON.

Possible output:
The model may try, but results vary: maybe the keys change, fields are missing, or it's not valid JSON.

✅ With Few-Shot (2 examples)

Prompt:

Extract structured data from a sentence.

Examples:

Input: "Schedule a meeting with Jordan on Monday at 9am to talk about onboarding."
Output: {
"person": "Jordan",
"date": "Monday",
"time": "9am",
"topic": "onboarding"
}

Input: "Set up a call with Mei on Friday at 2pm to review the Q3 roadmap."
Output: {
"person": "Mei",
"date": "Friday",
"time": "2pm",
"topic": "Q3 roadmap"
}

Now extract data from:
"Schedule a meeting with Dana tomorrow at 3pm about Q4 planning."

Expected Output:

{ "person": "Dana", "date": "tomorrow", "time": "3pm", "topic": "Q4 planning" }

Reflection

  • Without examples, the model had to guess what your format meant.

  • With two good examples, it nailed the structure, fields, and style.

  • You can tweak the structure, field names, or rules just by updating the examples — no need to re-write the prompt logic.

Tips

Context matters. Few-shot isn’t just “show any examples.” The examples are the model’s working context. They telegraph domain assumptions, sectioning, vocabulary, and “what counts as quality.” Choose examples that match your target audience, depth, and house style.

Contrast is powerful. It’s often just as useful to show what not to do as it is to show what to do. A concise bad example clarifies anti-patterns (hand-wavy prose, missing sections, inaccurate tone) and reduces ambiguity.

Style can be explicit. You can ask for established styles (“Write in the style of X” or “Follow our docs style guide”). When you do, combine that with constraints so the model imitates tone/structure, not proprietary content.

How to select examples

  • Representative: Mirror the domain (web app vs. data pipeline), audience (beginner vs. expert), and artifact (how-to vs. reference).

  • Scoped: 1–3 short examples beat one long wall of text—aim for pattern density.

  • Structured: Include headings, bullets, or JSON so the model can mirror shape, not just tone.

  • Contrastive: Pair each good example with a short “bad” counterexample that highlights a common failure mode you don’t want.

  • Current & canonical: Prefer examples aligned with your team’s latest standards (lint rules, naming, section order).

  • Traceable: If you have a house style guide, cite it and show a minimal example that embodies it.

In many cases it’s also useful to link to a style-guide or documentation via a url or attachment. This allows you to provide large amounts of context without muddying up the prompt itself.

Technical Requirements

✏️ Complete the following steps to integrate few-shot into your prompt.

  1. Add good examples

    1. Select 2-3 examples that are dense and representative of what you want your output to look like. Each example should bring something new to the table and be structured in a way that’s easy for the AI to parse.

  2. Add bad examples

    1. 1-3 bad examples can make a big difference in the final output. Select examples that represent anti-patterns and that the AI can extrapolate from. Be sure to mark these explicitly as “bad”.

    2. Sometimes, it’s effective to pair each good example with a corresponding bad one

  3. Test your prompt

    1. Run the prompt and observe the output. Depending on the type of prompt you’re writing, you’ll want to try out different scenarios.

      1. Ex. If you’re writing a prompt to generate unit tests, and you include an example of mocking, you’ll want to have it generate a test that requires a mock.

Solution

Continuing from the previous section, we take our instructions.md prompt and add few-shot examples.

You are a senior full-stack engineer documenting a codebase for a new AI teammate.
Generate an instructions.md file that explains the purpose, architecture, dependencies, and coding conventions of the project.

Use the example outlines below to help shape your output. The final document should be detail-rich and include a variety of sections:

Good Example Outline:

  • Overview: Brief but informative project description

  • Architecture: Explanation of major components (frontend/backend, APIs, database)

  • Dependencies: Key frameworks, libraries, versions

  • Conventions: Folder structure, naming patterns, style guides

  • Setup Instructions (if relevant): Install, run, test steps

  • Common Pitfalls: Gotchas or non-obvious behaviors

Bad Example Outline:

  • Overview: "It’s a web app"

  • No architecture section

  • Lists dependencies with no context

  • Doesn’t explain folder structure or coding practices

  • No instructions or assumptions listed

Output Description:
Now the AI consistently produces a document with the correct sections and format. Notice how both the good and bad examples provide useful information.

What Changed

We now have:

  • Persona: defines who the AI is.

  • Examples: define what quality looks like.

The prompt now yields more consistent, stylistically correct results.

Next Steps

Your prompt now includes good and bad examples to help shape the AI’s output!

Continue to: https://bitovi.atlassian.net/wiki/spaces/AIEnabledDevelopment/pages/1620377674/Chain-of-Thought?atlOrigin=eyJpIjoiMjgyMzUzZjQ2ZDBmNGZkOThhZmI5MDM0ZDAwMTFlYjMiLCJwIjoiYyJ9

In the next step, you’ll help the AI think through its response and make your results even more accurate and comprehensive.

Sources

  • OpenAI official documentation
    Talks about the importance of few-shot prompting for fine-tuning the AI's output https://platform.openai.com/docs/guides/prompt-engineering/best-practices-for-prompting?utm_source=chatgpt.com#few-shot-learning

  • “Rethinking the Role of Demonstrations: What Makes In-Context Learning Work?” (Min et al., 2022).
    Finds that correct labels aren’t always required; demonstrations mainly help by showing label space, input distribution, and formatting. (Paper + EMNLP version.) arXiv+2ACL Anthology+2

  • Prompt Engineering Guide — Few-Shot Prompting.
    Practitioner-oriented explainer with compact examples showing how demonstrations steer output format and quality. Prompting Guide

  • DigitalOcean Community Tutorial — “Few-Shot Prompting: Techniques, Examples, and Best Practices.”
    Developer-friendly primer connecting few-shot prompting to ICL and giving simple patterns to implement. DigitalOcean

  • Additional practitioner explainer (AI Prompt Theory).
    Short article clarifying how a few curated examples can improve accuracy and relevance without fine-tuning. aiprompttheory.com