Claude Sentiment Analysis with Python

Working Python code to run sentiment analysis with the Claude API in 2026. Zero-shot classifier, custom sentiment scales, aspect-based analysis, and batch processing patterns.

💥 50p impulse-buy: Power Prompts PDF (first 10 buyers) 30 battle-tested Claude Code prompts · 8-page PDF · paste into CLAUDE.md and never re-type a prompt again · 50p impulse-buy, no commitment

Claude's language understanding makes it an accurate zero-shot sentiment classifier — no training data or fine-tuning required. This guide covers every sentiment analysis pattern from simple positive/negative to aspect-level JSON extraction.

Installation

pip install anthropic

Simple positive / negative / neutral classifier

import anthropic
import json

client = anthropic.Anthropic()

def analyze_sentiment(text: str) -> dict:
    response = client.messages.create(
        model="claude-haiku-4-5-20251001",  # cheapest model — sufficient for sentiment
        max_tokens=128,
        temperature=0,  # deterministic output
        system=(
            "You are a sentiment classifier. "
            "Return ONLY valid JSON with this schema: "
            '{"sentiment": "positive"|"negative"|"neutral", "confidence": 0.0-1.0}'
        ),
        messages=[{"role": "user", "content": f"Classify the sentiment of: {text}"}],
    )
    return json.loads(response.content[0].text)

# Example
result = analyze_sentiment("The delivery was fast but the product broke after two days.")
print(result)
# {"sentiment": "negative", "confidence": 0.82}

Five-point sentiment scale with rationale

def analyze_sentiment_detailed(text: str) -> dict:
    response = client.messages.create(
        model="claude-haiku-4-5-20251001",
        max_tokens=256,
        temperature=0,
        system=(
            "You are a sentiment analyst. Return ONLY valid JSON: "
            '{"score": 1-5, "label": "very negative"|"negative"|"neutral"|"positive"|"very positive", '
            '"confidence": 0.0-1.0, "rationale": "one sentence"}'
        ),
        messages=[{"role": "user", "content": text}],
    )
    return json.loads(response.content[0].text)

result = analyze_sentiment_detailed(
    "I've been using this app for months. It's mostly fine but crashes weekly and support ignores tickets."
)
print(result)
# {"score": 2, "label": "negative", "confidence": 0.88,
#  "rationale": "Crashes and unresponsive support outweigh functional adequacy."}

Aspect-based sentiment analysis (ABSA)

def aspect_sentiment(text: str, aspects: list[str]) -> dict:
    aspect_list = ", ".join(aspects)
    response = client.messages.create(
        model="claude-sonnet-4-6",  # better reasoning for multi-aspect
        max_tokens=512,
        temperature=0,
        system=(
            "You are an aspect-based sentiment analyst. "
            "Return ONLY valid JSON: a dict mapping each aspect to "
            '{"sentiment": "positive"|"negative"|"neutral"|"not_mentioned", "evidence": "quote or null"}'
        ),
        messages=[{
            "role": "user",
            "content": f"Aspects to evaluate: {aspect_list}

Review: {text}"
        }],
    )
    return json.loads(response.content[0].text)

review = (
    "The camera takes stunning photos in daylight but struggles at night. "
    "Battery lasts two days easily. The price is steep for what you get."
)
result = aspect_sentiment(review, ["camera", "battery", "price", "design"])
# {
#   "camera": {"sentiment": "positive", "evidence": "takes stunning photos in daylight"},
#   "battery": {"sentiment": "positive", "evidence": "lasts two days easily"},
#   "price": {"sentiment": "negative", "evidence": "steep for what you get"},
#   "design": {"sentiment": "not_mentioned", "evidence": null}
# }

Batch sentiment analysis (50% cost with Batch API)

import anthropic

client = anthropic.Anthropic()

reviews = [
    "Great product, fast shipping!",
    "Broke after a week. Very disappointed.",
    "Average quality, nothing special.",
    # ... thousands more
]

# Build batch requests
requests = [
    {
        "custom_id": f"review-{i}",
        "params": {
            "model": "claude-haiku-4-5-20251001",
            "max_tokens": 64,
            "temperature": 0,
            "system": 'Return ONLY JSON: {"sentiment": "positive"|"negative"|"neutral"}',
            "messages": [{"role": "user", "content": text}],
        },
    }
    for i, text in enumerate(reviews)
]

# Submit batch (50% cheaper than real-time, processes within 24h)
batch = client.messages.batches.create(requests=requests)
print(f"Batch ID: {batch.id}")  # poll this later

# Poll for results
import time
while True:
    status = client.messages.batches.retrieve(batch.id)
    if status.processing_status == "ended":
        break
    time.sleep(60)

results = {}
for result in client.messages.batches.results(batch.id):
    if result.result.type == "succeeded":
        data = json.loads(result.result.message.content[0].text)
        results[result.custom_id] = data["sentiment"]

print(results)

Domain-specific sentiment (financial / medical)

def financial_sentiment(headline: str) -> dict:
    """Classify financial news headlines as bullish / bearish / neutral."""
    response = client.messages.create(
        model="claude-haiku-4-5-20251001",
        max_tokens=64,
        temperature=0,
        system=(
            "You are a financial news sentiment analyst. "
            'Return ONLY JSON: {"sentiment": "bullish"|"bearish"|"neutral", "confidence": 0.0-1.0}'
        ),
        messages=[{"role": "user", "content": headline}],
    )
    return json.loads(response.content[0].text)

print(financial_sentiment("Fed signals rate pause amid cooling inflation"))
# {"sentiment": "bullish", "confidence": 0.79}

Claude vs VADER vs TextBlob comparison

MethodAccuracy (nuanced)CostSpeedBest for
Claude HaikuHigh — handles irony, context, domain jargon$0.25/M tokens~200ms/callReviews, support tickets, financial news
VADERLow-medium — fails on sarcasmFree<1msSocial media, simple positive/negative volume
TextBlobLow — English only, lexicon-basedFree<1msBasic prototyping only
Fine-tuned BERTHigh in-domain, low out-of-domainGPU cost~20ms (GPU)Single domain with >10K labeled examples

Use the Claude API Cost Calculator to estimate cost at your review volume. For a general-purpose classifier (multi-class, not just sentiment), see the text classification guide. For bulk document summarization, see the summarization guide.

Frequently asked questions

Can Claude do sentiment analysis without training data?
Yes. Claude performs zero-shot sentiment classification — just describe your sentiment categories in the prompt. It typically outperforms fine-tuned BERT on nuanced or domain-specific text (legal, medical, financial) where labeled training data is scarce.
How do I get structured sentiment output from Claude?
Ask Claude to return a JSON object and set `temperature=0` for determinism. Use a system prompt like 'Return only valid JSON with keys: sentiment, confidence, rationale.' Then parse with `json.loads(response.content[0].text)`.
What is aspect-based sentiment analysis and how do I do it with Claude?
Aspect-based sentiment analysis (ABSA) extracts sentiment for specific attributes of a product or service (e.g., 'battery life: negative, camera: positive'). Ask Claude to extract a list of aspects and their sentiments in a single prompt — it handles this in one pass without a pipeline.
How fast and cheap is Claude for sentiment analysis at scale?
Claude Haiku (claude-haiku-4-5-20251001) is the most cost-effective: ~$0.25 per million input tokens. For bulk classification, use the Batch API (50% discount) — 1 million short reviews costs under $0.15. A 10-token review + 20-token output = $0.000000375 per item.
How does Claude sentiment analysis compare to VADER or TextBlob?
VADER and TextBlob use rule-based lexicons — fast and free but fail on sarcasm, domain jargon, and multi-sentence context. Claude understands context, irony, and domain-specific language but costs money per call. Use VADER for simple high-volume cases; Claude for accuracy where it matters.

Free tools

Cost Calculator → API Cookbook → Diff Summarizer → Skills Browser →

More examples

Claude API Python QuickstartClaude API Node.js / TypeScript QuickstartClaude API Streaming in PythonClaude API Streaming in Node.js / TypeScriptClaude API Tool Use in PythonClaude API Tool Use in Node.js / TypeScript