使用隔离的工具集和执行上下文跨专门的子 Agent 协调复杂的任务。
子 Agent 可以帮助您的主 Agent 处理复杂的任务。主 Agent 可以分解任务并将子任务委托给子 Agent,其中每个子 Agent 都有自己独立的上下文窗口。
根据您的使用案例,您可以利用 动态自克隆子 Agent 或 静态自定义子 Agent。
动态自克隆子 Agent
您的主 Agent 可以创建继承其权限和工具集的子 Agent。
例如,您可以在执行期间启用动态子 Agent 创建,以便主 Agent 可以根据需要生成子 Agent:
from google.antigravity import Agent, LocalAgentConfig, types
config = LocalAgentConfig(
capabilities=types.CapabilitiesConfig(enable_subagents=True)
)
async with Agent(config) as agent:
response = await agent.chat("Decompose and audit this codebase.")
print(await response.text())
静态自定义子 Agent
您可以利用 SDK 定义具有与主 Agent 不同的专用工具或特定系统指令的子 Agent。请注意,分配给子 Agent 的任何自定义工具也必须在父 Agent 的 tools 列表中注册。
例如,您可以配置专门负责代码审查的子 Agent:
from google.antigravity import Agent, LocalAgentConfig, types
def my_custom_reviewer_tool(file_path: str) -> str:
"""Audits docstrings in a python file."""
# Add your custom tool logic here
return f"Verified {file_path}"
reviewer = types.SubagentConfig(
name="code_reviewer",
description="Audits source code for style compliance.",
system_instructions="Check function docstrings in Python files.",
tools=[my_custom_reviewer_tool],
)
config = LocalAgentConfig(
tools=[my_custom_reviewer_tool],
subagents=[reviewer],
)
async with Agent(config) as agent:
response = await agent.chat(
"Check function docstrings in Python files under src/."
)
print(await response.text())
示例代码
有关完整的工作代码示例,请查看 GitHub 存储库: