Skip to main content

Overview

Agents provide built-in task scheduling with support for one-time tasks, delayed execution, cron-based schedules, and fixed intervals. Schedules persist across hibernation and support automatic retry on failure.

schedule()

Schedule a callback to run at a future time or on a recurring interval.
keyof this
required
Name of the method to call
ScheduleOptions
required
Schedule configuration (see variants below)
Returns: Promise<string> - Schedule ID

Schedule Types

One-Time (Specific Time)

Execute a callback at a specific date/time.
Date
required
Date/time to execute
T
Data to pass to the callback
RetryOptions
Retry options for this schedule

Delayed Execution

Execute a callback after a delay (in seconds).
number
required
Number of seconds to delay
T
Data to pass to the callback
RetryOptions
Retry options for this schedule

Cron Schedule

Execute a callback on a recurring schedule using cron syntax.
string
required
Cron expression (e.g., “0 0 * * *”)
T
Data to pass to the callback
RetryOptions
Retry options for this schedule
Cron Format: minute hour day month weekday
  • * = any value
  • */n = every n units
  • 0-6 = Sunday through Saturday (for weekday)

Interval

Execute a callback at fixed intervals (in seconds).
number
required
Number of seconds between executions
T
Data to pass to the callback
RetryOptions
Retry options for this schedule
Interval schedules are resilient to hung executions. If a callback takes longer than hungScheduleTimeoutSeconds (default: 30s), the interval is reset.

Callback Implementation

Scheduled callbacks receive the payload (if provided):

Retry Options

Schedules support automatic retry on failure:
RetryOptions
number
default:"3"
Maximum number of retry attempts
number
default:"100"
Base delay in milliseconds for exponential backoff
number
default:"3000"
Maximum delay cap in milliseconds
Retry options can also be configured globally via static options.retry on the Agent class.

Managing Schedules

getSchedules()

Query existing schedules.

cancelSchedule()

Cancel a scheduled task.

updateSchedule()

Update a schedule’s payload or timing:

Queue vs Schedule

queue()

For immediate asynchronous execution:
When to use:
  • Immediate background tasks
  • Fire-and-forget operations
  • No specific timing requirements

schedule()

For time-based or recurring execution:
When to use:
  • Time-based tasks
  • Recurring operations
  • Delayed execution

Natural Language Scheduling

Use AI to parse natural language schedule requests:
Example inputs:
  • “Backup database every day at midnight”
  • “Send report tomorrow at 2 PM”
  • “Run health check every 15 minutes”

Persistence

Schedules are stored in SQLite:
Schedules survive Agent hibernation and are automatically restored on wake.

Best Practices

Keep Callbacks Small

Use Payloads for Context

Handle Failures

Use Cron for Recurring Tasks