Overview
Background task tools enable launching agents asynchronously and polling their results. This allows parallel exploration, multi-agent orchestration, and non-blocking execution. Source:src/tools/background-task/
Architecture
Tools provide LLM-facing interface to background execution engine:Tools
background_output
Fetch results from running or completed background task.Task ID returned from
task() with run_in_background=trueExample: "bg-task-1234567890"Wait for completion (default:
false)false: Check status immediately, return current statetrue: Poll until task completes or timeout
Max wait time in milliseconds (default:
60000, max: 600000)Only applies when block=true.Return full session transcript (default:
true)true: All messages with filters appliedfalse: Summary status only
Include thinking/reasoning blocks (default:
false)Auto-enabled for active tasks.Max messages to return (capped at 100)Example:
message_limit: 10 returns last 10 messages.Return messages after this message ID (exclusive)For incremental polling: track last seen message ID.
Include tool results in output (default:
false)Auto-enabled for active tasks.Max characters for thinking content (default:
2000)Truncates long reasoning blocks.Task result or statusCompleted task (full_session=true):Running task (block=false):Error state:
create-background-output.ts:44
background_cancel
Cancel running background task(s).Task ID to cancelMutually exclusive with
all.Cancel all running tasks (default:
false)Mutually exclusive with taskId.Cancellation resultSingle task:All tasks:Not found:
create-background-cancel.ts
Usage Patterns
Launch and Poll
Parallel Exploration
Incremental Result Fetching
Timeout Handling
Task Lifecycle
Background tasks go through these states:- pending: Task created, session starting
- running: Session active, agent executing
- completed: Session idle, agent finished
- error: Execution failed
- cancelled: User cancelled
- interrupt: System interrupted (e.g., parent cancelled)
Concurrency Limits
BackgroundManager enforces per-model concurrency limits (default: 5):Output Formatting
Results formatted for LLM consumption:- Task metadata: Agent, category, description, status
- Messages: Role, content, thinking blocks (optional)
- Tool results: Truncated if
include_tool_results=false - Timestamps: Human-readable durations
Error Handling
Task Not Found
Task Deleted During Polling
Session Start Timeout
Implementation Details
Key Files
| File | Purpose |
|---|---|
create-background-output.ts:44 | background_output tool implementation |
create-background-cancel.ts | background_cancel tool implementation |
full-session-format.ts | Format full session transcript |
task-result-format.ts | Format task result |
task-status-format.ts | Format task status |
session-messages.ts | Fetch session messages from OpenCode |
clients.ts | Client interfaces for BackgroundManager |
Session Message Fetching
Status Polling
Related Tools
- task: Launch agents (sync or background)
- call_omo_agent: Direct agent invocation (also supports background)
- session_read: Read completed session history
Best Practices
✅ Do
- Use
run_in_background=trueonly for parallel exploration - Let system notify on completion (don’t poll aggressively)
- Use
since_message_idfor incremental fetching - Set reasonable timeouts (default 60s is usually sufficient)
- Cancel abandoned tasks to free resources
❌ Don’t
- Don’t poll every second — system notifies automatically
- Don’t use background for sequential work — use sync mode
- Don’t ignore timeout errors — task may still be running
- Don’t leak task IDs — track and cancel when no longer needed