DEV Community

The BookMaster
The BookMaster

Posted on

I Built an AI Agent That Analyzes Text Sentiment in Real-Time (Here's the Code)

The Problem Every AI Agent Operator Faces

If you're running AI agents in production, you know the pain: your agents generate a lot of text, but you have no idea if that text hits the right tone. Is the customer service agent being empathetic enough? Is the sales agent too aggressive? Is the content agent hitting the mark?

You end up reading through logs manually, or worse—finding out from customers that your agent's tone was off.

The Solution: TextInsight API

I built a simple wrapper around a sentiment analysis API that lets any AI agent self-check its output before sending it to users. The agent generates text, analyzes the sentiment, and can regenerate or adjust if needed.

Here's the core logic:

import requests

def analyze_and_regenerate(text, agent_id, target_sentiment="neutral"):
    """
    Analyze text sentiment and regenerate if tone is off.
    """
    response = requests.post(
        "https://thebookmaster.zo.space/api/textinsight",
        json={
            "text": text,
            "agent_id": agent_id,
            "target_sentiment": target_sentiment
        }
    )
    result = response.json()

    if result["sentiment_score"] < 0.3:
        return regenerate_with_tone(text, target_sentiment)
    return text

def regenerate_with_tone(original_text, target):
    # Your regeneration logic here
    return f"[Adjusted] {original_text}"
Enter fullscreen mode Exit fullscreen mode

Why This Matters for AI Agents

Most AI agents are blind to how their output feels. They optimize for correctness and relevance, but tone is an afterthought. By adding a simple sentiment check layer, you can:

  • Catch negative-sounding output before it reaches users
  • Ensure consistency across all agent interactions
  • Build trust with end users who expect professional, appropriate tone

Get Started

I built this as part of my AI agent toolkit. The full catalog of tools—including the TextInsight API—is available at my Bolt Marketplace:

Full catalog of my AI agent tools at https://thebookmaster.zo.space/bolt/market

The TextInsight API is available for individual purchase or as part of a bundle. If you're serious about production-quality AI agents, this is a must-have layer in your pipeline.

Top comments (0)