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 baseAgent 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.callback: The name of the method to call when processing the taskpayload: Data to pass to the callback methodoptions.retry: Optional retry configuration. See Retries for details.
dequeue()
Removes a specific task from the queue by ID.id: The ID of the task to remove
dequeueAll()
Removes all tasks from the queue.dequeueAllByCallback()
Removes all tasks that match a specific callback method.callback: Name of the callback method
getQueue()
Retrieves a specific queued task by ID.id: The ID of the task to retrieve
The payload is automatically parsed from JSON before being returned
getQueues()
Retrieves all queued tasks that match a specific key-value pair in their payload.key: The key to filter by in the payloadvalue: The value to match
This method fetches all queue items and filters them in memory by parsing each payload and checking if the specified key matches the value
How Queue Processing Works
1
Validation
When calling
queue(), the method validates that the callback exists as a function on the agent2
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 restartsQueue Callback Methods
When defining callback methods for queued tasks, they must follow this signature: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: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