btw_tool_agent_subagent() is a btw tool that enables hierarchical agent
workflows. When used by an LLM assistant (like btw_app(), btw_client(),
or third-party tools like Claude Code), this tool allows the orchestrating
agent to delegate complex tasks to specialized subagents, each with their own
isolated conversation thread and tool access.
This function is primarily intended to be called by LLM assistants via tool
use, not directly by end users.
How Subagents Work
When an LLM calls this tool:
A new chat session is created (or an existing one is resumed)
The subagent receives the prompt and begins working with only the tools
specified in the tools parameter
The subagent works independently, making tool calls until it completes
the task
The function returns the subagent's final message text and a session_id
The orchestrating agent can resume the session later by providing the
session_id
Each subagent maintains its own conversation context, separate from the
orchestrating agent's context. Subagent sessions persist for the duration of
the R session.
Tool Access
The orchestrating agent must specify which tools the subagent can use via
the tools parameter. The subagent is restricted to only these tools - it
cannot access tools from the parent session. Tools can be specified by:
Specific tool names: c("btw_tool_files_read_text_file", "btw_tool_files_write_text_file")
Tool groups: "files" includes all file-related tools
NULL (default): Uses the default tool set from options or
btw_tools()
Configuration Options
Subagent behavior can be configured via R options:
btw.subagent.client: The ellmer::Chat client or provider/model string
to use for subagents. If not set, falls back to btw.client, then to the
default Anthropic client.
btw.subagent.tools_default: Default tools to provide to subagents when
the orchestrating agent doesn't specify tools via the tools parameter.
If not set, falls back to btw.tools, then all btw tools from
btw_tools(). This is a convenience option for setting reasonable
defaults.
btw.subagent.tools_allowed: An allowlist of tools that subagents are
allowed to use at all. When set, any tools requested (either explicitly via
the tools parameter or from defaults) will be filtered against this list.
If disallowed tools are requested, an error is thrown. This provides a
security boundary to restrict subagent capabilities. If not set, all
btw_tools() are allowed.
These options follow the precedence: function argument > btw.subagent.*
option > btw.* option > default value. The tools_allowed option acts as a
filter on top of the resolved tools, regardless of their source.