Sub-agents¶
Run graphs as sub-agents of a node, in this process or on a background executor. See Sub-agents.
| Name | What it is |
|---|---|
| Spawn and gather | |
spawn_agents | Start sub-agents concurrently on their own threads. |
gather_agents | Wait for sub-agents and collect their results. |
AgentTask | A graph to run as a sub-agent. |
AgentHandle | Handle to a sub-agent running in the current process. |
AgentResult | Outcome of a sub-agent run. |
AgentStatus | Progress of a sub-agent, derived from its own stream events. |
| Background executors | |
AgentExecutor | Protocol for running sub-agents in the background. |
AsyncioExecutor | Background executor that runs sub-agents as asyncio tasks in this process. |
DurableAgentHandle | Handle to a sub-agent submitted to an AgentExecutor. |
| Registry | |
AgentRegistry | Tracks the sub-agents spawned during one graph run. |
Spawn and gather¶
spawn_agents¶
async spawn_agents(tasks, *, parent_thread_id=None, thread_prefix=None, registry=None) -> list[AgentHandle]
Start sub-agents concurrently on their own threads.
Each sub-agent runs on the thread "<prefix>:sub:<handle id>", or
"sub:<handle id>" without a prefix. Every state_in runs before any
sub-agent starts.
tasks | list[AgentTask] |
Sub-agents to start. |
parent_thread_id | str | None |
Prefix for the generated thread ids. |
thread_prefix | str | None |
Thread prefix; wins over |
registry | AgentRegistry | None |
Registry that tracks the handles. |
TypeError |
If a task has no |
ValueError |
If |
gather_agents¶
async gather_agents(handles, *, timeout=None, return_exceptions=False, registry=None) -> list[AgentResult]
Wait for sub-agents and collect their results.
handles | Sequence[AgentHandle | str] |
Handles returned by |
timeout | float | None |
Seconds to wait before cancelling the unfinished sub-agents. |
return_exceptions | bool |
Return failed and cancelled results, with the exception in
|
registry | AgentRegistry | None |
Registry the handles were registered with. |
list[AgentResult] |
Results in the order of |
AgentTimeoutError |
If the timeout expires first. |
AgentGroupError |
If a sub-agent failed or was cancelled and |
AgentHandleLostError |
If a handle is not in |
TypeError |
If a handle id is given without |
AgentTask¶
AgentTask(graph, input, name=None, state_in=None, state_out=None, on_progress=None, context=None)
A graph to run as a sub-agent.
graph | Graph |
Graph to run. |
input | Any |
Input of the sub-agent's run. |
name | str | None |
Display name; defaults to the graph name. |
state_in | Callable[[Any], Any] | None |
Maps |
state_out | Callable[[dict[str, Any]], dict[str, Any]] | None |
Maps the final state dict to the result's |
on_progress | Callable[[AgentStatus], None] | None |
Called with the |
context | object | None |
|
AgentHandle¶
AgentHandle(id, name, task, graph_name, thread_id)
Handle to a sub-agent running in the current process.
id | str | |
name | str | |
task | Task[AgentResult] |
The asyncio task running the sub-agent. |
graph_name | str | |
thread_id | str |
Thread the sub-agent runs on. |
Methods
done()¶
Whether the sub-agent has finished.
done()¶done() -> bool
async status()¶
Return the current status of the sub-agent.
async status()¶async status() -> AgentStatus
AgentResult¶
AgentResult(id, name, state, data, status, error=None, thread_id=None, checkpoint_id=None)
Outcome of a sub-agent run.
id | str |
Id of the handle the sub-agent was started with. |
name | str | |
state | Any |
Final state as the graph's state type; |
data | dict[str, Any] |
Final state as a dict, or what |
status | str |
|
error | BaseException | None |
What stopped a failed or cancelled run. |
thread_id | str | None |
Thread the sub-agent ran on. |
checkpoint_id | str | None |
History event to load the final or interrupted state at; |
AgentStatus¶
AgentStatus(id, name, status, step, node, thread_id, updated_at)
Progress of a sub-agent, derived from its own stream events.
id | str |
Handle id of the sub-agent. |
name | str |
Display name of the sub-agent. |
status | Literal['running', 'completed', 'interrupted', 'error', 'cancelled'] |
Where the sub-agent is. |
step | int |
Number of supersteps the sub-agent's thread has finished; 0 before the first one finishes. |
node | str | None |
Last node that finished; |
thread_id | str | None |
Thread the sub-agent runs on. |
updated_at | float |
|
Background executors¶
AgentExecutor¶
Protocol for running sub-agents in the background.
Methods
async submit(task, ...)¶
Start a sub-agent.
async submit(task, ...)¶async submit(task, *, thread_id=None, thread_prefix=None) -> DurableAgentHandle
task | AgentTask | |
thread_id | str | None |
Thread to run on. |
thread_prefix | str | None |
Without |
TypeError |
If the task has a |
async poll(handle_id)¶
Return the result if the sub-agent finished, else None.
async poll(handle_id)¶async poll(handle_id) -> AgentResult | None
handle_id | str | |
AgentResult or None |
A |
AgentHandleLostError |
If the executor does not know |
async status(handle_id)¶
Return the current status of a sub-agent.
async status(handle_id)¶async status(handle_id) -> AgentStatus
handle_id | str | |
AgentHandleLostError |
If the executor does not know |
async cancel(handle_id)¶
Cancel a running sub-agent.
async cancel(handle_id)¶async cancel(handle_id) -> bool
handle_id | str | |
bool |
Whether a sub-agent was cancelled. |
AgentHandleLostError |
If the executor does not know |
async list_running()¶
List the sub-agents that are still running.
async list_running()¶async list_running() -> list[DurableAgentHandle]
AsyncioExecutor¶
AsyncioExecutor(*, executor_id='asyncio')
Background executor that runs sub-agents as asyncio tasks in this process.
Keeps every sub-agent it started, finished or not, until forget.
executor_id | str |
Id stored on the handles it creates; |
Methods
async submit(task, ...)¶
Start a sub-agent as an asyncio task.
async submit(task, ...)¶async submit(task, *, thread_id=None, thread_prefix=None) -> DurableAgentHandle
task | AgentTask | |
thread_id | str | None |
Thread to run on. |
thread_prefix | str | None |
Without |
TypeError |
If the task has a |
ValueError |
If |
async poll(handle_id)¶
Return the result if the sub-agent finished, else None.
async poll(handle_id)¶async poll(handle_id) -> AgentResult | None
handle_id | str | |
AgentResult or None |
A |
AgentHandleLostError |
If the executor does not know |
async status(handle_id)¶
Return the current status of a sub-agent.
async status(handle_id)¶async status(handle_id) -> AgentStatus
handle_id | str | |
AgentHandleLostError |
If the executor does not know |
async cancel(handle_id)¶
Cancel a running sub-agent and wait until it stopped.
async cancel(handle_id)¶async cancel(handle_id) -> bool
handle_id | str | |
bool |
Whether a sub-agent was cancelled; False for sub-agents that had already finished. |
AgentHandleLostError |
If the executor does not know |
async forget(handle_id)¶
Release a finished or cancelled sub-agent; its id is unknown afterwards.
async forget(handle_id)¶async forget(handle_id) -> None
handle_id | str | |
AgentHandleLostError |
If the executor does not know |
ValueError |
If the sub-agent is still running. |
async list_running()¶
List the sub-agents that are still running.
async list_running()¶async list_running() -> list[DurableAgentHandle]
DurableAgentHandle¶
DurableAgentHandle(id, name, executor_id, graph_name, thread_id=None)
Handle to a sub-agent submitted to an AgentExecutor.
id | str | |
name | str | |
executor_id | str |
|
graph_name | str | |
thread_id | str | None |
Thread the sub-agent runs on. |
Registry¶
AgentRegistry¶
Tracks the sub-agents spawned during one graph run.
Methods
register(handle)¶
Remember a spawned sub-agent.
register(handle)¶register(handle) -> None
handle | AgentHandle | |
resolve(handle_id)¶
Look up a spawned sub-agent.
resolve(handle_id)¶resolve(handle_id) -> AgentHandle
handle_id | str | |
AgentHandleLostError |
If the handle is not registered. |
remove(handle_id)¶
Forget a sub-agent.
remove(handle_id)¶remove(handle_id) -> None
handle_id | str | |
async cancel_all()¶
Cancel and forget every sub-agent that was not gathered.
async cancel_all()¶async cancel_all() -> list[AgentHandle]
list[AgentHandle] |
The sub-agents that were still registered, in spawn order, including finished ones whose results nobody collected. |