Subagents
A subagent is a second session that the first one starts. The model hands it a task, it works on its own, and its report comes back as the result of the call that started it.
A long job fills a model’s context with things it will never need again: a hundred search results, a file it read once. A subagent keeps that out. It reads, searches and tries things in a fresh context, and the parent sees only the report. The same trick gives you three things:
- A fresh context. A side task, such as finding every caller of a function, doesn’t crowd what the parent is doing.
- Parallel work. Several children can run at once, each on its own piece.
- A narrower tool set. A child can be given only the tools its job needs, and it can only ever have less authority than its parent.
Starting one
Section titled “Starting one”The model starts a subagent with the Task tool. It picks a type and writes
the task: a prompt that says everything the child needs to know and what to
report, because the child sees nothing else of the conversation.
Two types are built in. general has the same tools as the parent, for a
self-contained job. explore is read-only, for finding things: it can read
and search, and it runs in plan mode, so it can’t change anything. A host
adds its own types. In the zelk CLI they are Markdown files in
.zelk/agents, .agents/agents or .claude/agents, in the project or at
home, and they name the type’s tools, mode, model and effort. In Go,
WithSubagents takes the same
definitions. The model sees each type’s one-line description in the Task
tool’s own description, and the user can ask for one by name.
A call can also run in the background, which returns at once and leaves the child running, or in a git worktree of its own.
- Two children in parallel: Each child is a session of its own, and the parent keeps one record of each. They ran side by side, each in its own log under its own lease, and each carved its share out of the parent’s budget. The parent’s log holds its two calls and the records that link the logs. What a child didn’t spend went back. You ask: “Find who calls Task, and update the docs”. The parent session holds its lease and records the message. The model replies with a Task call. Task starts explore 5a75: a session of its own with its own log and lease. The call reserves 30k of the parent’s budget for it. The call returns while explore runs. The parent’s log gets one record that links to the child, 5a75, and none of the child’s events. Task starts general 5975: a session of its own with its own log and lease. The call reserves 30k of the parent’s budget for it. The call returns while general runs. The parent’s log gets one record that links to the child, 5975, and none of the child’s events. explore runs Grep “Task(”. The entry lands in its log, not the parent’s. general runs Read docs/tools.md. The entry lands in its log, not the parent’s. Neither child has the Task tool, so neither can start a child. The tree is one level deep. explore runs Read a.go. The entry lands in its log, not the parent’s. general runs Edit docs/tools.md. The entry lands in its log, not the parent’s. explore finishes having spent 8.4k of its 30k, and gives its lease back. The model replies with a Task call. The parent records how explore ended: one record, and the report in the call’s result. The call settles the reservation: 8.4k spent, 21.6k back. general finishes having spent 19k of its 30k, and gives its lease back. The parent records how general ended: one record, and the report in the call’s result. The call settles the reservation: 19k spent, 11k back.
- A child asks you: Where a host suspends for answers, a child’s question reaches you through its parent. The child suspends, its parent’s model relays the question, and your answer goes back down the same way, once. A host with a live callback, like the terminal UI, is asked directly instead, and the request names the child. You ask: “Set up the database”. The parent session holds its lease and records the message. The model replies with a Task call. Task starts general 5a75: a session of its own with its own log and lease. The call reserves 30k of the parent’s budget for it. general runs Read schema.sql. The entry lands in its log, not the parent’s. general needs an answer: “Which database?”. It suspends and gives its lease back. The Task call returns a report marked suspended, with the child’s request. The child isn’t finished, so the parent records no end for it. The parent’s model asks you the question with AskUserQuestion. That request, the parent’s own, suspends the session. You answer “SQLite” through the parent, which resumes on a new lease. TaskOutput hands the answer down. The child takes it as a command marked relayed, so its reviewer reads it as the parent’s words beside what you said. It resumes on a lease of its own. general runs Write db.go. The entry lands in its log, not the parent’s. general finishes having spent 11.2k of its 30k, and gives its lease back. The parent records how general ended: one record, and the report in the call’s result. The call settles the reservation: 11.2k spent, 18.8k back.
- A crash: The same call opens the same child. A child’s ID derives from its parent and the call, so after a crash the call that runs again finds the child it started, with its log, instead of starting a second. Nothing is done twice. You ask: “Migrate the schema”. The parent session holds its lease and records the message. The model replies with a Task call. Task starts general 5a75: a session of its own with its own log and lease. The call reserves 30k of the parent’s budget for it. general runs Read schema.sql. The entry lands in its log, not the parent’s. general runs Write 001.sql. The entry lands in its log, not the parent’s. The process dies. Neither session gives its lease back, and neither log has a record of the end. The logs are all that is left. A new process takes the parent’s lease at fence 8 and folds its log. The Task call is open, so its attempt is closed as lost, charged its whole reservation, and the call runs again with the same key. The call runs again with the same ID, s/3/0, which hashes to the same child, 5a75. Its log exists, so zelk reopens it on a new lease. The task command is already in that log and isn’t accepted twice. general runs Edit 001.sql. The entry lands in its log, not the parent’s. general finishes having spent 9.8k of its 30k, and gives its lease back. The parent records how general ended: one record, and the report in the call’s result. The call settles the reservation: 9.8k spent, 20.2k back.
A session of its own
Section titled “A session of its own”A child isn’t a thread of the parent’s run. It is a session: it has its own log, its own lease and its own share of the budget, and it runs its own loop on them.
- Its log. Everything the child does lands in its log: its prompt, its
model calls, its tool calls. The parent’s log holds the
Taskcall and two records that link the two logs. The child’s start is written with the call’s result, and its end is written by the call that learns how it ended. The parent’s log holds none of the child’s events. The link records name the child’s session, so its log can be found and its claims checked. - Its lease. Only the holder of a session’s lease writes its log, so a child’s lease is its own and is taken when it starts. It is a separate grant from the parent’s, with its own fence. Durability and recovery explains what a lease is for.
- Its share of the budget. When the host sets a subagent budget
(
WithSubagentBudget), theTaskcall reserves that share from the parent’s budget, and the child can’t spend past it. When the call reports how the child ended, it settles on what the child spent, and the rest goes back to the parent. The zelk command sets no subagent budget. In Go you set one. - Narrower everything. The child’s tools are the agent’s, cut down to
its type’s list, without the
Tasktools, scheduled jobs, orMemorySaveandMemoryForget: a worker on one part of a task doesn’t decide what you want remembered. Its permission policy is narrowed from the parent’s current one, and a type that asks for a wider mode runs in the parent’s, with a warning. Hooks apply inside it only if they were registered for children.
How results come back
Section titled “How results come back”In the foreground, the parent waits, and the child’s final report is the
result of the Task call. The result names the child’s session and how
its run ended, and it is marked partial when the child ran out of turns or
budget. Text in a report that imitates zelk’s own markers, such as a
<system-reminder> tag or a Human: line, is neutralized before the
parent reads it, so a child can’t pass itself off as the harness.
In the background, the call returns a handle at once. The model reads the
report later with TaskOutput, or stops the child with TaskStop. A child
that finishes, or stops to ask something, tells the parent’s run so that
the model needn’t poll. By default a background child stops when the
parent’s run ends. One started to live across replies, with lifetime: "conversation", stays with the agent until it is stopped.
A finished child keeps its history. TaskContinue gives it more work under
the parent’s current permissions, and TaskMessage steers a running child
or hands a finished one its next task.
Questions and approvals
Section titled “Questions and approvals”A child may need a person: a question the task didn’t answer, or an action the policy asks about. What happens depends on how the host takes input.
A host with a live callback, like the terminal UI, is asked at once. The request says which session asks, so the host can show whose question it is.
A host that suspends for answers sends the question up the tree. The child
suspends and gives its lease back. The Task call returns a report marked
suspended, with the child’s request. The parent’s model asks you with
AskUserQuestion, and that request, the parent’s own, suspends the
session. You answer through the parent, and the parent passes the answer
down with TaskOutput. The child takes it as a command marked relayed, so
its reviewer reads it as the parent model’s words, beside what you said
yourself. The answer is delivered once, even if the call runs again.
Limits
Section titled “Limits”A session runs at most five children at once. It also keeps at most 128
uncollected results. Past either, the Task call returns a plain error
that says what to collect or stop, instead of starting anything. A child
can’t start children of its own, because it never gets the Task tools, so
the tree is one level deep: a parent and its children. Each child’s share of
the budget is a limit too: it can’t spend past it.
When something crashes
Section titled “When something crashes”A child’s session ID is derived from the parent’s session and the Task
call’s effect ID, which is itself derived from the log. A call that runs
again after a crash therefore names the same child. Task carries an
idempotency key, so recovery runs it again with the same key. zelk finds the
child’s log, reopens it on a new lease and carries on. The task command is
already in that log and isn’t accepted twice, so the child isn’t started
twice and its report is returned once.
The attempt the crash cut off is closed as lost and charged its whole reservation, as any lost attempt is, and the retry reserves its own. Durability and recovery has the rest.
In the zelk CLI
Section titled “In the zelk CLI”In the terminal UI, a Task call is a card in the transcript. It counts the
child’s tool uses while it runs, and for a background child it says whether
the child is running, waiting for input or finished. Ctrl+B moves running subagents to
the background, and Background work… (/ps) lists them, opens a
subagent’s conversation as it goes and stops it. When a child asks
something, the dialog says which subagent asks and what it is for. The
command palette lists each subagent type, and picking one starts a request
for you to finish. The background work page has the
keys.
Where to go deeper
Section titled “Where to go deeper”Skills and subagents shows how a Go host
defines subagent types and bounds their budget. The durability contract
gives the exact rules for child allocations and links
and for trees. The loop
shows where a Task call sits among the other effects, and
Extending zelk compares a subagent with a tool, a
hook and an MCP server. Permissions says how a
child’s policy narrows.