← writing

Your Agent Should Delegate More, and Better

Long agent sessions fill up with things the agent needed once and will never look at again. It greps through forty files to find where a config value gets set, reads twelve of them, and in the end needs one line. All forty results and twelve files stay in its context for the rest of the session.

With a subagent, the main agent hands that search to a second agent that has its own context window. The subagent does the reading and sends back the one line, and the forty grep results go away with it.

  without a subagent

  ┌ main agent context ──────────────────────────────┐
  │ task │ grep × 6 │ 12 files read │ answer │ ...   │
  └──────────────────────────────────────────────────┘
         └───── stays forever ──────┘

  with a subagent

  ┌ main agent context ──────────────────────────────┐
  │ task │ spawn │ answer │ ...                      │
  └──────────────────────────────────────────────────┘
            │      ▲
            ▼      │ 1 line
          ┌ subagent context ─────────────────┐
          │ grep × 6 │ 12 files read │ answer │
          └───────────────────────────────────┘
            thrown away when it's done

Spawning one is easy. Most of my time went into the handoff: what the subagent knows when it starts, how long it may run, and how its result gets back to the main agent.

The handoff

When the main agent spawns a subagent, it writes a brief for someone who has never seen the session. The subagent knows what's in that brief and whatever context the harness passes along, and nothing else.

  main agent ──► spawn_agent
                 ┌─────────────────────────────────────┐
                 │ task       what to do, and why      │
                 │ context    fresh, or forked         │
                 │ tools      which ones it may use    │
                 │ max_turns  how long it may run      │
                 │ background wait for it, or not      │
                 │ returns    what the answer has to   │
                 │            look like                │
                 └─────────────────────────────────────┘
                                │
                                ▼
                            subagent

A call to the spawn tool looks something like this:

spawn_agent({
  task: "Find where the provider timeout is set. " +
        "Return the file, line and default value.",
  context: "fork",          // or "fresh"
  tools: ["read", "grep", "glob"],
  max_turns: 15,
  background: true,
});

Give the subagent only the tools its task needs. For research that means read-only tools, so the main agent stays the only one changing files.

Ask for a specific shape of answer, too. "Find out how auth works" comes back as three paragraphs the main agent then has to read, while "return the file and line where the token is checked" comes back as one line it can act on.

Fork the context, or start fresh

A subagent can start with an empty context and just the brief, or with a fork of the main agent's context: everything the main agent has seen so far, plus the task.

  fresh                     fork

  ┌ subagent ─────────┐     ┌ subagent ───────────────┐
  │ system prompt     │     │ system prompt           │
  │ brief             │     │ the session so far      │
  └───────────────────┘     │ task                    │
                            └─────────────────────────┘

  fresh   knows only the brief, so the brief has
          to be complete. no inherited bias.
  fork    knows what the main agent knows, so the
          brief can be one line. cheap if the
          cache hits.

A fresh subagent hasn't picked up the main agent's assumptions, which is what you want for a second opinion, like reviewing a diff. The brief has to carry everything, though, down to file paths and what's already been ruled out. When the main agent writes a lazy one, the subagent redoes work the main agent already did.

A forked subagent already knows the user's goal and what's been tried, so the brief can be one line. Sending the whole session again sounds expensive, but the fork starts with the same prefix as the main agent, so the provider serves that part from the prompt cache. You pay full price only for the task at the end.

  main   │ system │ tools │ session so far │ next turn
         └───────── cached prefix ─────────┘
  fork   │ system │ tools │ session so far │ task
         └──── same prefix, cache hit ─────┘

The prefix has to match byte for byte. If you change the system prompt or hand the subagent a different tool list, the cache misses and you pay for the whole context again. When a forked subagent should have fewer tools, I keep the definitions identical and block the extra ones when it calls them.

I fork for work that continues the main thread, and start fresh when I want an independent take.

Limit the turns

A subagent with no limit wanders. It reads one more file and tries one more search, and twenty turns later it's still going. The main agent can't see any of that, and if it's waiting, so is the user.

I give every subagent a turn budget sized to the task. When the budget runs out, the subagent stops and reports what it has.

  max_turns: 15

  turn  1 ▓  grep "timeout"
  turn  2 ▓  read provider.ts
  turn  3 ▓  read config.ts
   ...
  turn 12 ▓  found it
  turn 13 ░
  turn 14 ░  unused
  turn 15 ░

  hit the limit?  stop, return what you found,
                  what you didn't, and where you'd look next

If the subagent gets cut off with nothing written down, every turn it took was wasted. So the harness warns it a few turns before the end ("3 turns left, start wrapping up"), and it writes a partial answer instead of starting another search. The main agent then gets something like "found the default in config.ts, didn't find where providers override it, check providers/*.ts next", and can decide whether to spawn again or finish the job itself.

Run it in the background

If the main agent waits for every subagent, it still does one thing at a time. In the background, a subagent returns an id right away and the main agent keeps working, or spawns another and lets both run in parallel.

  time ────────────────────────────────────────────────►

  main     spawn A, spawn B, keeps working ...  uses A + B
                              ▲             ▲
                              │             │
  sub A    ├─ search code ────┤ done        │
  sub B             ├─ run the tests ───────┤ done

Some rules I'd keep:

  • Background subagents shouldn't edit the same files as the main agent, or each other. Read-only work is the safest to run in the background.
  • Cap how many run at once. Five subagents in parallel is five times the tokens in the same minute.
  • The main agent needs a way to check on one, and a way to cancel it if the plan changes.
  • Tell the main agent not to guess what a subagent found before it reports back. Models do this more than you'd expect: they "predict" the result and carry on as if it were real.

Wake the main agent

The main agent shouldn't have to poll. When a background subagent finishes, the harness puts a message into the main agent's conversation, and starts a new turn if the agent was idle.

  subagent finishes
       │
       ▼
  harness builds a notification
       │
       ├─ main agent busy?   queue it for its next turn
       │
       └─ main agent idle?   wake it with the notification

The notification should carry enough for the main agent to act on it right away:

  <subagent-result id="a3f" status="completed">
    task:     find where the provider timeout is set
    turns:    12 of 15
    result:   src/providers/config.ts:41, default 60s.
              openai.ts:88 overrides it to 120s.
    files:    read 9, changed 0
  </subagent-result>

For errors, "the subagent failed" tells the main agent nothing. It needs to know what went wrong (a crash, the turn limit, a cancel, a tool error) and how far the subagent got:

  <subagent-result id="b71" status="error">
    task:     run the test suite and report failures
    error:    turn limit reached (20 of 20)
    partial:  14 of 16 test files run, 2 failures,
              both in auth.test.ts (token refresh)
    next:     the 2 remaining files are e2e and slow
  </subagent-result>

From that, the main agent can fix the two failures and run the last two files itself. Given only "failed", it would spawn the same subagent again and hit the same limit.

Teaching the main agent when to spawn

I spent more time tuning this than anything else in the post. A main agent that never spawns fills its context. One that spawns for everything is slow and expensive, since a subagent for a one-call lookup costs a whole extra agent run, and the main agent gets back a summary with less detail than the file itself.

The model decides from the spawn tool's description and the system prompt, so that's where the tuning goes. Mine says roughly this:

  use a subagent when the work means reading across
  many files and you only need the conclusion, when
  tasks are independent and can run in parallel, or
  for a second opinion (give that one a fresh context).

  do it yourself when it's one or two tool calls, or
  you already know which file to open. once you've
  handed something off, don't redo it while you wait.
  does the task need more than ~3 tool calls?
     │
     ├─ no ──► do it yourself
     │
     ▼ yes
  do you need the details, or only the conclusion?
     │
     ├─ details ──► do it yourself
     │
     ▼ conclusion
  does it depend on what you're doing right now?
     │
     ├─ yes ──► spawn, fork the context, wait for it
     │
     └─ no ───► spawn in the background, keep working

Criteria only get you part of the way, because models follow examples better than rules. So the description also has a few pairs: this request, spawn, and this one, don't. Then I log every spawn decision with the task that triggered it and look for two patterns:

  • Spawns that finished in one or two turns were probably not worth a subagent.
  • Sessions where the main agent read dozens of files itself, and its context filled up, probably needed one.

Each case I find becomes another example in the description, and I rerun the same sessions to check the change helped.

Where I'd start

If you're adding subagents to an agent today, start with read-only subagents that run in the background, with a turn limit and a notification that says what happened. That covers the common case, a search that would have filled the main context, and it can't break anything. Forking and the spawn tuning can come once you have logs to tune against.