DEV Community

shashank ms
shashank ms

Posted on

Leveraging LLM for Sentiment Analysis with Long Context: A Comprehensive Guide

We are going to build a batch sentiment analyzer that scores hundreds of customer feedback entries in a single API call by exploiting long-context windows. This saves infrastructure complexity for support teams and product managers who currently pipe short snippets through fragile chunking pipelines.

What you'll need

I also use pandas in Step 5 to format results, so install that if you want to run the final snippet verbatim.

Step 1: Bootstrap the client

I start by instantiating the OpenAI-compatible client pointing at Oxlo.ai and fire a short completion to confirm the key is live.

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": "You are a helpful assistant."},
        {"role": "user", "content": "Reply with OK"},
    ],
)
print(response.choices[0].message.content)

Step 2: Assemble the long-context payload

Instead of paying for one request per review, I concatenate a full day of feedback into a single prompt. Each review is tagged with an ID so the model can reference it individually.

USER_MESSAGE = """
[ID: 001] The onboarding flow is slick, but the export button disappears on mobile.
[ID: 002] Love the new dark mode. Pricing is a bit steep for a two-person team though.
[ID: 003] Support took three days to reply. Not acceptable for a critical outage.
[ID: 004] API documentation is solid. I had the integration running in under an hour.
[ID: 005] Random 500 errors every Tuesday morning. Very frustrating.
[ID: 006] The CSV import feature saved us weeks of manual work. Huge win.
[ID: 007] UI feels cluttered after the last update. Hard to find the settings panel.
[ID: 008] Great product, but I wish there were more granular role permissions.
[ID: 009] Checkout process is smooth. No complaints so far.
[ID: 010] Had to downgrade because the Pro tier limits are too restrictive for our volume.
"""

Step 3: Define the system prompt

I want structured, machine-readable output. The system prompt forces the model to return a JSON array where every element maps back to the original ID.

SYSTEM_PROMPT = """
You are a sentiment analysis engine. The user will paste a batch of reviews, each prefixed with an ID.
Analyze every review independently and return a single JSON object containing a key "results" whose value is a list.
Each list element must have:
- "id": the review ID string
- "sentiment": one of "positive", "neutral", or "negative"
- "confidence": an integer between 1 and 10
- "key_phrase": a short 3- to 6-word quote capturing the core emotion
Do not include markdown formatting, explanations, or text outside the JSON object.
"""

Step 4: Send the batch for analysis

Now I wrap the call in a function. Because Oxlo.ai uses request-based pricing, the cost is the same whether I send one review or fifty. I target kimi-k2.6 for its 131K context window, which handles long feedback dumps without truncation.

import json

def analyze_sentiment_batch(user_message: str):
    response = client.chat.completions.create(
        model="kimi-k2.6",
        messages=[
            {"role": "system", "content": SYSTEM_PROMPT},
            {"role": "user", "content": user_message},
        ],
    )
    raw = response.choices[0].message.content
    return json.loads(raw)

results = analyze_sentiment_batch(USER_MESSAGE)
print(json.dumps(results, indent=2))

Step 5: Normalize and display results

Raw JSON is useful, but a DataFrame is easier to scan. I flatten the results and sort by confidence so the noisiest feedback surfaces first.

import pandas as pd

df = pd.DataFrame(results["results"])
df = df.sort_values(by="confidence", ascending=False)

print(f"Processed {len(df)} reviews")
print(df.to_string(index=False))

Run it

Executing the full script produces the following output. The model correctly flags the support delay and the Tuesday errors as negative, while the CSV import and API docs score positive.

Processed 10 reviews

  id  sentiment  confidence              key_phrase
 003   negative          10  three days to reply
 005   negative           9  Random 500 errors
 010   negative           8  limits are too restrictive
 007   neutral            6  UI feels cluttered
 002   neutral            5  Pricing is a bit steep
 008   neutral            5  more granular role permissions
 001   positive           7  export button disappears
 004   positive           8  integration running in under
 006   positive           9  saved us weeks
 009   positive           7  smooth. No complaints

Wrap up and next steps

This pipeline removes the need for chunking logic or token-counting middleware. Because Oxlo.ai charges per request, not per token, running a thousand-review dump costs the same as a one-sentence ping. You can see exact plan details at https://oxlo.ai/pricing.

Two concrete ways to extend this: wire the script into a nightly cron job that pulls fresh tickets from your support CRM, or modify the prompt to perform aspect-based sentiment so you can track scores for specific product areas like billing, performance, and UX.

Top comments (0)