Skip to main content
Duration: 60 minutes

Introduction

Text classification is one of the most common NLP tasks—from sentiment analysis to content moderation. While LLMs are primarily designed to generate text, we can leverage them for classification by carefully structuring our prompts. In this lesson, you’ll learn proven patterns to get reliable, consistent classifications.

The Classification Challenge

LLMs generate text probabilistically, which creates unique challenges for classification:
Common Issues:
  • Inconsistent output formats
  • Verbose explanations when you need labels
  • Hallucinated categories
  • Difficulty with edge cases
The solution? Constrained output patterns that guide the model toward structured responses.

Core Classification Patterns

Pattern 1: Direct Label Classification

The simplest approach—provide examples and ask for a label.
Classify the sentiment of the following text as Positive, Negative, or Neutral.

Text: "The product exceeded my expectations!"
Sentiment:
Output:
Positive
Best for: Simple binary or multi-class classification with clear boundaries

Pattern 2: Few-Shot Classification

Provide examples to establish the pattern and decision boundaries.
Classify customer feedback into categories.

Example 1: "Fast shipping, great quality" → Category: Product Quality
Example 2: "Customer service was unhelpful" → Category: Customer Service
Example 3: "Website is hard to navigate" → Category: User Experience

Now classify: "The checkout process was confusing"
Category:
Output:
User Experience
Few-shot examples help the model understand:
  • The classification criteria
  • Edge case handling
  • Output format expectations

Pattern 3: Constrained Output with Reasoning

Get both the classification and the reasoning behind it.
Classify the following email as Spam or Not Spam.
Provide your reasoning, then give the final classification.

Email: "Congratulations! You've won $1,000,000! Click here to claim your prize now!"

Reasoning:
Classification:
Output:
Reasoning: This email contains typical spam indicators: unrealistic prize claim, urgency tactics, and suspicious links without context.
Classification: Spam

Pattern 4: Multi-Label Classification

When items can belong to multiple categories simultaneously.
Classify the following article into one or more categories: Technology, Business, Health, Politics, Entertainment.

Article: "Apple announces new health features in latest iPhone, boosting stock prices."

Categories (comma-separated):
Output:
Technology, Business, Health

Advanced Techniques

Confidence Scoring

Ask the model to provide confidence levels with classifications.
Classify the sentiment and provide a confidence score (0-100).

Text: "The product is okay, nothing special."

Sentiment:
Confidence:
Output:
Sentiment: Neutral
Confidence: 85

Hierarchical Classification

For complex taxonomies, use a two-stage approach.
First, classify the broad category, then the specific subcategory.

Text: "The laptop's battery life is disappointing."

Step 1 - Broad Category (Product, Service, Shipping, Other):
Step 2 - Specific Issue (if Product: Quality, Performance, Design, Price):
Output:
Step 1 - Broad Category: Product
Step 2 - Specific Issue: Performance

Zero-Shot with Definitions

Provide clear definitions when examples aren’t available.
Classify the following text using these definitions:

- Informative: Provides factual information or explanations
- Persuasive: Attempts to convince or influence opinion
- Entertaining: Primarily for amusement or enjoyment
- Instructional: Teaches how to do something

Text: "Follow these steps to bake the perfect chocolate cake."

Classification:
Output:
Instructional

Handling Edge Cases

Ambiguous Cases

Classify the sentiment. If ambiguous, explain why and provide the most likely classification.

Text: "The service was fast, but the food was cold."

Analysis:
Classification:
Output:
Analysis: Mixed sentiment - positive about speed, negative about quality. The negative aspect (core product) likely outweighs the positive (service speed).
Classification: Negative

Unknown Categories

Classify into: Bug Report, Feature Request, Question, or Other.
If "Other", briefly explain.

Text: "I love the new design!"

Classification:
Explanation (if Other):
Output:
Classification: Other
Explanation: This is positive feedback/praise, not fitting the technical support categories.

Best Practices

Be Explicit

Clearly define all possible categories and their boundaries

Use Examples

Provide 2-5 examples per category when possible

Constrain Output

Specify exact format: “Respond with only: Category A, Category B, or Category C”

Handle Edge Cases

Include instructions for ambiguous or out-of-scope inputs

Common Pitfalls

Problem: “Classify as good or bad”Solution: Define what makes something “good” or “bad” in your context
Classify product reviews as Positive (recommends product, satisfied) or Negative (dissatisfied, would not recommend).
Problem: Asking the model to choose from 20+ categoriesSolution: Use hierarchical classification or group related categories
First classify as: Product, Service, or Shipping
Then provide specific subcategory based on the first classification
Problem: Model returns “positive”, “Positive”, “POSITIVE”, or “The sentiment is positive”Solution: Explicitly constrain the output format
Respond with exactly one word: Positive, Negative, or Neutral

Practice Exercises

Exercise 1: Sentiment Analysis

Create a prompt to classify movie reviews as Positive, Negative, or Mixed. Test with:
  • “Best film I’ve seen this year!”
  • “Terrible acting, waste of time”
  • “Great visuals but weak plot”
Classify the movie review sentiment as exactly one of: Positive, Negative, or Mixed.

Positive: Overall recommendation, more positive than negative aspects
Negative: Overall not recommended, more negative than positive aspects  
Mixed: Balanced positive and negative aspects

Review: [INSERT REVIEW]
Sentiment:

Exercise 2: Content Moderation

Design a prompt to classify social media posts as Safe, Review, or Unsafe based on content policy.
Classify social media content according to these categories:

Safe: Appropriate for all audiences, no policy violations
Review: Potentially problematic, needs human review (borderline cases)
Unsafe: Clear policy violation (hate speech, violence, explicit content)

Post: [INSERT POST]
Classification:
Reason:

Exercise 3: Multi-Label Classification

Create a prompt to tag news articles with relevant topics (can have multiple tags).
Tag the following news article with all relevant categories from this list:
Politics, Technology, Business, Health, Science, Sports, Entertainment, Environment

Provide tags as a comma-separated list.

Article: [INSERT ARTICLE]
Tags:

Real-World Application: Email Triage System

Let’s build a complete email classification system:
You are an email triage assistant. Classify emails into categories and assign priority.

Categories:
- Sales: Customer inquiries about products/services
- Support: Technical issues or help requests  
- Billing: Payment, invoices, refunds
- Feedback: Suggestions, complaints, reviews
- Spam: Unsolicited or irrelevant messages

Priority Levels:
- High: Urgent issues, angry customers, time-sensitive
- Medium: Standard requests, general inquiries
- Low: Feedback, non-urgent questions

Email: "I was charged twice for my subscription this month. Please refund the duplicate charge immediately."

Category:
Priority:
Reasoning:
Output:
Category: Billing
Priority: High
Reasoning: Billing error requiring immediate attention. Customer expects prompt resolution of duplicate charge.

Key Takeaways

Constrain outputs by explicitly listing valid categories
Use few-shot examples to establish classification boundaries
Handle edge cases and ambiguous inputs explicitly
Consider hierarchical classification for complex taxonomies
Request reasoning when you need explainable classifications

Next Steps

Now that you can classify text reliably, you’re ready to extract structured information from unstructured content.

Next: Lesson 2.2 - Information Extraction Prompts

Learn to pull structured data from unstructured text
I