Proactive Notification and Alert Systems
Agents that surface important information without being asked.
Proactive vs Reactive Agents
Most agents are reactive — they respond to requests. A proactive agent monitors conditions and reaches out to the user when something noteworthy happens, without being asked. This is more like having a personal assistant.
Background Polling Loop
The simplest proactive pattern: a background loop that runs a check every N minutes and sends an alert if a condition is met. Use a thread or an async loop to avoid blocking.
import asyncio
import httpx
from datetime import datetime
async def poll_price(ticker: str, alert_threshold: float) -> None:
async with httpx.AsyncClient() as client:
response = await client.get(
f'https://api.finance.example.com/quote/{ticker}',
headers={'Authorization': 'Bearer your-api-key'}
)
data = response.json()
price = float(data.get('price', 0))
if price < alert_threshold:
await send_push_notification(
title=f'{ticker} Price Alert',
message=f'{ticker} is now ${price:.2f}, below your threshold of ${alert_threshold:.2f}'
)
async def price_alert_loop(ticker: str, threshold: float, interval_seconds: int = 300):
print(f'Monitoring {ticker} every {interval_seconds}s, alert below ${threshold}')
while True:
try:
await poll_price(ticker, threshold)
except Exception as e:
print(f'Polling error: {e}')
await asyncio.sleep(interval_seconds)
print('Price alert loop defined')All lessons in this course
- Always-On Agent Design Patterns
- Proactive Notification and Alert Systems
- Context Persistence Across Sessions
- Building a Daily Briefing Agent