Skip to content
mentoor

Topic 5 of 5 · 5 min read

Delegation loops and unbounded agent spawning

Goal: Spot a delegation loop or unbounded spawning in a run log and add the depth, count and time limits that stop it.

If nothing in the system limits depth, count or time, an agent that can delegate will eventually delegate forever. The loop is rarely a crash. It is a run that keeps spending tokens and never finishes.

Check yourself · 1 of 3

A run log shows planner -> coder and coder -> planner repeating the same two tasks. What is this?

Choose one answer

Why it matters

Anthropic reported that early versions of its research system spawned dozens of subagents for simple queries and kept searching for sources that did not exist. It also reported that multi-agent runs use many times more tokens than a single chat, so a runaway run is an expensive one. A loop on a shared production machine, or one that calls paid APIs, costs real money before anyone notices.

Three shapes

  1. Ping-pong. Agent A hands the task to B, B decides it is A's job and hands it back. Nobody works.
  2. Fan-out explosion. A lead agent spawns 5 workers, and each worker can spawn 5 more. Depth 4 is 625 agents.
  3. Deadlock. A waits for B's result and B waits for A's. Nothing errors, nothing moves.

How to spot it in a log

Look for repeated pairs (A -> B, B -> A), the same task text appearing again, a growing count of agent ids, tool calls with no new output, and long gaps with no events.

The limits to add

  • Depth. Subagents cannot spawn subagents, or only to a fixed depth.
  • Count. A maximum number of agents per run and per parent.
  • Turns and time. A maximum number of turns per agent and a wall-clock timeout for the run.
  • Budget. A token or cost ceiling that ends the run.
  • Loop detection. If the same task text is delegated twice, stop and report.
  • Effort rules in the prompt. For example: "a simple lookup uses one agent and at most 5 tool calls".

Before and after

# Before: any agent can spawn, with no ceiling
def delegate(task):
    return spawn_agent(task)

# After: depth, count and a repeat check
MAX_DEPTH, MAX_AGENTS = 2, 8
seen = set()

def delegate(task, depth, run):
    if depth >= MAX_DEPTH:
        raise Limit("depth", depth)
    if run.agent_count >= MAX_AGENTS:
        raise Limit("agents", run.agent_count)
    key = hash(task.strip().lower())
    if key in seen:
        raise Limit("repeat", task[:60])
    seen.add(key)
    run.agent_count += 1
    return spawn_agent(task, depth + 1)

What goes wrong

  • Limits on the framework default only. The default turn limit may be high, or may not cover handoffs.
  • A limit that fails silently. The run ends with no report. Return what was done and which limit fired.
  • No timeout on waiting. A blocked agent waits for ever and holds its resources.

Examples

The ping-pong log

10:01 planner  -> coder    task="implement export"
10:02 coder    -> planner  task="clarify export format"
10:02 planner  -> coder    task="implement export"
10:03 coder    -> planner  task="clarify export format"

The same two tasks alternate, and nothing new is learned. The planner never answers the question, and the coder cannot continue without the answer. Fix: a repeat check that stops the second identical handoff, and a rule that a clarification question goes to the user.

The fan-out

A lead agent is told to "research every competitor in depth". It spawns 6 workers. Each worker can also spawn helpers and does so for every sub-topic. The log shows 140 agent ids in 12 minutes. Setting a maximum depth of 1 for workers and 8 agents per run caps the run at 9 agents.

Key points

  • Without limits on depth, count and time, an agent that can delegate will eventually delegate forever.
  • Loops show in logs as repeated agent pairs, repeated task text, a growing agent count or long silent gaps.
  • Set a maximum depth, a maximum agents per run, a turn limit per agent and a wall-clock timeout.
  • Add a repeat check so the same task is not delegated twice, and a token or cost budget.
  • When a limit fires, return a partial result and name the limit, instead of ending without a report.

Common mistakes

  • Trusting the framework's default turn limit without checking whether it covers handoffs and spawns.
  • Letting every agent in the tree hold the spawn tool.
  • Ending the run on a limit with no output, so nobody knows what happened.
  • Waiting on another agent's result with no timeout.

Check yourself · 2 of 3

Each of 5 workers may spawn 5 helpers, who may spawn 5 more. How do you cap the total?

Choose one answer

Check yourself · 3 of 3

A run hits its turn limit and ends with an empty response. What should it do instead?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic