Skip to main content
Background Tasks enable agents to execute long-running operations asynchronously, allowing your application to remain responsive while complex work continues in the background.

Overview

The Background Tasks primitive allows agents to perform time-intensive operations without blocking your application. Instead of waiting for completion, you can initiate a background task, receive a task ID, and check status or retrieve results later. Background tasks are essential for:
  • Long-Running Operations: Multi-hour data processing, analysis, or generation tasks
  • Async Workflows: Decouple request initiation from result consumption
  • Batch Processing: Process large datasets without timeout constraints
  • Scheduled Jobs: Execute recurring agent tasks on a schedule
  • Resource-Intensive Tasks: Complex computations without blocking other operations

Non-Blocking Execution

Initiate tasks and continue without waiting for completion

Status Tracking

Monitor progress, check status, and receive notifications when complete

Reliable Completion

Tasks continue running even if client disconnects or crashes

Result Retrieval

Fetch results when ready, with full session context preserved

How Background Tasks Work

Task Lifecycle

Background tasks follow a well-defined lifecycle:
  1. Initiation: Submit task request with background: true parameter
  2. Task Creation: System creates background job and returns task ID immediately
  3. Execution: Agent processes task asynchronously in background
  4. Progress Updates: Task status updates as execution proceeds
  5. Completion: Task finishes with success or error state
  6. Result Retrieval: Results remain available for retrieval
  7. Cleanup: Completed tasks eventually expire and clean up

Task States

Tasks transition through these states:
  • queued: Task accepted, waiting to start
  • running: Task currently executing
  • completed: Task finished successfully
  • failed: Task encountered error and stopped
  • cancelled: Task was manually cancelled
Persistent Execution: Background tasks continue running even if your application disconnects. Results remain available until you retrieve them.

Code Examples

Basic Background Task

Polling for Completion

Webhook Notifications

Batch Background Processing

Use Cases

1. Large-Scale Data Analysis

Process massive datasets without timeout constraints:

2. Content Generation at Scale

Generate large volumes of content asynchronously:

3. Scheduled Reports

Generate periodic reports in background:

4. ETL Pipelines

Run complex data pipelines asynchronously:

5. Machine Learning Training

Train models in background:

6. Web Scraping Jobs

Large-scale web scraping operations:

Best Practices

Task Design

Monitoring and Management

Resource Management

Integration with Other Primitives

With Persistence

Background tasks maintain session state:
Learn more: Persistence Primitive

With Hooks

Execute callbacks during background task lifecycle:
Learn more: Hooks Primitive

With Traces

Monitor background task execution:
Learn more: Traces Primitive

Performance Considerations

Task Overhead

  • Submission: < 100ms to queue task
  • Status Check: < 50ms to check task status
  • Result Retrieval: 100-500ms depending on result size

Concurrency Limits

  • Default Limit: 50 concurrent background tasks per account
  • Enterprise Limit: Configurable based on needs
  • Queue Depth: Unlimited queued tasks

Resource Optimization

Troubleshooting

Problem: Task not starting executionSolutions:
  • Check concurrent task limits
  • Verify account has available resources
  • Review task priority settings
Problem: Results not available after completionSolutions:
  • Verify task actually completed successfully
  • Check result retention period (typically 7 days)
  • Ensure using correct task ID
Problem: Task fails with timeout errorSolutions:
  • Increase timeout setting
  • Break into smaller subtasks
  • Optimize task execution

Persistence

Session state for background tasks

Hooks

Lifecycle callbacks for tasks

Traces

Monitor background execution

Self-Healing

Automatic error recovery in tasks

Additional Resources

API Reference

Background task parameters

API Events

Configure webhook notifications

Use Cases

Background task examples
Remember: Background tasks are perfect for operations that take more than a few seconds. Use webhooks for notifications and implement proper monitoring for production workloads.