CDX-301h · Module 3
Dynamic Agent Spawning
3 min read
Static pipelines pre-define the number and type of agents. Dynamic spawning lets the supervisor create agents at runtime based on the task's actual requirements. If the decomposition reveals five independent modules, the supervisor spawns five workers. If it reveals two, it spawns two. This elastic scaling matches agent resources to task complexity without over-provisioning or under-provisioning.
Dynamic spawning requires the supervisor to make runtime decisions about model selection, context allocation, and concurrency limits. A well-designed supervisor uses heuristics: routine tasks get fast models, complex tasks get reasoning models, tasks that read large files get models with larger context windows. The supervisor must also respect concurrency limits — spawning twenty parallel agents may exceed API rate limits or produce diminishing returns from coordination overhead.
from agents import Agent, Runner
from dataclasses import dataclass
@dataclass
class TaskSpec:
description: str
files: list[str]
complexity: str # "routine", "moderate", "complex"
def select_model(spec: TaskSpec) -> str:
"""Choose model based on task complexity."""
models = {
"routine": "gpt-4.1",
"moderate": "codex-1",
"complex": "o3",
}
return models.get(spec.complexity, "codex-1")
def spawn_workers(specs: list[TaskSpec],
max_concurrent: int = 5) -> list[Agent]:
"""Dynamically create agents matched to task requirements."""
workers = []
for i, spec in enumerate(specs[:max_concurrent]):
worker = Agent(
name=f"worker-{i}",
model=select_model(spec),
instructions=f"""
Task: {spec.description}
Files to modify: {", ".join(spec.files)}
Follow project conventions in AGENTS.md.
Report completion with a structured summary.
""",
)
workers.append(worker)
return workers
- Set concurrency limits Define the maximum number of concurrent agents based on your API rate limits and cost budget. Typical limit: 5-10 concurrent workers.
- Map complexity to models Create a model selection policy: routine tasks → fast models, complex tasks → reasoning models. The supervisor applies this policy when spawning.
- Monitor spawn patterns Track how many agents each pipeline run spawns. Consistently high counts may indicate decomposition granularity issues; consistently low counts may indicate tasks are too coarse.