Skip to content
  • Auto
  • Light
  • Dark
DiscordForumGitHubSign up
Building on the Letta API
View as Markdown
Copy Markdown

Open in Claude
Open in ChatGPT

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.

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.

The most straightforward approach for development and testing:

import { LettaClient } from "@letta-ai/letta-client";
const client = new LettaClient({ token: process.env.LETTA_API_KEY });
const agentId = "your_agent_id";
while (true) {
const response = await client.agents.messages.create(agentId, {
messages: [
{
role: "user",
content: `Scheduled check at ${new Date()}`,
},
],
});
console.log(`[${new Date()}] Agent responded`);
await new Promise((resolve) => setTimeout(resolve, 300000)); // 5 minutes
}

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

For production deployments, use cron for reliability:

#!/usr/bin/env node
import { LettaClient } from "@letta-ai/letta-client";
async function sendMessage() {
try {
const client = new LettaClient({ token: process.env.LETTA_API_KEY });
const response = await client.agents.messages.create("your_agent_id", {
messages: [
{
role: "user",
content: "Scheduled maintenance check",
},
],
});
console.log(`[${new Date()}] Success`);
} catch (error) {
console.error(`[${new Date()}] Error:`, error);
}
}
sendMessage();

Add to crontab with crontab -e:

Terminal window
*/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

  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

Complete example that performs periodic memory cleanup:

#!/usr/bin/env node
import { LettaClient } from "@letta-ai/letta-client";
async function runMaintenance() {
try {
const client = new LettaClient({ token: process.env.LETTA_API_KEY });
const agentId = "your_agent_id";
const response = await client.agents.messages.create(agentId, {
messages: [
{
role: "user",
content:
"Please review your memory blocks for outdated information and clean up as needed.",
},
],
});
// Print any assistant messages
for (const message of response.messages) {
if (message.messageType === "assistant_message") {
console.log(`Agent response: ${message.content?.substring(0, 100)}...`);
}
}
} catch (error) {
console.error("Maintenance failed:", error);
}
}
// Run if called directly
if (import.meta.url === `file://${process.argv[1]}`) {
runMaintenance();
}

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.