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:- Initiation: Submit task request with
background: trueparameter - Task Creation: System creates background job and returns task ID immediately
- Execution: Agent processes task asynchronously in background
- Progress Updates: Task status updates as execution proceeds
- Completion: Task finishes with success or error state
- Result Retrieval: Results remain available for retrieval
- Cleanup: Completed tasks eventually expire and clean up
Task States
Tasks transition through these states:queued: Task accepted, waiting to startrunning: Task currently executingcompleted: Task finished successfullyfailed: Task encountered error and stoppedcancelled: 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:Customer Analytics
Customer Analytics
Financial Modeling
Financial Modeling
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
Make Tasks Self-Contained
Make Tasks Self-Contained
Set Realistic Timeouts
Set Realistic Timeouts
Include Progress Checkpoints
Include Progress Checkpoints
Implement Error Recovery
Implement Error Recovery
Monitoring and Management
Resource Management
Integration with Other Primitives
With Persistence
Background tasks maintain session state:With Hooks
Execute callbacks during background task lifecycle:With Traces
Monitor background task execution: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
Task Stuck in 'queued' State
Task Stuck in 'queued' State
Problem: Task not starting executionSolutions:
- Check concurrent task limits
- Verify account has available resources
- Review task priority settings
Cannot Retrieve Results
Cannot Retrieve Results
Problem: Results not available after completionSolutions:
- Verify task actually completed successfully
- Check result retention period (typically 7 days)
- Ensure using correct task ID
Task Timeout
Task Timeout
Problem: Task fails with timeout errorSolutions:
- Increase timeout setting
- Break into smaller subtasks
- Optimize task execution
Related Primitives
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