Scheduling

Scheduling is a technique for triggering Letta agents at regular intervals. Many real-world applications require proactive behavior, such as checking emails every few hours or scraping news sites. Scheduling can support autonomous agents with the capability to manage ongoing processes.

Native scheduling functionality is on the Letta Cloud roadmap. The approaches described in this guide are temporary solutions that work with both self-hosted and cloud deployments.

Common Use Cases

When building autonomous agents with Letta, you often need to trigger them at regular intervals for tasks like:

  • System Monitoring: Health checks that adapt based on historical patterns
  • Data Processing: Intelligent ETL processes that handle edge cases contextually
  • Memory Maintenance: Agents that optimize their own knowledge base over time
  • Proactive Notifications: Context-aware alerts that consider user preferences and timing
  • Continuous Learning: Agents that regularly ingest new information and update their understanding

This guide covers simple approaches to implement scheduled agent interactions.

Option 1: Simple Loop

The most straightforward approach for development and testing:

1import time
2from letta_client import Letta
3from datetime import datetime
4
5client = Letta(base_url="http://localhost:8283")
6agent_id = "your_agent_id"
7
8while True:
9 response = client.agents.messages.create(
10 agent_id=agent_id,
11 messages=[{
12 "role": "user",
13 "content": f"Scheduled check at {datetime.now()}"
14 }]
15 )
16 print(f"[{datetime.now()}] Agent responded")
17 time.sleep(300) # 5 minutes

Pros: Simple, easy to debug
Cons: Blocks terminal, stops if process dies

Option 2: System Cron Jobs

For production deployments, use cron for reliability:

1#!/usr/bin/env python3
2from letta_client import Letta
3from datetime import datetime
4
5try:
6 client = Letta(base_url="http://localhost:8283")
7 response = client.agents.messages.create(
8 agent_id="your_agent_id",
9 messages=[{
10 "role": "user",
11 "content": "Scheduled maintenance check"
12 }]
13 )
14 print(f"[{datetime.now()}] Success")
15except Exception as e:
16 print(f"[{datetime.now()}] Error: {e}")

Add to crontab with crontab -e:

$*/5 * * * * /usr/bin/python3 /path/to/send_message.py >> /var/log/letta_cron.log 2>&1
># or for Node.js:
>*/5 * * * * /usr/bin/node /path/to/send_message.js >> /var/log/letta_cron.log 2>&1

Pros: System-managed, survives reboots
Cons: Requires cron access

Best Practices

  1. Error Handling: Always wrap API calls in try-catch blocks
  2. Logging: Log both successes and failures for debugging
  3. Environment Variables: Store credentials securely
  4. Rate Limiting: Respect API limits and add backoff for failures

Example: Memory Maintenance Bot

Complete example that performs periodic memory cleanup:

1#!/usr/bin/env python3
2import logging
3from datetime import datetime
4from letta_client import Letta
5
6logging.basicConfig(
7 level=logging.INFO,
8 format='%(asctime)s - %(levelname)s - %(message)s'
9)
10
11def run_maintenance():
12 try:
13 client = Letta(base_url="http://localhost:8283")
14 agent_id = "your_agent_id"
15
16 response = client.agents.messages.create(
17 agent_id=agent_id,
18 messages=[{
19 "role": "user",
20 "content": "Please review your memory blocks for outdated information and clean up as needed."
21 }]
22 )
23
24 # Print any assistant messages
25 for message in response.messages:
26 if message.message_type == "assistant_message":
27 logging.info(f"Agent response: {message.content[:100]}...")
28
29 except Exception as e:
30 logging.error(f"Maintenance failed: {e}")
31
32if __name__ == "__main__":
33 run_maintenance()

Choose the scheduling method that best fits your deployment environment. For production systems, cron offers the best reliability, while simple loops are perfect for development and testing.