
| Difficulty: Intermediate | Category: Ai Tools |
Build a Political News Sentiment Tracker with Real-Time API Monitoring
With Trump ordering DOJ investigations into California Governor Gavin Newsom on June 15, 2026, institutional investors, compliance teams, and policy analysts need real-time alerting systems for political developments that could impact markets, regulatory posture, or operational risk. By the end of this tutorial, you’ll deploy a REST API monitoring pipeline that ingests breaking news, scores sentiment with Claude Sonnet 4.5, and flags anomalous executive actions—all running on free-tier infrastructure for under $2/month.
Prerequisites
- Python >=3.11 installed locally
- Anthropic API key (free tier includes $5 credit; get yours at console.anthropic.com)
- NewsAPI account (free tier: 100 requests/day at newsapi.org/register)
- Basic familiarity with JSON and HTTP requests
- Terminal/command line access
Step-by-Step Guide
Step 1: Set Up Your Development Environment
Create a project directory and virtual environment to isolate dependencies:
mkdir political-news-tracker
cd political-news-tracker
python3.11 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
Install required packages with pinned versions for reproducibility:
pip install anthropic==0.28.0 requests==2.32.3 python-dotenv==1.0.1
Create a .env file in your project root with your API credentials:
ANTHROPIC_API_KEY=sk-ant-api03-your-key-here
NEWSAPI_KEY=your-newsapi-key-here
⚠️ WARNING: Never commit .env to version control. Add it to .gitignore immediately.
Step 2: Build the News Fetcher Module
Create news_fetcher.py to query NewsAPI for political developments. We’ll focus on executive branch actions and DOJ-related stories:
import os
import requests
from dotenv import load_dotenv
from datetime import datetime, timedelta
load_dotenv()
def fetch_political_news(query="DOJ investigation OR executive order", hours_ago=24):
"""Fetch recent political news from NewsAPI."""
api_key = os.getenv("NEWSAPI_KEY")
# Calculate time window
from_date = (datetime.now() - timedelta(hours=hours_ago)).isoformat()
url = "https://newsapi.org/v2/everything"
params = {
"q": query,
"from": from_date,
"sortBy": "publishedAt",
"language": "en",
"apiKey": api_key,
"pageSize": 20 # Free tier limit
}
response = requests.get(url, params=params)
response.raise_for_status()
articles = response.json().get("articles", [])
return [
{
"title": a["title"],
"description": a["description"],
"url": a["url"],
"published": a["publishedAt"],
"source": a["source"]["name"]
}
for a in articles if a["description"]
]
Gotcha: NewsAPI’s free tier is limited to 100 requests/day and only returns articles from the past 30 days. For production use, upgrade to the Business plan ($449/month) for real-time webhooks.
Step 3: Implement Sentiment Analysis with Claude Sonnet 4.5
Create sentiment_analyzer.py using Anthropic’s latest model family. Claude Sonnet 4.5 costs $3.00 per million input tokens and $15.00 per million output tokens—roughly $0.005 per article at our payload size:
import os
from anthropic import Anthropic
from dotenv import load_dotenv
load_dotenv()
client = Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))
def analyze_sentiment(article):
"""Analyze political news sentiment and extract key entities."""
prompt = f"""Analyze this breaking political news article:
Title: {article['title']}
Description: {article['description']}
Source: {article['source']}
Provide a JSON response with:
1. sentiment_score: float from -1.0 (very negative) to 1.0 (very positive)
2. political_risk_level: "low", "medium", "high", or "critical"
3. key_actors: list of political figures mentioned
4. implications: 2-sentence summary of potential impact
5. anomaly_flag: true if this represents unusual executive action
Return ONLY valid JSON, no markdown formatting."""
message = client.messages.create(
model="claude-sonnet-4-5",
max_tokens=1024,
temperature=0.3, # Lower temp for consistent scoring
messages=[{"role": "user", "content": prompt}]
)
import json
return json.loads(message.content[0].text)
Pro Tip: Set temperature=0.3 for analytical tasks requiring consistency. For creative summarization, use temperature=0.7.
Step 4: Create the Anomaly Detection Layer
Build anomaly_detector.py to flag stories that deviate from normal political patterns. We’ll use a simple statistical threshold, but this is where you’d integrate time-series models in production:
def detect_anomaly(analysis_result, historical_baseline=-0.15):
"""Flag articles that significantly deviate from baseline sentiment.
Args:
analysis_result: Dict from sentiment_analyzer
historical_baseline: Average sentiment score for political news
(empirically ~-0.15 for executive branch coverage)
"""
score = analysis_result["sentiment_score"]
risk = analysis_result["political_risk_level"]
# Flag if sentiment is >2 std deviations from baseline OR high risk
threshold = 0.4 # ~2 std deviations for political news
if abs(score - historical_baseline) > threshold or risk in ["high", "critical"]:
return {
"is_anomaly": True,
"deviation": abs(score - historical_baseline),
"reason": f"Sentiment deviation: {score:.2f} vs baseline {historical_baseline:.2f}"
}
return {"is_anomaly": False}
⚠️ WARNING: This baseline is a placeholder. Calibrate against your own historical data for accurate alerting. A week of logged scores gives a robust mean and standard deviation.
Step 5: Build the Main Pipeline Orchestrator
Create main.py to tie everything together with proper error handling:
from news_fetcher import fetch_political_news
from sentiment_analyzer import analyze_sentiment
from anomaly_detector import detect_anomaly
import json
from datetime import datetime
def run_monitoring_cycle():
"""Execute one monitoring cycle: fetch -> analyze -> detect."""
print(f"[{datetime.now()}] Starting monitoring cycle...")
# Fetch recent news
articles = fetch_political_news(
query="DOJ investigation OR Trump executive order OR Newsom",
hours_ago=6
)
print(f"Found {len(articles)} articles")
# Analyze and detect anomalies
anomalies = []
for article in articles:
try:
analysis = analyze_sentiment(article)
anomaly_check = detect_anomaly(analysis)
if anomaly_check["is_anomaly"]:
anomalies.append({
"article": article,
"analysis": analysis,
"anomaly": anomaly_check,
"timestamp": datetime.now().isoformat()
})
print(f"\n🚨 ANOMALY DETECTED:")
print(f"Title: {article['title']}")
print(f"Risk: {analysis['political_risk_level']}")
print(f"Sentiment: {analysis['sentiment_score']:.2f}")
print(f"Actors: {', '.join(analysis['key_actors'])}")
except Exception as e:
print(f"Error processing article: {e}")
continue
# Save results
if anomalies:
with open("anomalies.json", "w") as f:
json.dump(anomalies, f, indent=2)
return len(anomalies)
if __name__ == "__main__":
count = run_monitoring_cycle()
print(f"\nCycle complete. {count} anomalies detected.")
Run your first monitoring cycle:
python main.py
You should see output analyzing the Newsom-DOJ investigation story with a likely “high” or “critical” risk flag.
Step 6: Schedule Automated Monitoring
For continuous monitoring, use cron (Linux/Mac) or Task Scheduler (Windows). Create run_every_hour.sh:
#!/bin/bash
cd /path/to/political-news-tracker
source venv/bin/activate
python main.py >> logs/monitor.log 2>&1
Add to crontab to run every hour:
0 * * * * /path/to/political-news-tracker/run_every_hour.sh
Pro Tip: For production deployments, use AWS Lambda with EventBridge triggers (runs free for , “risk_level”: “”}}”””
msg = anthropic_client.messages.create(
model="claude-sonnet-4-5",
max_tokens=256,
messages=[{"role": "user", "content": prompt}]
)
import json
return json.loads(msg.content[0].text)
Run
articles = get_news() for a in articles[:3]: # First 3 to stay under free tier result = analyze(a) print(f”\n📰 {a[‘title’]}”) print(f”Sentiment: {result[‘sentiment_score’]:.2f} | Risk: {result[‘risk_level’]}”)
Run with: `python quick_tracker.py`
Expected output for the Newsom-DOJ story should show sentiment around -0.6 to -0.8 with "high" risk classification.
## Debugging Section
**Error:** `anthropic.AuthenticationError: invalid_api_key`
**Cause:** API key not loaded from `.env` or incorrect key format
**Fix:** Verify `.env` exists in working directory. Check key starts with `sk-ant-api03-`. Reload with `load_dotenv()` before creating client.
**Error:** `newsapi.exceptions.APIException: rateLimited`
**Cause:** Exceeded 100 requests/day on free tier
**Fix:** Reduce `pageSize` parameter or upgrade to NewsAPI Business plan. Implement exponential backoff with `time.sleep(3600)` to retry after rate limit window.
**Error:** `json.JSONDecodeError: Expecting value`
**Cause:** Claude returned markdown-formatted JSON instead of raw JSON
**Fix:** Strengthen prompt with "Return ONLY valid JSON, no markdown code fences." Add fallback parser:
```python
import re
def extract_json(text):
match = re.search(r'\{.*\}', text, re.DOTALL)
return json.loads(match.group(0)) if match else {}
Error: All articles flagged as anomalies
Cause: Historical baseline doesn’t match current political climate
Fix: Run sentiment analysis on 50+ articles without anomaly detection. Calculate mean and std dev. Update historical_baseline to your computed mean.
Key Takeaways
- REST APIs enable real-time intelligence: Combining NewsAPI for ingestion with Claude Sonnet 4.5 for analysis creates a production-grade monitoring system at commodity pricing ($0.005/article).
- Sentiment scoring requires calibration: Generic baselines fail in specialized domains like political news. Always compute your own historical mean from domain-specific data before deploying anomaly detection.
- Modern LLMs excel at structured extraction: Claude Sonnet 4.5 with low temperature (0.3) reliably outputs valid JSON for analytical tasks, replacing complex NLP pipelines that would have required labeled training data and custom models in previous years.
- Free tiers support MVP validation: NewsAPI’s 100 requests/day and Anthropic’s $5 credit let you test this entire pipeline for 30 days before spending a dollar—enough to prove value to stakeholders before scaling.
What’s Next
Once your tracker is running, extend it with real-time webhook ingestion using FastAPI and ngrok for sub-second latency, or build a historical risk dashboard with Streamlit to visualize sentiment trends across political actors over time—both tutorials available in AtlasSignal’s advanced AI tools series.
Key Takeaway: You’ll build a production-ready sentiment analysis pipeline that monitors breaking political news via REST APIs, processes it with Claude’s latest models, and surfaces anomalies in executive actions—skills directly applicable to compliance, risk monitoring, and media intelligence workflows.
New AI tutorials published daily on AtlasSignal. Follow @AtlasSignalDesk for more.
This report was produced with AI-assisted research and drafting, curated and reviewed under AtlasSignal’s editorial standards. For corrections or feedback, contact atlassignal.ai@gmail.com.