Dev.to AI 🤖 Ai 👁 0 📖 3 min read

Using LLM for Sentiment Analysis: A Step-by-Step Guide

Sentiment analysis remains one of the most common production tasks for large language models. Unlike classical pipeline approaches that require task-specific training data and fixed lexicons, an LLM can adapt to domain-s

Sentiment analysis remains one of the most common production tasks for large language models. Unlike classical pipeline approaches that require task-specific training data and fixed lexicons, an LLM can adapt to domain-specific language, nuanced emotional tones, and multi-aspect ratings from a single prompt. This guide walks through a practical implementation, from prompt design to structured output parsing, using tools that fit directly into existing engineering stacks.

Select a model and inference backend

For sentiment classification, you do not always need the largest frontier model. A capable mid-size model such as Qwen 3 32B or Llama 3.3 70B on Oxlo.ai handles nuanced classification well, while DeepSeek R1 671B MoE is useful when you need explicit chain-of-thought reasoning before the final label. Because Oxlo.ai charges a flat rate per API request rather than per token, analyzing long customer reviews or running few-shot prompts with extended examples does not inflate cost. You can explore model options and plans on the Oxlo.ai pricing page. The platform is fully OpenAI SDK compatible, so switching endpoints requires only a base URL change.

Craft the prompt

A good sentiment prompt defines the label set, the input text, and any formatting rules. For product reviews, a three-class schema (positive, neutral, negative) is usually sufficient, but you can add confidence scores or aspect-based labels.

Example prompt template:

You are a sentiment classifier. Read the product review below and classify its overall sentiment as one of: positive, neutral, negative.
Return a JSON object with keys: "sentiment" (string), "confidence" (integer 1-10), "explanation" (string, max 20 words).
Review: """{{review_text}}"""

Enforce structured output with JSON mode

Instead of parsing free text, use JSON mode to guarantee valid output. Oxlo.ai supports JSON mode and streaming responses across its chat models. Here is a minimal Python example using the OpenAI SDK.

import os
from openai import OpenAI

client = OpenAI(
    base_url="https://api.oxlo.ai/v1",
    api_key=os.getenv("OXLO_API_KEY")
)

def classify_sentiment(review: str, model: str = "llama-3.3-70b"):
    response = client.chat.completions.create(
        model=model,
        messages=[
            {"role": "system", "content": "You are a sentiment classifier."},
            {"role": "user", "content": f"""Classify the review sentiment as positive, neutral, or negative.
Return JSON with keys: sentiment, confidence (1-10), explanation.
Review: \"\"\"{review}\"\"\"
"""}
        ],
        response_format={"type": "json_object"},
        temperature=0.1,
    )
    return response.choices[0].message.content

review = "The battery life is outstanding, but the camera struggles in low light."
result = classify_sentiment(review)
print(result)

Setting temperature low reduces variance, which is important for consistent classification.

Handle long-context inputs

Customer feedback often arrives as lengthy support tickets or multi-page survey responses. On token-based platforms, long inputs raise cost linearly. Oxlo.ai's request-based pricing removes that penalty, so sending a full 131K context window with Kimi K2.6 or a 1M context with DeepSeek V4 Flash costs the same flat rate regardless of input length. This makes long-document sentiment extraction predictable for budgeting.

Batch for throughput

If you process high volumes, submit requests concurrently and keep individual prompts focused. Because there are no cold starts on popular Oxlo.ai models, latency stays consistent even under load. You can also use function calling if you want the model to emit structured records that trigger downstream tools automatically.

Evaluate rigorously

Build a small labeled evaluation set representative of your domain. Measure accuracy, precision, and recall per class. If the model confuses neutral and negative on terse responses, add two or three examples in a few-shot prompt. Iterate on the system message before increasing model size. Oxlo.ai gives you access to 45 plus models across categories, so you can A/B test a fast coder like Oxlo.ai Coder Fast against a reasoning model like DeepSeek R1 without rewriting client code.

Conclusion

LLM-based sentiment analysis replaces brittle pipelines with promptable, interpretable classifiers. By combining structured generation, careful prompting, and a flat-cost inference backend, you can ship a robust solution without token math. Oxlo.ai fits naturally into this workflow through OpenAI SDK compatibility, broad model choice, and request-based pricing that stays cheap as your inputs grow. Start with the free tier to prototype, then scale on a plan that matches your daily request volume.

📰 Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.