Skip to main content

Overview

The Agents SDK provides a built-in queue system that allows you to schedule tasks for asynchronous execution. This is particularly useful for background processing, delayed operations, and managing workloads that don’t need immediate execution. The queue system is built into the base Agent class. Tasks are stored in a SQLite table and processed automatically in FIFO (First In, First Out) order.

QueueItem Type

Core Methods

queue()

Adds a task to the queue for future execution.
Parameters:
  • callback: The name of the method to call when processing the task
  • payload: Data to pass to the callback method
  • options.retry: Optional retry configuration. See Retries for details.
Returns: The unique ID of the queued task Example:

dequeue()

Removes a specific task from the queue by ID.
Parameters:
  • id: The ID of the task to remove
Example:

dequeueAll()

Removes all tasks from the queue.
Example:

dequeueAllByCallback()

Removes all tasks that match a specific callback method.
Parameters:
  • callback: Name of the callback method
Example:

getQueue()

Retrieves a specific queued task by ID.
Parameters:
  • id: The ID of the task to retrieve
Returns: The QueueItem with parsed payload or undefined if not found
The payload is automatically parsed from JSON before being returned
Example:

getQueues()

Retrieves all queued tasks that match a specific key-value pair in their payload.
Parameters:
  • key: The key to filter by in the payload
  • value: The value to match
Returns: Array of matching QueueItem objects
This method fetches all queue items and filters them in memory by parsing each payload and checking if the specified key matches the value
Example:

How Queue Processing Works

1

Validation

When calling queue(), the method validates that the callback exists as a function on the agent
2

Automatic Processing

After queuing, the system automatically attempts to flush the queue
3

FIFO Order

Tasks are processed in the order they were created (created_at timestamp)
4

Context Preservation

Each queued task runs with the same agent context (connection, request, email)
5

Automatic Retries

If a callback fails, it is retried with exponential backoff (configurable per task)
6

Automatic Dequeue

Tasks are removed from the queue after successful execution or after all retry attempts are exhausted
7

Error Handling

If a callback method does not exist at execution time, an error is logged and the task is skipped
8

Persistence

Tasks are stored in the cf_agents_queues table and survive agent restarts

Queue Callback Methods

When defining callback methods for queued tasks, they must follow this signature:
Example:

Use Cases

Background Processing

Delayed Operations

Batch Operations

Best Practices

Keep Payloads Small

Payloads are JSON-serialized and stored in the database

Idempotent Operations

Design callback methods to be safe to retry

Error Handling

Include proper error handling in callback methods

Monitoring

Use logging to track queue processing

Cleanup

Regularly clean up completed or failed tasks if needed

Error Handling and Retries

Queued tasks are automatically retried on failure with exponential backoff. The default is 3 attempts. You can customize this per task:
If you need custom error handling in the callback:
See Retries for full documentation on retry options and patterns.

Integration with Other Features

The queue system works seamlessly with other Agent SDK features:
  • State Management: Access agent state within queued callbacks
  • Scheduling: Combine with schedule() for time-based queue processing
  • Retries: Built-in retry with exponential backoff. See Retries.
  • Context: Queued tasks maintain the original request context
  • Database: Uses the same database as other agent data

Limitations

  • Tasks are processed sequentially, not in parallel
  • No priority system (FIFO only)
  • Queue processing happens during agent execution, not as separate background jobs
  • Failed tasks are dequeued after all retry attempts are exhausted (no dead-letter queue)