Background Agent System
The Background Agent System provides a framework for creating autonomous agents that execute tasks on a schedule. These agents are ideal for system monitoring, periodic reports, and data synchronization.BackgroundAgentService
TheBackgroundAgentService is the primary manager for background agents. It handles scheduling, status tracking, and resource management.
Creating a Background Agent
To create a background agent, define its configuration including theinterval (in seconds) and the query it should run.
Management API
The service provides a comprehensive API to manage your background fleet:| Method | Description |
|---|---|
start_agent(agent_id) | Begin the scheduled execution for an agent. |
pause_agent(agent_id) | Temporarily stop the agentβs schedule. |
resume_agent(agent_id) | Resume a paused agent. |
stop_agent(agent_id) | Completely stop and remove the agent from the schedule. |
get_agent_status(agent_id) | Get detailed metrics (run count, error count, last run time). |
list_agents() | List all managed background agents. |
Observability
Background agents emit events just like standard agents. You can stream these events to monitor βself-flyingβ tasks in real-time.Event Types
background_task_startedbackground_task_completedbackground_task_errorbackground_agent_status
Use Cases
1. ποΈ Database Archival
Run an agent every 24 hours to find old records and archive them to S3 or a secondary database.2. π¨ Sytem Health Checks
Monitor disk space, memory usage, and service availability every 60 seconds.3. π° Content Aggregator
An agent that fetches news from several sources once an hour, summarizes them, and saves the result to your blog or newsletter database.Best Practices
- Timeout Management: Ensure your
tool_call_timeoutandintervalare balanced. If a task takes 10 minutes but the interval is 5 minutes, you may experience overlaps. - Resource Limits: Set
request_limitandtotal_tokens_limitin the agent config to prevent runaway costs if an agent gets stuck in a loop. - Persistent Storage: Always use
redisordatabasememory and event stores for background agents to ensure you donβt lose progress if your main process restarts.