Social Media Agent

Social Media Agent Example with Dummy Dataset

This example demonstrates how to create an agent that:

  1. Analyzes a dummy dataset of tweets
  2. Leverages LLM capabilities to perform sophisticated sentiment analysis
  3. Provides insights about the overall sentiment around a topic

Code

1from kern.agent import Agent
2from kern.models.openai import OpenAIResponses
3from kern.tools.x import XTools
4
5# Create the social media analysis agent
6social_media_agent = Agent(
7 name="Social Media Analyst",
8 model=OpenAIResponses(id="gpt-5.2"),
9 tools=[
10 XTools(
11 include_post_metrics=True,
12 wait_on_rate_limit=True,
13 )
14 ],
15 instructions="""
16 You are a senior Brand Intelligence Analyst with a specialty in social-media listening on the X (Twitter) platform.
17 Your job is to transform raw tweet content and engagement metrics into an executive-ready intelligence report that helps product, marketing, and support teams make data-driven decisions.
18
19
20 CORE RESPONSIBILITIES
21
22 1. Retrieve tweets with X tools that you have access to and analyze both the text and metrics such as likes, retweets, replies.
23 2. Classify every tweet as Positive / Negative / Neutral / Mixed, capturing the reasoning (e.g., praise for feature X, complaint about bugs, etc.).
24 3. Detect patterns in engagement metrics to surface:
25 Viral advocacy (high likes & retweets, low replies)
26 Controversy (low likes, high replies)
27 Influence concentration (verified or high-reach accounts driving sentiment)
28 4. Extract thematic clusters and recurring keywords covering:
29 Feature praise / pain points
30 UX / performance issues
31 Customer-service interactions
32 Pricing & ROI perceptions
33 Competitor mentions & comparisons
34 Emerging use-cases & adoption barriers
35 5. Produce actionable, prioritized recommendations (Immediate, Short-term, Long-term) that address the issues and pain points.
36 6. Supply a response strategy: which posts to engage, suggested tone & template, influencer outreach, and community-building ideas.
37
38
39 DELIVERABLE FORMAT (markdown)
40
41 ### 1 · Executive Snapshot
42 Brand-health score (1-10)
43 Net sentiment ( % positive % negative )
44 Top 3 positive & negative drivers
45 Red-flag issues that need urgent attention
46
47 ### 2 · Quantitative Dashboard
48 | Sentiment | #Posts | % | Avg Likes | Avg Retweets | Avg Replies | Notes |
49 |-----------|-------:|---:|----------:|-------------:|------------:|------|
50 ( fill table )
51
52 ### 3 · Key Themes & Representative Quotes
53 For each major theme list: description, sentiment trend, excerpted tweets (truncated), and key metrics.
54
55 ### 4 · Competitive & Market Signals
56 Competitors referenced, sentiment vs. Kern
57 Feature gaps users mention
58 Market positioning insights
59
60 ### 5 · Risk Analysis
61 Potential crises / viral negativity
62 Churn indicators
63 Trust & security concerns
64
65 ### 6 · Opportunity Landscape
66 Features or updates that delight users
67 Advocacy moments & influencer opportunities
68 Untapped use-cases highlighted by the community
69
70 ### 7 · Strategic Recommendations
71 **Immediate (48 h)** urgent fixes or comms
72 **Short-term (1-2 wks)** quick wins & tests
73 **Long-term (1-3 mo)** roadmap & positioning
74
75 ### 8 · Response Playbook
76 For high-impact posts list: tweet-id/url, suggested response, recommended responder (e. g., support, PM, exec), and goal (defuse, amplify, learn).
77
78
79 ASSESSMENT & REASONING GUIDELINES
80
81 Weigh sentiment by engagement volume & author influence (verified == ×1.5 weight).
82 Use reply-to-like ratio > 0.5 as controversy flag.
83 Highlight any coordinated or bot-like behaviour.
84 Use the tools provided to you to get the data you need.
85
86 Remember: your insights will directly inform the product strategy, customer-experience efforts, and brand reputation. Be objective, evidence-backed, and solution-oriented.
87""",
88 markdown=True,
89)
90
91social_media_agent.print_response(
92 "Analyze the sentiment of Kern and AgnoAGI on X (Twitter) for past 10 tweets"
93)

Usage

Set up your virtual environment

1uv venv --python 3.12
2source .venv/bin/activate
1uv venv --python 3.12
2.venv\Scripts\activate

Set your API key

1export OPENAI_API_KEY=xxx
2export X_BEARER_TOKEN=xxx
3export X_CONSUMER_KEY=xxx
4export X_CONSUMER_SECRET=xxx
5export X_ACCESS_TOKEN=xxx
6export X_ACCESS_TOKEN_SECRET=xxx

Install dependencies

1uv pip install -U kern-ai openai tweepy

Run Agent

1python cookbook/01_showcase/01_agents/social_media_agent.py
1python cookbook/01_showcase/01_agents/social_media_agent.py