agentleFS
lauren-frameworklauren-framework/lauren-ai/skills/sub-agent-spawning/SKILL.md

sub-agent-spawning

Skill0 starsChanged 6 months agoMITView on GitHub

Shows how an orchestrator agent spawns multiple sub-agents and aggregates their results. Use when building hierarchical multi-agent systems where a coordinator delegates specialised tasks to sub-agents, either sequentially or in parallel via asyncio.gather.

The file

We split this skill into the rules it gives an agent

1 rule. About 0 are common, 0 are distinctive, and 1 is very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈0

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    When sub-agents live in different AgentModules, inject via AgentRunner[SubAgentClass]:

    line 90

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Use codemap find "SymbolName" to locate any symbol before reading — it gives exact file + line range and is faster than grep across the whole repo.

Sub-Agent Spawning & Result Aggregation

Overview

AgentRunnerBase.run() can be called multiple times with different @agent classes. An orchestrator function calls runner.run(sub_agent, prompt) for each delegated task and collects AgentResponse.content strings.

Sequential and parallel patterns are both straightforward:


Sequential sub-agent spawning

# orchestrator.py — from __future__ import annotations is safe here
from __future__ import annotations
import asyncio
from lauren_ai import agent
from lauren_ai._agents._runner import AgentRunnerBase as AgentRunner

@agent(model=None, system="You are a research specialist.")
class ResearchSubAgent: ...

@agent(model=None, system="You are a writing specialist.")
class WritingSubAgent: ...

async def spawn_sequential(runner: AgentRunner, prompts: list[tuple]) -> list[str]:
    """Run sub-agents one at a time and collect results."""
    results = []
    for sub_agent, prompt in prompts:
        response = await runner.run(sub_agent, prompt)
        results.append(response.content)
    return results

Parallel sub-agent spawning

async def spawn_parallel(runner: AgentRunner, prompts: list[tuple]) -> list[str]:
    """Run sub-agents concurrently and collect all results."""
    tasks = [runner.run(sub_agent, prompt) for sub_agent, prompt in prompts]
    responses = await asyncio.gather(*tasks)
    return [r.content for r in responses]

asyncio.gather runs all coroutines on the same event loop. Because MockTransport uses a shared dequeue, parallel tests must queue enough responses for every concurrent call.


Runner setup for tests

from lauren_ai._agents._runner import AgentRunnerBase as AgentRunner
from lauren_ai._config import LLMConfig
from lauren_ai._transport import Completion, TokenUsage
from lauren_ai._transport._mock import MockTransport

def _make_runner(mock=None):
    if mock is None:
        mock = MockTransport()
    cfg = LLMConfig(provider="anthropic", model="mock-model", api_key="mock")
    runner = AgentRunner(transport=mock)
    return runner, mock

def _completion(content="OK", *, n=1):
    return Completion(
        id=f"c{n}", model="mock-model", content=content,
        tool_calls=[], stop_reason="end_turn",
        usage=TokenUsage(input_tokens=10, output_tokens=5),
    )

Cross-module AgentRunner injection

When sub-agents live in different AgentModules, inject via AgentRunner[SubAgentClass]:

class OrchestratorService:
    def __init__(self, research: AgentRunner[ResearchSubAgent]) -> None:
        self._research = research

    async def run_task(self, topic: str) -> str:
        response = await self._research.run(ResearchSubAgent(), topic)
        return response.content

Reference files

FileContents
src/lauren_ai/_agents/_runner.pyAgentRunnerBase.run()
src/lauren_ai/_agents/__init__.py@agent(), AgentResponse
src/lauren_ai/_transport/_mock.pyMockTransport

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.