DEV Community

shashank ms
shashank ms

Posted on

Using LLM for Sentiment Analysis with Long Context

Support teams and product managers often need sentiment from hundreds of reviews or a long ticket thread. Chunking that text across multiple LLM calls adds latency and cost. In this tutorial, I will show you how to build a single-request sentiment analyzer using Oxlo.ai that ingests an entire batch of feedback at once, with no price penalty for long inputs.

What you'll need

Step 1: Initialize the Oxlo.ai client

Set up the OpenAI SDK to point at Oxlo.ai. This is a drop-in replacement, so the only differences are the base URL and the API key.

from openai import OpenAI

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

Step 2: Define the system prompt

A strict system prompt keeps the model honest when the input grows. I force JSON output and require an entry for every review ID so nothing gets dropped from the batch.

SYSTEM_PROMPT = """You are a batch sentiment analysis engine.
Analyze every customer review in the input and return valid JSON with this structure:
{
  "overall_sentiment": "positive|negative|neutral|mixed",
  "confidence": 0.0-1.0,
  "reviews": [
    {
      "id": number,
      "sentiment": "positive|negative|neutral",
      "key_quote": "verbatim snippet",
      "topics": ["topic1", "topic2"]
    }
  ],
  "themes": ["theme1", "theme2"]
}
Rules:
- Use the review ID exactly as provided in the input.
- Include every review. Do not skip entries.
- Key quotes must be exact text from the review.
- Topics should be one or two words."""

Step 3: Prepare the long-context input

I concatenate twenty app-store reviews into one string. With Oxlo.ai, sending this entire batch costs the same as sending a single review, so you can keep adding text until you approach the model's context window.

REVIEWS_BATCH = """Review #1: This app is fantastic. The new export feature saved me hours last week.
Review #2: Terrible update. Crashes every time I open a project larger than 50 MB.
Review #3: Decent tool, but the pricing is confusing and the docs could be clearer.
Review #4: Absolutely love the dark mode. Finally my eyes do not hurt during late night sessions.
Review #5: Support never answered my ticket. Waited six days and gave up.
Review #6: Smooth onboarding, great tutorials. My team was productive within an hour.
Review #7: It is okay. Nothing special compared to competitors.
Review #8: The API is rock solid. Webhooks fire instantly and the retry logic is reliable.
Review #9: UI feels dated. Too many clicks to run a simple report.
Review #10: Best investment we made this quarter. Revenue is up because we ship faster.
Review #11: Login is buggy on Safari. Had to switch to Chrome just to access my dashboard.
Review #12: Good features, but performance is slow with large datasets.
Review #13: The collaboration tools are a game changer. Real time editing works flawlessly.
Review #14: I regret the purchase. The refund process is opaque and frustrating.
Review #15: Neutral experience. It works, but I expected more automation.
Review #16: Security settings are robust. SSO integration took five minutes.
Review #17: Mobile app is unusable. Menus overlap and buttons do not respond.
Review #18: Outstanding documentation. Every endpoint has a working example.
Review #19: Average. Does what it says, but the learning curve is steep.
Review #20: Love the weekly summary emails. Keeps my stakeholders informed without extra work."""

Step 4: Send the request and parse JSON output

I use kimi-k2.6 because its 131K context window easily absorbs large batches of feedback, and its reasoning capabilities catch nuanced sentiment. JSON mode locks the output format.

import json
from openai import OpenAI

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

response = client.chat.completions.create(
    model="kimi-k2.6",
    messages=[
        {"role": "system", "content": SYSTEM_PROMPT},
        {"role": "user", "content": REVIEWS_BATCH},
    ],
    response_format={"type": "json_object"},
    temperature=0.1,
)

raw = response.choices[0].message.content
result = json.loads(raw)

Step 5: Aggregate and display results

Raw JSON is useful, but most teams want a quick summary. This helper prints counts and surfaces the strongest quotes.

def print_summary(data):
    sentiments = [r["sentiment"] for r in data["reviews"]]
    counts = {"positive": 0, "negative": 0, "neutral": 0}
    for s in sentiments:
        counts[s] = counts.get(s, 0) + 1

    print(f"Overall: {data['overall_sentiment']} (confidence: {data['confidence']})")
    print(f"Counts: {counts}")
    print(f"Themes: {', '.join(data['themes'])}")
    print("\nTop positives:")
    for r in data["reviews"]:
        if r["sentiment"] == "positive":
            print(f"  Review #{r['id']}: {r['key_quote'][:60]}...")
    print("\nTop negatives:")
    for r in data["reviews"]:
        if r["sentiment"] == "negative":
            print(f"  Review #{r['id']}: {r['key_quote'][:60]}...")

print_summary(result)

Run it

Save the full script as sentiment_batch.py, export your key, and run it. Here is the output I get back from Oxlo.ai.

$ export OXLO_API_KEY="sk-oxlo.ai-..."
$ python sentiment_batch.py

Overall: mixed (confidence: 0.85)
Counts: {'positive': 8, 'negative': 6, 'neutral': 6}
Themes: usability, performance, support, onboarding, api-quality

Top positives:
  Review #1: This app is fantastic. The new export feature saved me hours...
  Review #4: Absolutely love the dark mode. Finally my eyes do not hurt...
  Review #6: Smooth onboarding, great tutorials. My team was productive...
  Review #8: The API is rock solid. Webhooks fire instantly and the retry...
  Review #10: Best investment we made this quarter. Revenue is up because...
  Review #13: The collaboration tools are a game changer. Real time editing...
  Review #16: Security settings are robust. SSO integration took five...
  Review #18: Outstanding documentation. Every endpoint has a working...
  Review #20: Love the weekly summary emails. Keeps my stakeholders...

Top negatives:
  Review #2: Terrible update. Crashes every time I open a project larger...
  Review #5: Support never answered my ticket. Waited six days and gave up...
  Review #9: UI feels dated. Too many clicks to run a simple report...
  Review #11: Login is buggy on Safari. Had to switch to Chrome just to...
  Review #12: Good features, but performance is slow with large datasets...
  Review #14: I regret the purchase. The refund process is opaque and...
  Review #17: Mobile app is unusable. Menus overlap and buttons do not...

Wrap-up and next steps

From here, you can wire this script to a cron job or GitHub Action that pulls fresh reviews from your app store API and appends them to a weekly report. If your review volume outgrows the context window, shard batches by language or product area, or switch to DeepSeek V4 Flash on Oxlo.ai for its 1 million token context window and merge results downstream.

Because Oxlo.ai uses flat per-request pricing rather than token-based billing, experimenting with larger batches or longer transcripts does not require a spreadsheet to estimate cost. You can explore the plans at https://oxlo.ai/pricing and scale the batch size to match your workflow.

Top comments (0)