s13: Background Tasks — Slow Operations Go to the Background
s01 → ... → s11 → s12 → s13 → s14 → s15 → ... → s20
"Slow operations go to the background, agent continues processing" — Background threads run commands, inject notifications when done.
Harness Layer: Background — Async execution, doesn't block the main loop.
The Problem
Ever used a washing machine? Throw clothes in, press start, then go do other things — cook, reply to messages, read papers. 30 minutes later the machine beeps: done. You don't stand there waiting for 30 minutes.
The agent's bash tool is the same. pip install torch takes 10 minutes, npm run build takes 3 minutes. While these commands run, the agent waits for bash to return, unable to use that time to process other tasks.
Reading files is milliseconds, no wait. git status returns in under a second, no wait. But npm install? Minutes. The agent waits 10 minutes doing nothing, and LLM calls are billed by token — idle time is waste.
The Solution
)
Teaching code carries forward S12's simplified task system and prompt assembly; to stay focused on background tasks, it omits full error recovery, memory, and skill systems. The only change: slow operations go to background threads, the agent continues running the loop, and background results are injected as notifications.
Sync vs Background:
How It Works
should_run_background: Explicit Request First, Heuristic Fallback
The model explicitly requests background execution via the bash tool's run_in_background parameter. If the model doesn't specify, the teaching version falls back to keyword heuristics:
CC's bash tool schema has a run_in_background: boolean parameter (BashTool.tsx:241). The model decides which commands go to background, no keyword guessing. The teaching version keeps heuristics as fallback, but the primary path is explicit model request.
start_background_task: Background Execution and Lifecycle
Wraps the tool call in a worker function, dispatches to a daemon thread. Each background task gets a unique ID, with state tracked in the background_tasks dict:
Returns bg_id instead of just [Running in background...]. daemon=True ensures threads exit when the agent process exits. The teaching version uses in-memory dicts for tracking; real CC has LocalShellTaskState, output redirected to files, with full lifecycle including stopping tasks and reading subsequent output.
collect_background_results: Notification Collection
When background tasks complete, results are collected and formatted as <task_notification> messages:
Notifications don't reuse the original tool_use_id. The original tool call was already answered with a placeholder tool_result; background completion is an independent event, injected in task_notification format. This respects Messages API tool pairing: one tool_use gets exactly one tool_result.
Loop Integration
In the agent loop, tool execution splits into two paths. Notifications and results merge into a single user message:
Slow operations get a placeholder tool_result with bg_id, so the LLM knows this command is still running and can do other things first. When background completes, the notification is injected as an independent text block alongside the current turn's tool_results in one user message.
The teaching version polls background results while the agent loop continues running. Real CC uses a notification queue (messageQueueManager.ts) to deliver background completion events to subsequent turns, without waiting for the tool loop.
Putting It Together
The agent didn't wait — while npm install ran in the background, it read the config file.
Changes from s12
Try It
Try these prompts:
Run pip list in the background and find all Python files in this directoryRun npm install (use run_in_background) and while waiting, read package.jsonCreate a task to setup the project, then run pip list in the background
What to observe: Are slow operations dispatched to background? Is a bg_id returned? Are background notifications injected in <task_notification> format?
What's Next
Background tasks solved "slow operations don't block." But what if you want to do something on a schedule? Like "run tests every morning at 9am" or "check server status every 5 minutes."
s14 Cron Scheduler → Give the agent an alarm clock.
Deep Dive into CC Source
The following is a complete analysis based on CC source code
query.ts(lines 211, 1054-1060, 1411-1482),services/toolUseSummary/toolUseSummaryGenerator.ts(L15 prompt text),LocalShellTask.tsx(L24-25 constants, L59-98 watchdog logic),messageQueueManager.ts(notification queue),utils/task/framework.ts(L267enqueueTaskNotification).
1. pendingToolUseSummary: Haiku Background Generation
CC starts a Haiku side-query after each batch of tool executions to generate a tool use summary. Initiated at query.ts:1411-1482, prompt text defined at services/toolUseSummary/toolUseSummaryGenerator.ts:15 (variable TOOL_USE_SUMMARY_SYSTEM_PROMPT). The prompt is "Write a short summary label... think git-commit-subject, not sentence", past tense, ~30 characters.
Haiku summary (~1s) completes during the main model's streaming output (5-30s). Before the next turn starts, the summary is yielded. SDK consumers use these summaries for mobile progress display.
2. Thread Model: No Real Threads
CC runs on Node.js/Bun's single-threaded event loop. "Background" just means "don't await". ShellCommand.background(taskId) redirects stdout/stderr to files, letting the process run independently.
3. Seven Background Task Types
CC defines 7 background task types (Task.ts:7-13): local_bash, local_agent, remote_agent, in_process_teammate, local_workflow, monitor_mcp, dream. Each has its own registration, lifecycle, and notification mechanism.
4. Notification Injection: Command Queue
When a background task completes, it's enqueued via enqueueTaskNotification (utils/task/framework.ts:267) or enqueuePendingNotification (messageQueueManager.ts) into a shared command queue. The notification format is structured XML:
Priority is next > later (messageQueueManager.ts). Background tasks default to later (don't block user input). Consumption point at query.ts:1566-1593.
5. Stall Watchdog
Background bash tasks have a watchdog (LocalShellTask.tsx L24-25 constants, L59-98 logic) that periodically checks if output has stalled. After 45 seconds with no growth, it detects interactive prompts ((y/n) etc.), preventing background tasks from getting stuck on unanswered interactive dialogs.
6. Concurrency Limits
Foreground tool calls: CLAUDE_CODE_MAX_TOOL_USE_CONCURRENCY (default 10 concurrent safe tools). Background bash tasks: no hard limit, they're independent subprocesses.
