By Sagar Shankaran, Founder of CallSphere
Master sentiment analysis techniques for AI agents, including aspect-based sentiment, fine-grained emotion detection, and strategies for integrating sentiment scores into agent routing and escalation logic.
Key takeaways
An AI agent that cannot detect user frustration will treat "This is the THIRD time I've asked!!!" the same as "Could you help me with something?" The words contain a request in both cases, but the emotional context demands radically different responses. Sentiment analysis gives agents the ability to adapt their behavior, escalate when needed, and match tone to the user's emotional state.
Sentiment analysis classifies text along an emotional axis — typically positive, negative, or neutral. More advanced models detect fine-grained emotions like anger, joy, sadness, or frustration, and aspect-based models pinpoint which specific topic within the text carries which sentiment.
The Hugging Face transformers library provides ready-to-use sentiment models that work out of the box.
flowchart LR
IN(["Input text"])
TOK["Tokenizer<br/>BPE or SentencePiece"]
EMB["Token plus position<br/>embeddings"]
subgraph BLOCK["Transformer block (xN)"]
ATTN["Multi head<br/>self attention"]
NORM1["Layer norm"]
FF["Feed forward<br/>MLP"]
NORM2["Layer norm"]
end
HEAD["LM head plus<br/>softmax"]
SAMP["Sampling<br/>top-p, temperature"]
OUT(["Next token"])
IN --> TOK --> EMB --> ATTN --> NORM1 --> FF --> NORM2 --> HEAD --> SAMP --> OUT
SAMP -.->|Append| EMB
style BLOCK fill:#ede9fe,stroke:#7c3aed,color:#1e1b4b
style ATTN fill:#4f46e5,stroke:#4338ca,color:#fff
style OUT fill:#059669,stroke:#047857,color:#fff
from transformers import pipeline
sentiment_analyzer = pipeline(
"sentiment-analysis",
model="distilbert-base-uncased-finetuned-sst-2-english"
)
def analyze_sentiment(text: str) -> dict:
result = sentiment_analyzer(text)[0]
return {
"label": result["label"], # POSITIVE or NEGATIVE
"confidence": result["score"], # 0.0 to 1.0
}
print(analyze_sentiment("Your product is fantastic, I love it!"))
# {'label': 'POSITIVE', 'confidence': 0.9998}
print(analyze_sentiment("I've been waiting three days with no response."))
# {'label': 'NEGATIVE', 'confidence': 0.9994}
Binary positive/negative is often insufficient for agent decision making. A user who is confused needs a different response than one who is angry. Multi-label emotion classifiers provide richer signals.
Hear it before you finish reading
Talk to a live CallSphere AI voice agent in your browser — 60 seconds, no signup.
from transformers import pipeline
emotion_classifier = pipeline(
"text-classification",
model="j-hartmann/emotion-english-distilroberta-base",
top_k=None,
)
def detect_emotions(text: str) -> list[dict]:
results = emotion_classifier(text)[0]
return [
{"emotion": r["label"], "score": round(r["score"], 3)}
for r in sorted(results, key=lambda x: x["score"], reverse=True)
if r["score"] > 0.1
]
emotions = detect_emotions("I can't believe you charged me twice!")
# [{'emotion': 'anger', 'score': 0.87}, {'emotion': 'disgust', 'score': 0.06}]
Aspect-based sentiment goes beyond overall document sentiment to identify sentiment toward specific topics within the same message.
from transformers import pipeline
absa = pipeline(
"text-classification",
model="yangheng/deberta-v3-base-absa-v1.1",
)
def aspect_sentiment(text: str, aspects: list[str]) -> dict:
"""Analyze sentiment toward each aspect mentioned in the text."""
results = {}
for aspect in aspects:
input_text = f"{text} [SEP] {aspect}"
prediction = absa(input_text)[0]
results[aspect] = {
"sentiment": prediction["label"],
"confidence": round(prediction["score"], 3),
}
return results
feedback = "The food was excellent but the wait time was terrible."
aspects = ["food", "wait time"]
print(aspect_sentiment(feedback, aspects))
# {'food': {'sentiment': 'Positive', 'confidence': 0.96},
# 'wait time': {'sentiment': 'Negative', 'confidence': 0.94}}
The real power of sentiment analysis comes from using it to drive agent behavior. Here is a pattern that routes conversations based on emotional state.
from dataclasses import dataclass
from enum import Enum
class EscalationLevel(Enum):
NORMAL = "normal"
EMPATHETIC = "empathetic"
URGENT = "urgent"
HUMAN_HANDOFF = "human_handoff"
@dataclass
class SentimentContext:
overall: str
confidence: float
dominant_emotion: str
escalation: EscalationLevel
class SentimentRouter:
def __init__(self, analyzer, emotion_detector):
self.analyzer = analyzer
self.emotion_detector = emotion_detector
self.negative_streak = 0
def evaluate(self, message: str) -> SentimentContext:
sentiment = self.analyzer(message)
emotions = self.emotion_detector(message)
dominant = emotions[0]["emotion"] if emotions else "neutral"
if sentiment["label"] == "NEGATIVE":
self.negative_streak += 1
else:
self.negative_streak = 0
escalation = self._determine_escalation(
sentiment, dominant, self.negative_streak
)
return SentimentContext(
overall=sentiment["label"],
confidence=sentiment["confidence"],
dominant_emotion=dominant,
escalation=escalation,
)
def _determine_escalation(self, sentiment, emotion, streak):
if streak >= 3:
return EscalationLevel.HUMAN_HANDOFF
if emotion in ("anger", "disgust") and sentiment["confidence"] > 0.9:
return EscalationLevel.URGENT
if sentiment["label"] == "NEGATIVE":
return EscalationLevel.EMPATHETIC
return EscalationLevel.NORMAL
This router tracks consecutive negative messages and escalates appropriately. After three negative messages in a row, the agent hands off to a human — a pattern that dramatically reduces customer churn in support scenarios.
Sentiment models add latency to every agent interaction. Optimize with these strategies:
Still reading? Stop comparing — try CallSphere live.
CallSphere ships complete AI voice agents per industry — 14 tools for healthcare, 10 agents for real estate, 4 specialists for salons. See how it actually handles a call before you book a demo.
Sarcasm is one of the hardest problems in NLP. Standard sentiment models frequently misclassify sarcastic text as positive. The most effective mitigation is to use a two-stage approach: run a dedicated sarcasm detection model first, then flip the sentiment if sarcasm is detected. Models trained on Twitter data tend to handle sarcasm better because social media text is rich in ironic expressions.
For real-time chat agents, analyze every user message since emotional state can shift rapidly. However, you can reduce cost by using a lightweight model (DistilBERT) for every message and only invoking a more accurate model when the lightweight one detects negative sentiment above a threshold.
Ensure your sentiment model is tested across demographics and language styles. Some models score African American Vernacular English or non-native English as more negative than Standard American English. Audit your model with diverse test sets and set thresholds that account for model uncertainty rather than treating raw scores as ground truth.
#SentimentAnalysis #NLP #EmotionDetection #AIAgents #Python #Transformers #AgenticAI #LearnAI #AIEngineering

Written by
Sagar Shankaran· Founder, CallSphere
LinkedInSagar Shankaran is the founder of CallSphere, where he builds production AI voice and chat agents deployed across healthcare, hospitality, real estate, and home services. He writes about agentic AI, LLM engineering, and shipping voice agents that handle real calls in production.
See how AI voice agents work for your industry. Live demo available -- no signup required.
A founder's guide to the personal AI assistant market: best AI assistant apps, business-grade options, and how CallSphere's voice agent fits in.
A founder's guide to free AI agents, low-code AI agent builders, and how to know when you should pay for a real platform like CallSphere.
Graphiti is the open-source temporal knowledge graph for AI agents in 2026. Learn how bi-temporal memory beats vector RAG for voice agents and long-running LLMs.
Chatbot app vs ChatGPT in 2026: a founder's clear take on the difference, when to use which, and how a real AI chatbot app development works.
How we built a fault-tolerant HVAC emergency triage and tech-dispatch platform on Kubernetes — three-tier CQRS, 11 micro-agents on the OpenAI Agents SDK + LangGraph, NATS JetStream, DTMF/SMS/WebSocket acceptance, circuit breakers, and an evaluation pipeline that catches regressions before they wake a tech at 3 AM.
Head-to-head: OpenAI Frontier and Anthropic's managed agent stack — strengths, fit, and what each means for enterprise AI voice and chat deployment.
© 2026 CallSphere Inc. All rights reserved.
Made within San Francisco
Watch how CallSphere handles real customer calls, schedules appointments, and processes payments — live.