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.
Output:
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.
Output:
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.
Output:

Pattern 4: Multi-Label Classification

When items can belong to multiple categories simultaneously.
Output:

Advanced Techniques

Confidence Scoring

Ask the model to provide confidence levels with classifications.
Output:

Hierarchical Classification

For complex taxonomies, use a two-stage approach.
Output:

Zero-Shot with Definitions

Provide clear definitions when examples aren’t available.
Output:

Handling Edge Cases

Ambiguous Cases

Output:

Unknown Categories

Output:

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
Problem: Asking the model to choose from 20+ categoriesSolution: Use hierarchical classification or group related categories
Problem: Model returns “positive”, “Positive”, “POSITIVE”, or “The sentiment is positive”Solution: Explicitly constrain the output format

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”

Exercise 2: Content Moderation

Design a prompt to classify social media posts as Safe, Review, or Unsafe based on content policy.

Exercise 3: Multi-Label Classification

Create a prompt to tag news articles with relevant topics (can have multiple tags).

Real-World Application: Email Triage System

Let’s build a complete email classification system:
Output:

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