Skip to main content

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

The run_background() function returns a BackgroundTaskHandle with these methods:

Task Status Values

  • pending - Task is queued but not started
  • running - Task is currently executing
  • completed - Task finished successfully
  • failed - Task failed with an error
  • cancelled - Task was cancelled

Using with Agents Directly

Configuration

Safe Defaults

Background tasks use safe defaults to prevent runaway execution:

TEMPLATE.yaml Runtime Block

Configure background task defaults in your recipe’s TEMPLATE.yaml:

Error Handling

Best Practices

  1. Always set timeouts - Prevent tasks from running indefinitely
  2. Use session IDs - Track related tasks across executions
  3. Handle cancellation - Clean up resources when tasks are cancelled
  4. Monitor progress - Use status checks for long-running tasks
  5. Limit concurrency - Don’t overwhelm system resources

See Also