Dispatch many subagent calls in parallel from inside the REPL, with bounded concurrency, by importing a skill the agent pulls in on demand.
The interesting part is that the orchestration logic — the semaphore
pool, task dispatch, result collection — lives inside a skill
(skills/swarm/index.ts), not in Python. The agent imports it with:
const { runSwarm } = await import("@/skills/swarm");…runs it, and gets a structured summary back.
repl_swarm/
├── skills/
│ └── swarm/
│ ├── SKILL.md # frontmatter (module: ./index.ts) + prose docs
│ └── index.ts # the runSwarm() executor
├── swarm_agent.py # thin driver that wires everything up
└── README.md # this file
swarm_agent.pycreates acreate_deep_agentwithskills=[".../skills"]andCodeInterpreterMiddleware(ptc=["task"]).SkillsMiddlewareparsesSKILL.mdfrontmatter, including the newmodulekey, and writes aSkillMetadataentry into state.- When the model writes
await import("@/skills/swarm")inside anevalcall,CodeInterpreterMiddlewarescans the source, loads the skill dir via the backend, builds aModuleScopewithindex.ts(oxidase strips TypeScript types at install time), and callsctx.install. - Guest code imports
runSwarm, which callstools.task(...)through the PTC layer.Promise.allplus a worker-pool keeps at mostconcurrencycalls in flight. - Results come back in input order; the skill returns a
SwarmSummarywith completion counts.
uv run python swarm_agent.py
# custom task:
uv run python swarm_agent.py "Use the swarm skill to summarize these files: ..."The default task asks the agent to write three different numbers to three different paths in parallel — a concrete proof the fan-out actually runs.
- Many independent subtasks. If the subtasks don't share state and don't depend on each other's output, a swarm saves round trips.
- Stable subtask shape. Each task is just a
descriptionstring (optionally an overridesubagentType). If your subtasks need richer structured input, extend the skill.
- Sequential dependencies. If task B needs task A's output, use
plain
tools.taskwith an ordinaryawaitchain. - Hundreds of tasks. The default
concurrency=5cap (hard-max 10) is intentional — subagent invocations aren't free. For mass fan-out at thousands of tasks, batch them in a job queue, not a REPL call.
- LangChain Academy — Comprehensive, free courses on LangChain libraries and products, made by the LangChain team.
- Code of Conduct — community guidelines and standards