# Subagents

> How the model hands a piece of work to a session of its own, what the child shares with its parent and what it doesn't, how its answers and questions travel, and how a crash reopens it.

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

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`](/go/skills-subagents/#subagents) 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.

A subagent is a tree of sessions: the parent, and a child for each `Task` call, at most 5 at once. The parent’s budget is 200k tokens and each child’s share is 30k. A child has no `Task` tool, so the tree is one level deep.

- **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.
   1. You ask: “Find who calls Task, and update the docs”. The parent session holds its lease and records the message.
   2. The model replies with a Task call.
   3. 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.
   4. 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.
   5. 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.
   6. 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.
   7. explore runs Grep “Task(”. The entry lands in its log, not the parent’s.
   8. general runs Read docs/tools.md. The entry lands in its log, not the parent’s.
   9. Neither child has the Task tool, so neither can start a child. The tree is one level deep.
   10. explore runs Read a.go. The entry lands in its log, not the parent’s.
   11. general runs Edit docs/tools.md. The entry lands in its log, not the parent’s.
   12. explore finishes having spent 8.4k of its 30k, and gives its lease back.
   13. The model replies with a Task call.
   14. 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.
   15. general finishes having spent 19k of its 30k, and gives its lease back.
   16. 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.
   1. You ask: “Set up the database”. The parent session holds its lease and records the message.
   2. The model replies with a Task call.
   3. 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.
   4. general runs Read schema.sql. The entry lands in its log, not the parent’s.
   5. general needs an answer: “Which database?”. It suspends and gives its lease back.
   6. 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.
   7. The parent’s model asks you the question with AskUserQuestion. That request, the parent’s own, suspends the session.
   8. You answer “SQLite” through the parent, which resumes on a new lease.
   9. 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.
   10. general runs Write db.go. The entry lands in its log, not the parent’s.
   11. general finishes having spent 11.2k of its 30k, and gives its lease back.
   12. 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.
   1. You ask: “Migrate the schema”. The parent session holds its lease and records the message.
   2. The model replies with a Task call.
   3. 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.
   4. general runs Read schema.sql. The entry lands in its log, not the parent’s.
   5. general runs Write 001.sql. The entry lands in its log, not the parent’s.
   6. 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.
   7. 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.
   8. 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.
   9. general runs Edit 001.sql. The entry lands in its log, not the parent’s.
   10. general finishes having spent 9.8k of its 30k, and gives its lease back.
   11. 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

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 `Task` call 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](/concepts/durability/) explains what a lease is for.
- **Its share of the budget.** When the host sets a subagent budget
  (`WithSubagentBudget`), the `Task` call 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 `Task` tools, scheduled jobs, or
  `MemorySave` and `MemoryForget`: 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

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

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

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

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](/concepts/durability/) has the rest.

## 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](/cli/background/) page has the
keys.

## Where to go deeper

[Skills and subagents](/go/skills-subagents/#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](/internals/durability/#b7)
and for [trees](/internals/durability/#p3). [The loop](/concepts/loop/)
shows where a `Task` call sits among the other effects, and
[Extending zelk](/concepts/extending/) compares a subagent with a tool, a
hook and an MCP server. [Permissions](/concepts/permissions/) says how a
child's policy narrows.
