0PricingLogin
AI Prompt Engineering · Lesson

LLM as Text Classifier

Using prompts for sentiment, intent, topic, and multi-label classification.

LLMs as Text Classifiers

Traditional text classification requires labeled training data, model fine-tuning, and deployment infrastructure. LLMs can classify text with just a prompt — no training data required.

LLM-based classifiers excel when:

  • The categories require semantic understanding (not just keyword matching)
  • You need to add new categories without retraining
  • You have limited labeled examples
  • The categories are nuanced (intent behind a message, not just its topic)

Sentiment Classification

Sentiment is one of the most common classification tasks. A well-crafted prompt outperforms simple keyword matching for nuanced cases:

import anthropic, json

client = anthropic.Anthropic(api_key='YOUR_API_KEY')

def classify_sentiment(text):
    prompt = f'''
Classify the sentiment of the text below.
Return ONLY JSON: {{"sentiment": "positive|negative|neutral", "confidence": "high|medium|low"}}

Definitions:
- positive: Overall favorable opinion or emotion
- negative: Overall unfavorable opinion or dissatisfaction
- neutral: Factual, balanced, or no clear sentiment

Text: {text}
'''
    r = client.messages.create(
        model='claude-opus-4-5', max_tokens=50,
        messages=[{'role': 'user', 'content': prompt}]
    )
    return json.loads(r.content[0].text)

print(classify_sentiment('The product works but setup was painful.'))
print(classify_sentiment('Delivery was incredibly fast and packaging was perfect!'))

All lessons in this course

  1. Named Entity Extraction Prompts
  2. Schema-Driven Data Extraction
  3. LLM as Text Classifier
  4. Confidence and Uncertainty in Classification
← Back to AI Prompt Engineering