Sub topic 14
Planning and sub-agents
This chapter assumes you have read the earlier chapters on the agent loop, stop conditions, and context management. You should be comfortable with a loop that calls a model, runs tools, and appends results to a message list.
Why plan first
A long task such as "rename this API across the codebase" can fail in the middle of the loop. The model loses track of what it has finished, repeats work, or edits files in an order that breaks the build. A plan is a short piece of text written into the context early. It lists the steps, so later model calls can see what is done and what remains.
Planning needs no new machinery. The harness can ask the model for a plan as ordinary text, or provide a tool such as update_plan that stores a checklist and echoes it back. Because the model only sees what is in the context, the plan helps only if it stays there. Keep it short and update it as steps finish.
Why delegate
Every file read and command output stays in the context window. A broad investigation, such as "find where authentication is configured", can fill the window with search results the main task never needs again.
A sub-agent is a second agent loop started by the first one. It gets its own fresh message list, does the noisy work, and returns only a short answer. The parent's context grows by the summary, not by everything the sub-agent read.
A minimal delegate tool
The sub-agent is the same run_agent function called with a new task. This sketch assumes run_agent, TOOLS, and a call_model placeholder from earlier chapters.
def make_delegate(call_model, max_steps=10, max_chars=3000):
def delegate(task):
# Fresh messages: the sub-agent does not see the parent's history.
answer = run_agent(task, call_model, max_steps=max_steps)
return answer[:max_chars]
return delegate
TOOLS["delegate"] = make_delegate(call_model)Three details matter here:
- The sub-agent starts with only the task string. The parent must put everything the sub-agent needs into that string.
- A smaller step limit keeps a stuck sub-agent from burning budget.
- The returned text is cut to a fixed length, for the same reason as other tool output.
Trade-offs and common errors
Delegation is not free. Each sub-agent call costs extra model calls, and the parent trusts a summary it cannot check.
| Problem | Cause | Fix |
|---|---|---|
| Sub-agent answers the wrong question | Task string left out needed detail | Name files, goals, and the expected answer format |
| Summary hides a key fact | The sub-agent compressed too much | Ask for file paths and line numbers, not conclusions only |
| Runaway recursion | Sub-agents can call `delegate` too | Remove `delegate` from the sub-agent's tools or track depth |
| Conflicting edits | Two sub-agents change the same file | Delegate read-only research, and keep edits in the parent |
A good rule is to delegate work that is read-heavy and has a short answer. Keep work in the parent when it needs the full conversation or makes edits.
Exercise
A parent agent's context is nearly full after it read 30 files to locate a bug. Next, it must fix the bug. Which work should have been delegated, and what should the sub-agent return?
Show answer
The search and reading of the 30 files should have gone to a read-only sub-agent. It should return a short result: the file paths, line numbers, and a brief note on the cause. The parent then does the edit with a small context.