Back to cookbook

AI Prompt for Few-Shot Prompting to Get Consistent, Correctly Formatted Output

2 views Updated
Share

This is a few-shot prompting prompt: a template for teaching ChatGPT, Claude, or Gemini the exact output format you want by showing it two or three worked examples before asking it to handle a new case. Instead of describing the format in abstract rules ("use a table", "keep it under 50 words"), you show the model what a correct input-output pair looks like, and it pattern-matches to that structure on the next input. This is useful for anyone who needs repeatable, consistently formatted output across many similar requests — support ticket tagging, product data extraction, log classification, or any task run in a loop or pipeline where format drift breaks downstream code.

Few-shot prompting works because large language models are strong at in-context pattern completion: given a handful of consistent examples, they infer the implicit schema (field order, tone, length, delimiters) more reliably than from a written specification alone. The tradeoff is prompt length and token cost — each example adds tokens on every call — and the examples themselves have to be genuinely representative, including at least one edge case, or the model will confidently misapply the pattern to inputs that do not fit it.

Before running this at scale, it is worth checking that the few-shot examples themselves do not contain ambiguous labels or inconsistent formatting between them, since the model will faithfully copy any inconsistency it sees; running the finished prompt through Prompt Debugger will flag mismatched examples and vague output constraints before you burn a batch of API calls on a flawed pattern.

Prompt template

Role: You are a [TASK TYPE, e.g. support ticket classifier / data extractor / content tagger].

Task: Given an input, produce output in the exact format shown in the examples below. Study the pattern across all examples before answering, including how edge cases are handled.

Example 1 Input: [EXAMPLE INPUT 1] Output: [EXAMPLE OUTPUT 1]

Example 2 Input: [EXAMPLE INPUT 2] Output: [EXAMPLE OUTPUT 2]

Example 3 (edge case: [DESCRIBE WHAT MAKES THIS ONE TRICKY]) Input: [EXAMPLE INPUT 3] Output: [EXAMPLE OUTPUT 3]

Constraints:

  • Match the exact structure, field names, and formatting shown above
  • If the new input does not clearly fit the pattern, output [FALLBACK VALUE, e.g. "UNCLEAR"] instead of guessing
  • Do not add commentary, explanation, or text outside the output format

Now classify this new input using the same format: Input: [NEW INPUT TO PROCESS] Output:

Example input

Role: You are a customer support ticket classifier.

Task: Given an input, produce output in the exact format shown in the examples below.

Example 1 Input: "My card was charged twice for the same order." Output: {"category": "billing", "priority": "high"}

Example 2 Input: "How do I change the email on my account?" Output: {"category": "account", "priority": "low"}

Example 3 (edge case: message mentions both a bug and a billing issue) Input: "The app crashed right after I was billed, not sure if I actually got charged." Output: {"category": "billing", "priority": "medium"}

Constraints:

  • Match the exact structure and field names shown above
  • If the new input does not clearly fit the pattern, output {"category": "unclear", "priority": "low"} instead of guessing
  • Do not add commentary, explanation, or text outside the output format

Now classify this new input using the same format: Input: "I was charged for the annual plan but I only ever used the free trial." Output:

Example output

{"category": "billing", "priority": "high"}

When to use it

  • You need the same output structure (JSON fields, a table, a fixed tag set) across hundreds of similar inputs run through an API or script
  • A plain instruction keeps producing inconsistent formatting, field order, or length from one run to the next
  • The task has a few tricky edge cases that are hard to describe in words but easy to show by example
  • You are building a classification or extraction step in a pipeline where downstream code parses the model's output and cannot tolerate format drift

Best practices

  • Use 2-4 examples, not one: a single example lets the model guess at a rule that does not generalize, while 2-4 examples pin down the actual pattern
  • Include at least one edge case or boundary example (an empty field, an unusual input, a tie-breaker) so the model learns the pattern's limits, not just the easy case
  • Keep every example in the identical format you want back, down to punctuation and field names, since the model copies surface formatting as literally as it copies structure
  • Order examples from simplest to most complex, and put the real input last, right after the final example, so it reads as the next item in the same list rather than a separate question

Common mistakes

  • Using only one example, which teaches a format but not its boundaries, so the model overfits to that single case
  • Writing inconsistent formatting between the examples themselves (different date formats, mismatched field names), which the model then reproduces as noise
  • Making examples too similar to each other, leaving the model to guess how to handle inputs that differ meaningfully from all of them
  • Forgetting to show the exact delimiter or wrapper (quotes, code fences, JSON braces) you want in the final answer, so the model picks its own

FAQs

What is few-shot prompting?

Few-shot prompting is a technique where you include a small number of example input-output pairs directly in the prompt before asking the model to handle a new case. The model uses those examples to infer the format, tone, and structure it should follow, rather than relying only on a written instruction.

How many examples should I use in a few-shot prompt?

Most tasks work well with 2-4 examples. One example is usually not enough because the model cannot tell which details of that single example are the actual rule versus incidental. More than 4-5 examples adds token cost without much added accuracy for most straightforward formatting tasks.

Does few-shot prompting work the same way on ChatGPT, Claude, and Gemini?

Yes, in-context learning from examples is a general capability of current large language models, so the same example-based prompt structure works across ChatGPT, Claude, and Gemini. Exact output consistency can still vary slightly between models, so it is worth testing the finished prompt on whichever model you will actually run in production.

Why does my few-shot prompt still produce inconsistent output?

This usually means the examples themselves are inconsistent with each other, too similar to cover real variation in your inputs, or missing an edge case that shows up in production. Checking that every example matches the exact structure you want, and adding one deliberately tricky example, fixes most inconsistency.

Which Cuelara tool can help me check this few-shot prompt before running it at scale?

Prompt Debugger — it scans a prompt for vague constraints, inconsistent examples, and edge cases the model is likely to mishandle, which is exactly where few-shot prompts tend to break down before a full production run.

Found this prompt useful? Share it.

Share

More in Prompt Fundamentals & Techniques

Prompt Fundamentals & Techniques

AI Prompt for Self-Critique and Refinement Before a Final Answer

This AI prompt for self critique and refinement is for anyone who wants a model to check its own draft answer for gaps, errors, or unsupport…

Role: You are an assistant that produces a draft answer, critiques it against explicit criteria, and then revises it.

Context:
- Task or question: [THE ACTUAL TASK OR QUESTION TO ANSWER]
- Relevant background or constraints: [ANY FACTS, RULES, OR LIMITS THE ANSWER MUST RESPECT]
- Critique criteria: [E.G. FACTUAL ACCURACY, COMPLETENESS, UNSUPPORTED ASSUMPTIONS, CLARITY]

Instructions:
1. Write a first draft answer to the task above.
2. Critique the draft explicitly against each criterion listed. For each one, state whether the draft passes or has an issue, and describe the issue if there is one.
3. Write a revised final answer that addresses every issue found in step 2.

Constraints:
- Do not skip stating an issue just because it would require significant rework
- Do not present the draft and the revision as identical if real issues were found
- If no issues are found for a criterion, say so explicitly rather than omitting it

Output format:
1. Draft Answer
2. Critique (one line per criterion)
3. Revised Final Answer
Prompt Fundamentals & Techniques

ChatGPT Prompt for Reliable Function and Tool Calling

This is a function and tool calling prompt for developers wiring ChatGPT, Claude, or Gemini into an app that has real tools available — a se…

You are an assistant with access to the following tools:

[TOOL_1_NAME]: [ONE-LINE DESCRIPTION OF WHAT IT DOES AND WHEN TO USE IT]
[TOOL_2_NAME]: [ONE-LINE DESCRIPTION OF WHAT IT DOES AND WHEN TO USE IT]

Before responding to the user, work through these steps:

1. Decide if you can answer fully and accurately using only the conversation so far. If yes, answer directly and do not call any tool.
2. If a tool is genuinely needed, identify which single tool is the best fit. Do not call a tool "just in case."
3. List the required arguments for that tool. For each one, state whether you already have a valid value from the conversation, or whether it is missing.
4. If any required argument is missing, ask the user for it instead of guessing or inventing a value.
5. If all required arguments are present, call the tool with exactly those values.

Context: [DESCRIBE THE USER'S REQUEST OR TASK HERE]

Respond now, following the steps above.
Prompt Fundamentals & Techniques

Chain-of-Thought Prompt for Step-by-Step Reasoning

This is a chain of thought prompt that gets ChatGPT, Claude, or Gemini to reason through a problem step by step before giving a final answer…

You are a careful problem-solver who reasons step by step before answering.

Problem: [DESCRIBE THE PROBLEM OR QUESTION]

Relevant facts or constraints: [LIST ANY NUMBERS, RULES, OR CONSTRAINTS]

Instructions:
1. Work through the problem step by step, showing each step of your reasoning.
2. Double-check any calculation before moving to the next step.
3. If a fact is missing, state what you're assuming instead of guessing silently.
4. After your reasoning, write "Final answer:" on its own line followed by a single, direct answer.

Output format:
Reasoning:
[step-by-step reasoning]

Final answer:
[one direct answer]