A LeadAgent is an agent that can delegate tasks to specialized sub-agents.
It automatically has a delegate_to_agent tool that allows it to spawn
sub-agents based on registered AgentDefinitions.
Super class
Agent -> LeadAgent
Methods
Inherited methods
Agent$add_hook()Agent$add_turn()Agent$chat()Agent$chat_async()Agent$chat_structured()Agent$chat_structured_async()Agent$checkpoint()Agent$compact()Agent$cost()Agent$get_context_turns()Agent$get_cost()Agent$get_model()Agent$get_model_object()Agent$get_permission_mode()Agent$get_provider()Agent$get_system_prompt()Agent$get_tokens()Agent$get_tools()Agent$get_turns()Agent$interrupt()Agent$last_compaction()Agent$last_run()Agent$last_turn()Agent$list_checkpoints()Agent$load_mcp()Agent$load_session()Agent$load_skill()Agent$mcp_status()Agent$mcp_tools()Agent$on_tool_request()Agent$on_tool_result()Agent$pending_approval()Agent$provider()Agent$register_tool()Agent$register_tools()Agent$resolve_tool_result()Agent$resume_approval()Agent$rewind_files()Agent$run()Agent$run_async()Agent$run_sync()Agent$save_session()Agent$session_id()Agent$set_model()Agent$set_permission_mode()Agent$set_system_prompt()Agent$set_tools()Agent$set_turns()Agent$skills()Agent$stream()Agent$stream_async()Agent$token_count()Agent$turns()Agent$usage()
LeadAgent$new()
Create a new LeadAgent.
Usage
LeadAgent$new(
chat,
sub_agents = list(),
tools = list(),
system_prompt = NULL,
permissions = NULL,
usage_limits = NULL,
context_policy = ContextPolicy(),
enable_file_checkpointing = FALSE,
file_checkpoint_max_file_bytes = 50 * 1024^2,
file_checkpoint_max_journal_bytes = 250 * 1024^2,
working_dir = getwd(),
session_id = NULL,
run_context = list(),
agent_id = NULL,
agent_name = NULL,
fallback_chats = list()
)Arguments
chatAn ellmer Chat object
sub_agentsList of
agent_definition()objectstoolsAdditional tools for the lead agent
system_promptSystem prompt for the lead agent
permissionsPermissions for the lead agent (also applied to sub-agents)
usage_limitsOptional UsageLimits for each lead-agent run.
context_policyA ContextPolicy controlling automatic compaction and durable offloading of large tool results for the lead agent and its delegated agents.
enable_file_checkpointingWhether to journal reversible file preimages in one workspace journal shared by the lead agent and its delegated agents.
file_checkpoint_max_file_bytesMaximum bytes captured for one file preimage. Defaults to 50 MiB.
file_checkpoint_max_journal_bytesMaximum aggregate serialized bytes for workspace checkpoint records, markers, metadata, and pending captures. Defaults to 250 MiB.
working_dirWorking directory
session_idOptional stable session identifier used for correlation. A unique identifier is generated by default.
run_contextImmutable canonical product context inherited by lead runs and delegated agents.
agent_idOptional stable identifier for this LeadAgent instance.
agent_nameOptional human-readable LeadAgent name.
fallback_chatsOrdered configured fallback Chats for the lead. Child definitions inherit the selected provider, without an implicit fallback policy of their own. See Agent.
LeadAgent$register_sub_agent()
Register a new sub-agent definition.
Arguments
definitionAn
agent_definition()object
LeadAgent$parallel_delegate()
Run independent, stateless responders concurrently.
Each selected AgentDefinition gets a fresh conversation and at most one model request. Definitions with tools, skills, or MCP servers are rejected. This is tier-1 fan-out, not background tool-using agents. Results preserve input order, including failures and unstarted tasks.
Usage
LeadAgent$parallel_delegate(
tasks,
max_active = 2L,
mode = "stateless",
usage_limits = NULL,
run_context = list()
)Arguments
tasksA named character vector of tasks. Names select unique registered AgentDefinitions.
max_activeMaximum simultaneous responders.
modeExecution contract. Currently only
"stateless"is supported.usage_limitsOptional batch-wide UsageLimits. Unset fields inherit the lead's defaults. Requests are reserved before dispatch; token and cost ceilings are divided across each concurrent wave and checked after responses, with possible overage by one response per active responder.
run_contextAdditional immutable context for the batch.
Returns
A list with mode, named results (AgentResult or NULL),
named errors, named status, and an aggregate run (AgentResult).
$last_run() retains the aggregate run. The lead's conversation is
unchanged. Failed responders do not discard successful siblings.
LeadAgent$parallel_delegate_async()
Run stateless fan-out without blocking the R event loop.
Usage
LeadAgent$parallel_delegate_async(
tasks,
max_active = 2L,
mode = "stateless",
usage_limits = NULL,
run_context = list()
)LeadAgent$get_subagent_results()
Get retained results from delegated sub-agent runs.
Arguments
agent_nameOptional sub-agent name filter
delegation_idOptional delegation identifier filter
Returns
List of AgentResult objects or NULL entries for failed runs