VOOZH about

URL: https://dev.to/henryknight_dev/how-i-automated-reddit-monitoring-with-the-claude-api-full-code-1g7g

⇱ How I Automated Reddit Monitoring with the Claude API (Full Code) - DEV Community


Stop Wasting Hours on Manual Reddit Monitoring

If you've ever tried to track Reddit for mentions of your product, niche, or competitors, you know the pain. You open r/MachineLearning, r/learnpython, r/entrepreneur — scan hundreds of posts — and most of it is noise. An hour gone. Maybe you caught something useful, maybe not.

I automated this entire workflow with Python and the Claude API. It now runs on a schedule, scores every post for relevance and sentiment, and pings me only when something worth acting on shows up.

Here's the full setup.


Step 1: Install the Anthropic SDK

pip install anthropic praw requests

You'll need an Anthropic API key from console.anthropic.com. Set it as an environment variable:

export ANTHROPIC_API_KEY="sk-ant-..."

Step 2: Fetch Reddit Posts

You can use PRAW (the official Reddit API wrapper) or plain requests against the JSON endpoint. Here's the lightweight requests approach — no OAuth needed for read-only:

import requests

def fetch_reddit_posts(subreddit: str, limit: int = 25) -> list[dict]:
 url = f"https://www.reddit.com/r/{subreddit}/new.json"
 headers = {"User-Agent": "RedditMonitor/1.0"}
 params = {"limit": limit}

 resp = requests.get(url, headers=headers, params=params, timeout=10)
 resp.raise_for_status()

 posts = []
 for item in resp.json()["data"]["children"]:
 d = item["data"]
 posts.append({
 "id": d["id"],
 "title": d["title"],
 "selftext": d.get("selftext", "")[:500],
 "score": d["score"],
 "url": f"https://reddit.com{d['permalink']}",
 "created_utc": d["created_utc"],
 })
 return posts

Call this for each subreddit you want to monitor:

subreddits = ["MachineLearning", "learnpython", "entrepreneur", "SideProject"]
all_posts = []
for sub in subreddits:
 all_posts.extend(fetch_reddit_posts(sub, limit=20))

Step 3: Score Posts with Claude API

Now we pass the posts to Claude for sentiment and relevance scoring. I batch them to keep API costs low — one call handles up to 15 posts at a time.

import anthropic
import json

client = anthropic.Anthropic()

def score_posts(posts: list[dict], topic: str) -> list[dict]:
 if not posts:
 return []

 payload = [
 {"id": p["id"], "title": p["title"], "body": p["selftext"]}
 for p in posts
 ]

 prompt = f"""You are a Reddit monitoring assistant. The user tracks the topic: "{topic}".

For each post below, return a JSON array with:
- id: the post id
- relevance: 0-10 (how relevant to the topic)
- sentiment: "positive", "neutral", or "negative"
- reason: one sentence why

Posts:
{json.dumps(payload, indent=2)}

Return ONLY valid JSON. No explanation outside the array."""

 message = client.messages.create(
 model="claude-sonnet-4-6",
 max_tokens=1024,
 messages=[{"role": "user", "content": prompt}]
 )

 raw = message.content[0].text.strip()
 if raw.startswith("```

"):
 raw = raw.split("

```")[1]
 if raw.startswith("json"):
 raw = raw[4:]

 scores = json.loads(raw.strip())
 score_map = {s["id"]: s for s in scores}
 for post in posts:
 post.update(score_map.get(post["id"], {}))

 return posts

Step 4: Filter High-Signal Posts and Alert

Set a threshold — anything with relevance >= 7 gets surfaced. You can print it, log it, or fire a webhook.

def monitor_reddit(topic: str, subreddits: list[str], threshold: int = 7):
 all_posts = []
 for sub in subreddits:
 all_posts.extend(fetch_reddit_posts(sub, limit=20))

 scored = score_posts(all_posts, topic)

 high_signal = [
 p for p in scored
 if p.get("relevance", 0) >= threshold
 ]

 if not high_signal:
 print("No high-signal posts found.")
 return

 print(f"\n=== {len(high_signal)} High-Signal Posts ===\n")
 for post in sorted(high_signal, key=lambda x: x.get("relevance", 0), reverse=True):
 print(f"[{post['relevance']}/10] {post['title']}")
 print(f" Sentiment: {post['sentiment']}")
 print(f" Reason: {post['reason']}")
 print(f" URL: {post['url']}\n")

monitor_reddit(
 topic="AI automation tools for developers",
 subreddits=["MachineLearning", "learnpython", "entrepreneur", "SideProject"]
)

Example output:

=== 3 High-Signal Posts ===

[9/10] I built an AI agent that monitors Slack + Reddit for competitor mentions
 Sentiment: positive
 Reason: Directly describes the automation topic with code included
 URL: https://reddit.com/r/SideProject/...

[8/10] Looking for tools to automate social media monitoring
 Sentiment: neutral
 Reason: User actively seeking solutions in this space
 URL: https://reddit.com/r/entrepreneur/...

What This Replaces

  • 2-3 hours of manual Reddit scrolling per day
  • Missed mentions because you can't be everywhere
  • Gut-feel relevance scoring that's inconsistent

The Claude API does the reasoning — you just act on what it surfaces.


Extend It

A few directions I've taken this further:

  • Schedule it: Run with cron or GitHub Actions every hour
  • Deduplicate: Store post IDs in SQLite so you don't re-score old posts
  • Webhook alerts: Fire to Slack, Discord, or Telegram when relevance >= 9
  • Track competitors: Add competitor names to the topic string — Claude picks them out naturally

Want the full browser automation starter kit? I packaged everything here: https://knightops.gumroad.com/l/ytakiy — includes pre-built Claude API agent templates.