Background Tasks
Background tasks allow you to run recipes and agents asynchronously without blocking your main application. Tasks run in the background with full progress tracking, cancellation support, and result retrieval.Python API
Running a Recipe in Background
BackgroundTaskHandle
Therun_background() function returns a BackgroundTaskHandle with these methods:
| Method | Description |
|---|---|
task_id | Unique task identifier |
recipe_name | Name of the recipe being executed |
session_id | Session ID for conversation continuity |
status() | Get current task status (async) |
wait(timeout) | Wait for completion and return result (async) |
cancel() | Cancel the running task (async) |
Task Status Values
pending- Task is queued but not startedrunning- Task is currently executingcompleted- Task finished successfullyfailed- Task failed with an errorcancelled- Task was cancelled
Using with Agents Directly
Configuration
Safe Defaults
Background tasks use safe defaults to prevent runaway execution:| Setting | Default | Description |
|---|---|---|
timeout_sec | 300 | Maximum execution time (5 minutes) |
max_concurrent | 5 | Maximum concurrent tasks |
cleanup_delay_sec | 3600 | Time before completed tasks are cleaned up |
TEMPLATE.yaml Runtime Block
Configure background task defaults in your recipe’sTEMPLATE.yaml:
Error Handling
Best Practices
- Always set timeouts - Prevent tasks from running indefinitely
- Use session IDs - Track related tasks across executions
- Handle cancellation - Clean up resources when tasks are cancelled
- Monitor progress - Use status checks for long-running tasks
- Limit concurrency - Don’t overwhelm system resources
See Also
- Background Tasks CLI - CLI commands for background tasks
- Async Jobs - Server-based job execution
- Scheduler - Periodic task scheduling

