How to Spawn a Subagent? #1734
|
I want to spawn a subagent by tool-call. when executing the subagent tool, the result is the handle of async task of spawning subagent instead of waiting subagent finished. How do I know the subagent is finished? how to keep the current turn running to wait subagent finished? |
Replies: 1 comment 1 reply
|
The Rig way to model this is to make the subagent a tool. Rig already implements That means the current turn stays running automatically. You do not need to return a task handle or manually check whether the subagent has finished. There is an existing example in the repo: The key part is: let calculator_agent = openai_client
.agent(providers::openai::GPT_4O)
.preamble("You are a calculator...")
.tool(Adder)
.tool(Subtract)
.build();
let agent_using_agent = openai_client
.agent(providers::openai::GPT_4O)
.preamble("You are a helpful assistant...")
.tool(calculator_agent)
.build();In this workflow:
So for your use case, instead of spawning the subagent and returning an async task handle, register the subagent itself as a tool: let sub_agent = client
.agent(openai::GPT_5_2)
.name("researcher")
.description("Handles focused research tasks")
.preamble("You are a focused research subagent.")
.default_max_turns(4)
.build();
let parent_agent = client
.agent(openai::GPT_5_2)
.preamble("Use the researcher tool when delegation is useful.")
.tool(sub_agent)
.default_max_turns(4)
.build();
let answer = parent_agent
.prompt("Research this topic and summarize the result.")
.max_turns(4)
.await?;The important detail is that the subagent tool returns only after the subagent has produced its final answer. That final answer is what the parent agent receives as the tool result. |
The Rig way to model this is to make the subagent a tool.
Rig already implements
ToolforAgent<M>, so an agent can be passed directly into another agent with.tool(sub_agent). When the parent agent calls that tool, Rig awaits the subagent'sprompt(...)call and uses the subagent's final answer as the tool result.That means the current turn stays running automatically. You do not need to return a task handle or manually check whether the subagent has finished.
There is an existing example in the repo:
examples/agent_with_agent_tool.rs.The key part is: