← LearnPrompting · Structure

Claude Few-Shot Examples for Sentiment Classification Tasks

To classify sentiment with Claude, include two to five labeled examples directly in your prompt before the review you want classified. Wrapping them in XML tags like <examples> locks in the exact output format—POSITIVE, NEUTRAL, or NEGATIVE—so Claude returns a consistent, machine-readable label every time.

What Are Few-Shot Examples for Sentiment Classification in Claude?

Few-shot prompting (also called multishot prompting) is a technique where you include a small number of carefully crafted input-output examples directly inside your prompt before your actual request. Instead of describing what you want in abstract terms, you show Claude concrete demonstrations — Claude reads the pattern from those examples and applies it to new inputs.

For sentiment classification specifically, this means providing two or three labeled customer reviews — each tagged POSITIVE, NEUTRAL, or NEGATIVE — before you paste in the review you actually want classified. The result is a consistent, single-word label rather than a freeform paragraph of commentary.

As Anthropic's multishot prompting documentation explains, showing Claude the input format, output structure, and tone you expect is generally more reliable than writing lengthy prose instructions. This technique works across every Claude interface — claude.ai chat, the desktop and mobile apps, the Anthropic API, and Claude Code — with no special settings or plan upgrades required.

How Do You Write a Few-Shot Sentiment Classification Prompt?

The structure is straightforward. You write a brief instruction, add your examples inside XML tags, then state the live input you want classified. Here is a complete, copy-paste-ready example:

Classify each review as POSITIVE, NEUTRAL, or NEGATIVE.

<examples>
<example>
<input>The packaging was fine but nothing special.</input>
<output>NEUTRAL</output>
</example>
<example>
<input>Absolutely love this product, will buy again!</input>
<output>POSITIVE</output>
</example>
<example>
<input>Broke after one day. Total waste of money.</input>
<output>NEGATIVE</output>
</example>
</examples>

Now classify: "Shipping was slow but the product itself is great."

Claude's expected output for that live input: POSITIVE.

Without the examples, Claude might return something like "This review has a mixed but overall positive tone." The examples lock in the exact single-word format needed for spreadsheet filtering or downstream automation.

How Do You Set Up Few-Shot Prompting Step by Step?

  1. Open any Claude interface. This works on claude.ai (web, desktop, or mobile), the Anthropic API, or Claude Code. No extra settings are needed.
  2. Write a brief instruction line. One sentence is enough: "Classify each review as POSITIVE, NEUTRAL, or NEGATIVE."
  3. Add an <examples> block. Use a parent <examples> tag containing individual <example> tags, each with <input> and <output> child tags.
  4. Include two to five diverse examples. Cover the full range of labels you expect — at least one POSITIVE, one NEUTRAL, and one NEGATIVE review.
  5. State the live input after the examples. A simple line like "Now classify: [review text]" is sufficient.
  6. Submit the prompt. No special flags, API parameters, or plan upgrades are required. Token usage counts toward your normal plan limits.
  7. For API use: include the examples as part of the content string in the user message, or pass them in the system prompt. No extra API parameters are required.

Why Does This Work Better Than Just Describing the Task?

The technique works because language models are highly sensitive to demonstrated patterns. When you show Claude two to five examples of the input format, reasoning style, output structure, and tone you expect, it can generalize that pattern to new inputs far more reliably than when you describe the same requirements in prose.

For sentiment classification, the practical benefit is format consistency. A product team that wants to label customer reviews in a machine-readable format needs the exact output tokens Claude produces to be predictable — POSITIVE, not "Positive sentiment detected" or "This is a positive review." Providing two or three labeled examples locks in those exact output tokens, eliminating freeform commentary.

XML tags play an important structural role here. Wrapping examples in <examples> containing individual <example> blocks lets Claude cleanly distinguish demonstration data from the live task, reducing the chance it treats an example review as the input to classify.

What Are the Most Common Pitfalls and How Do You Fix Them?

  • Too few examples that don't cover edge cases. Aim for three to five examples. Include at least one that demonstrates how to handle an unusual input — a review that is genuinely mixed, for instance.
  • Inconsistent format across examples. If your first example outputs a bare label and your second outputs a label with a colon, Claude will produce mixed results. Standardize every example's output format.
  • Examples that are too similar to each other. If all three examples are glowing five-star reviews, Claude may struggle with negative or neutral inputs. Make examples diverse — cover different input types and difficulty levels.
  • Too many examples with diminishing returns. Research from Anthropic indicates that generic examples on well-known topics show diminishing returns quickly. Curate a minimal but diverse set rather than padding with redundant examples.
  • No reasoning shown for complex edge cases. For tasks where the classification logic is non-obvious, embed <thinking> blocks inside your examples to demonstrate the reasoning chain. Claude will generalize the reasoning pattern to new inputs rather than just mirroring the surface output.
  • Examples too close to the live input. Keep examples realistic but distinct from your actual live input. The goal is to demonstrate a pattern, not to pre-solve the exact question you're asking.

When Should You Use Few-Shot Examples vs. Other Approaches?

Approach Use when… Skip when…
Few-shot examples You need a specific output format, consistent structure, or a tone that is hard to describe in prose. Especially useful for classification, extraction, and structured generation tasks. Your context window is tight and examples would crowd out important task content.
Zero-shot prompting The task is general enough that Claude already performs it well, or you need to minimize token usage for simple tasks. Output format consistency is critical — zero-shot often produces freeform responses.
System prompt instructions Your reasoning process can be captured in clear, unambiguous rules. The judgment call is nuanced and hard to articulate — demonstrated examples outperform prose descriptions in those cases.
Fine-tuning You have hundreds or thousands of labeled examples and need the behavior deeply embedded without consuming context window space on every call. Your dataset is small or you need behavior changes quickly without infrastructure work.

Does Few-Shot Prompting Work in the API and Claude Code?

Yes. Few-shot examples are not a special feature you toggle on — they are a prompting technique baked into how you write your prompt. They work across every Claude interface: claude.ai chat, the desktop and mobile apps, the Anthropic API, and Claude Code.

For API users, include the examples as part of the content string in the user message or pass them in the system prompt. No extra API parameters are required. Token usage counts toward your normal plan limits, but no extra settings or plan upgrades are needed to use the technique.

Claude Code users can embed few-shot examples in context files to give Claude consistent classification behavior across an entire project. Note that paid claude.ai subscriptions and the Claude API are separate products — a chat subscription does not include API access, which is billed separately by token usage.

Is Few-Shot Sentiment Classification Worth It for Production Pipelines?

For teams processing customer reviews at scale, the answer is almost always yes — with caveats. The technique requires no infrastructure changes, no fine-tuning dataset, and no additional API parameters. You can go from zero to consistent POSITIVE/NEUTRAL/NEGATIVE labels in a single prompt edit.

The tradeoff is token cost: every API call includes the full example block, which adds tokens on each request. For high-volume pipelines, you should benchmark whether a minimal two-example prompt performs as well as a five-example prompt for your specific review corpus. Curating a minimal but diverse set — rather than padding with redundant examples — keeps costs manageable while preserving classification accuracy.

For teams that need the behavior deeply embedded without consuming context window space on every call, fine-tuning is the longer-term path. But for most product teams getting started, few-shot examples deliver reliable, machine-readable sentiment labels with almost no setup cost.

Frequently asked questions

How many examples do I need for sentiment classification with Claude?

Aim for three to five examples that cover each label you expect — at least one POSITIVE, one NEUTRAL, and one NEGATIVE. Including an edge case (like a mixed review) helps Claude generalize rather than overfit to a narrow pattern.

Do I need a paid Claude plan to use few-shot prompting?

No. Few-shot prompting is a technique baked into how you write your prompt. It works on Free, Pro, Max, Team, Enterprise, Claude Code, and the API. No special settings or plan upgrades are required, though token usage counts toward your normal plan limits.

Why should I use XML tags in my few-shot prompt?

Wrapping examples in <examples> tags containing individual <example> blocks with <input> and <output> child tags lets Claude cleanly distinguish demonstration data from the live task, reducing the chance it confuses an example review with the input you want classified.

What if Claude still returns a full sentence instead of a single label?

Check that your output format is consistent across all examples — every example output should be a bare label with no extra punctuation or explanation. You can also add an explicit instruction like 'Respond with only the label, nothing else' before the examples block.

Can I use few-shot examples in the Anthropic API for batch sentiment classification?

Yes. Include the examples as part of the content string in the user message or in the system prompt. No extra API parameters are required. Keep in mind that the API is billed separately from claude.ai subscriptions.

Should I show reasoning steps in my sentiment classification examples?

For straightforward three-label classification, bare input-output examples are usually sufficient. For nuanced tasks where the classification logic is non-obvious, embedding <thinking> blocks inside your examples teaches Claude the reasoning chain rather than just the surface label.

Go deeper

Few-shot examples is one of 85 features in Claude Master — the independent, continuously updated manual with worked examples, the pitfalls, and the workflows that put Claude to work.

Get Claude Master — founding price

Independent product. Not affiliated with or endorsed by Anthropic. "Claude" is a trademark of Anthropic, used here only to describe the subject of this guide.