Sub-agents in OpenClaw are spawned sessions that run in isolation from the parent agent. The sessions_spawn command creates a new session with its own context window, tools, and sandbox scope, allowing an orchestrator agent to delegate tasks to specialized workers without polluting its own context. This is the foundation of the orchestrator pattern: a coordinator agent breaks a complex task into pieces, spawns sub-agents to handle each piece, collects results, and synthesizes a final response. Sub-agents can run in parallel, share results through explicit handoff, and are cleaned up automatically when they complete.
Master openclaw sub-agents & sessions_spawn
Take the Introduction to OpenClaw course with hands-on lessons and challenges.
The --parent-session flag links the sub-agent to its orchestrator. The --wait flag blocks until the sub-agent completes and returns its result. Without --wait, the spawn returns immediately with the session ID for later polling.
The orchestrator agent has access to sessions_spawn (to create sub-agents), sessions_list (to monitor them), and sessions_result (to collect their output). The file-reviewer agent is the worker template. The orchestrator spawns one file-reviewer per changed file, all running in parallel with isolated sandboxes.
The sessions list output shows the parent-child relationship between orchestrator and sub-agents. Each sub-agent has an independent status. The orchestrator can check results from completed sessions and handle errors from failed ones without its own session being affected.
Spawn options let the orchestrator control cost and security per sub-agent. Override the model to use a cheaper one for simple tasks. Set timeouts to prevent runaway sub-agents. Restrict sandbox-paths to limit file access to only what the sub-agent needs.
This script demonstrates the full orchestration loop: spawn sub-agents in parallel with --no-wait, wait for all to finish, then collect results sequentially. In practice, the orchestrator agent handles this logic internally using its tools, but the CLI equivalent helps visualize the workflow.
Spawning sub-agents without setting sandbox scope to 'tree', allowing sub-agents to have broader permissions than the parent
Always set sandbox scope to 'tree' on the orchestrator agent. This propagates sandbox restrictions to all spawned sub-agents, ensuring they cannot access files or tools outside the parent's allowed scope.
Spawning too many sub-agents in parallel without considering API rate limits and cost
Each sub-agent makes its own API calls. Spawning 50 sub-agents simultaneously can hit rate limits and generate unexpected costs. Use a concurrency limit (--max-concurrent) or batch sub-agents in groups of 5-10.
Using the parent session's context to pass large amounts of data to sub-agents instead of using file references
Sub-agents have their own context windows. Pass file paths or references instead of raw data. A sub-agent can read the file directly using its tools, avoiding context window bloat in the parent.
Not handling sub-agent errors in the orchestrator, causing the entire workflow to fail silently when one sub-agent errors
Check the status of each sub-agent after spawning. Use sessions result with error handling to detect failures. Implement retry logic or graceful degradation for non-critical sub-agent failures.
Sub-agents in OpenClaw are spawned via sessions_spawn to run isolated tasks under an orchestrator. The orchestrator pattern decomposes complex tasks, spawns specialized sub-agents in parallel, and synthesizes results. Each sub-agent has its own context window, sandbox, and lifecycle. Use sandbox scope 'tree' to propagate security restrictions, set timeouts to prevent runaway sessions, and monitor with sessions list. Sub-agents are ephemeral by default and cleaned up on completion.
Interactive lessons and challenges on Stanza, practice in VS Code, Cursor, or the web.
Interactive lessons and challenges, right in your code editor.
Check the free courses. No credit card.