external-subagents
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseExternal Subagents
外部子代理
Overview
概述
This skill teaches agents how to invoke other coding agent CLIs (Claude Code, Codex CLI, Gemini CLI) as external subagents when the user explicitly requests it. The core principle is that different models provide different perspectives -- a second opinion from a different model can catch issues, suggest alternatives, or validate approaches that a single model might miss.
本技能用于指导Agent在用户明确要求时,调用其他编码Agent CLI(Claude Code、Codex CLI、Gemini CLI)作为外部子代理。核心原则是不同模型能提供不同视角——来自不同模型的第二意见可以发现单一模型可能遗漏的问题、提出替代方案或验证方法。
When to Use
使用场景
Only invoke an external subagent when the user explicitly asks. Look for phrases like:
- "Get a second opinion from Codex"
- "Have Gemini review this"
- "Run this through Claude Code"
- "Ask another agent to look at this"
- "Use a different model to analyze this"
- "Get Codex/Gemini/Claude to do X"
Do NOT use this skill when:
- The user has not asked for an external agent -- never invoke autonomously
- The task is something you can do yourself and the user has not requested delegation
- The user asks you to "think harder" or "try again" -- that is about your own reasoning, not delegation
- No supported CLI is installed on the system
- You are already the agent mentioned by the user. For example, if you are Claude Code, and the user requested to perform review using claude, you should use your own Task tool to create subagents instead of invoking another Claude instance using Bash. If you are Claude Code or Claude, you MUST not launch . If you are Codex or Codex CLI, you MUST not launch
claude. If you are Gemini or Gemini CLI, you MUST not launchcodex.gemini
仅在用户明确要求时调用外部子代理。留意以下表述:
- “让Codex给出第二意见”
- “让Gemini审核这个内容”
- “通过Claude Code运行这个”
- “让另一个Agent查看这个”
- “使用不同模型分析这个”
- “让Codex/Gemini/Claude执行X操作”
禁止使用本技能的场景:
- 用户未要求调用外部Agent时——禁止自主调用
- 任务可由你自行完成且用户未要求委托时
- 用户要求你“再想想”或“重试”时——这是要求你自行推理,而非委托
- 系统中未安装任何受支持的CLI时
- 你本身就是用户指定的Agent时。例如,如果你是Claude Code,而用户要求用Claude进行审核,你应使用自身的Task工具创建子代理,而非通过Bash调用另一个Claude实例。如果你是Claude Code或Claude,绝对不能启动命令;如果你是Codex或Codex CLI,绝对不能启动
claude命令;如果你是Gemini或Gemini CLI,绝对不能启动codex命令。gemini
Core Flow
核心流程
-
Detect available CLIs. Run(or check each individually) to see which agent CLIs are installed. Each line of output corresponds to a found binary. If none are found, inform the user and stop. This command must be ran on host, running inside sandbox will not work.
which claude codex gemini -
Select the tool. If the user specified which agent to use, use that one. If they said something generic like "get a second opinion", suggest from the available CLIs and confirm with the user, or pick one if only one is available.
-
Check sandbox status. Determine if you are running in a sandbox. If so, you will need to escape the sandbox for the invocation command (see Sandbox Escape below). The subagent itself still runs sandboxed.
-
Build the command. Construct the CLI invocation with:
- The task as the prompt (include relevant file paths and context)
- Flags for non-interactive, sandboxed execution with full permissions
- The current working directory so the subagent operates on the same project
- Any skill or context file paths the subagent should read (see Passing Context below)
-
Execute. Run the command in the background if your tooling supports it (see Background Execution). Otherwise, run in the foreground and wait for completion.
-
Report results. Present the subagent's stdout output to the user. Summarize key findings if the output is long.
-
检测可用CLI:运行(或分别检查每个命令)查看已安装的Agent CLI。输出的每一行对应一个已找到的二进制文件。如果未找到任何CLI,需告知用户并终止流程。此命令必须在主机上运行,在沙箱内执行将无法生效。
which claude codex gemini -
选择工具:如果用户指定了要使用的Agent,直接使用该Agent;如果用户使用诸如“获取第二意见”之类的通用表述,需从可用CLI中给出建议并征得用户确认,或在仅有一种CLI可用时直接选择该CLI。
-
检查沙箱状态:确定你是否在沙箱中运行。如果是,你需要在调用命令时跳出沙箱(参见下文“沙箱逃逸”)。子代理本身仍会在沙箱中运行。
-
构建命令:构建CLI调用命令时需包含:
- 作为提示词的任务描述(包含相关文件路径和上下文)
- 用于非交互式、带完整权限的沙箱执行的标志
- 当前工作目录,以便子代理在同一项目中操作
- 子代理需要读取的所有技能或上下文文件路径(参见下文“传递上下文”)
-
执行命令:如果你的工具支持,可在后台运行命令(参见下文“后台执行”);否则,在前台运行并等待执行完成。
-
汇报结果:将子代理的标准输出内容呈现给用户。如果输出内容过长,需总结关键发现。
Command Templates
命令模板
Claude Code
Claude Code
bash
claude --settings '{"sandbox":{"enabled":true,"autoAllowBashIfSandboxed":true}}' \
-p "Your task description here."bash
claude --settings '{"sandbox":{"enabled":true,"autoAllowBashIfSandboxed":true}}' \
-p "Your task description here."Codex CLI
Codex CLI
bash
codex exec --full-auto "Your task description here."bash
codex exec --full-auto "Your task description here."Gemini CLI
Gemini CLI
bash
gemini -p "Your task description here." \
--approval-mode yolo --sandboxNotes:
- All three CLIs output plain text to stdout by default.
- No flag is included -- let each CLI use its default model.
--model - The prompt string should contain the full task description, relevant file paths, and any instructions for the subagent.
- See the reference files for additional flags and options.
bash
gemini -p "Your task description here." \
--approval-mode yolo --sandbox注意事项:
- 三个CLI默认均向标准输出输出纯文本。
- 未包含标志——让每个CLI使用其默认模型。
--model - 提示词字符串应包含完整的任务描述、相关文件路径以及对子代理的所有指令。
- 更多标志和选项请参考参考文件。
Sandbox Escape
沙箱逃逸
If the parent agent (you) is running in a sandbox, the subagent CLI binary may not be accessible or allowed to execute inside that sandbox. You must escape the sandbox for the invocation only -- the subagent itself still runs in its own sandbox with full permissions.
Per-agent mechanisms:
-
Claude Code as parent: Seton the Bash tool call that runs the subagent command. This lets the invocation run on the host. The subagent CLI then manages its own sandbox independently.
dangerouslyDisableSandbox: true -
Other agents as parent: Use your agent's equivalent sandbox-bypass mechanism for the invocation command only. Thesandbox flag (Claude Code),
--settingsflag (Codex), and--full-autoflag (Gemini) ensure the subagent itself runs sandboxed regardless.--sandbox
Key principle: Only the invocation escapes the parent's sandbox. The subagent process runs with its own sandboxing and permission model.
如果父Agent(即你)在沙箱中运行,子代理CLI二进制文件可能无法在该沙箱内访问或执行。你必须仅在调用时跳出沙箱——子代理本身仍会在其自身的沙箱中运行,并拥有完整权限。
各Agent的实现机制:
-
当父Agent为Claude Code时:在运行子代理命令的Bash工具调用中设置。这会让调用命令在主机上运行。子代理CLI随后会独立管理自身的沙箱。
dangerouslyDisableSandbox: true -
当父Agent为其他类型时:使用你的Agent对应的沙箱绕过机制仅处理调用命令。Claude Code的沙箱标志、Codex的
--settings标志以及Gemini的--full-auto标志可确保子代理本身始终在沙箱中运行。--sandbox
核心原则:仅调用过程会跳出父沙箱。子代理进程会使用自身的沙箱和权限模型运行。
Passing Context and Skills
传递上下文和技能
Enrich the subagent's prompt with relevant context so it can do useful work:
-
Skill files: Tell the subagent to read skill files by including absolute paths in the prompt text. Do not use system prompt flags -- put instructions directly in the prompt.
-
Working directory: Run the subagent command from the same working directory as the parent agent so it has access to the same project files.
-
Specific files: Mention specific files the subagent should look at rather than expecting it to discover them on its own.
Example prompt with skill and file references:
bash
claude --settings '{"sandbox":{"enabled":true,"autoAllowBashIfSandboxed":true}}' \
-p "Review the authentication logic in /home/user/project/src/auth.py \
for security vulnerabilities. Before starting, read the skill file at \
/home/user/.claude/skills/security-review/SKILL.md and follow its guidelines. \
Focus on input validation and session management."向子代理的提示词中添加相关上下文,使其能完成有效工作:
-
技能文件:通过在提示词中包含绝对路径,告知子代理读取技能文件。不要使用系统提示词标志——直接在提示词中添加指令。
-
工作目录:在与父Agent相同的工作目录中运行子代理命令,使其能访问相同的项目文件。
-
特定文件:明确提及子代理需要查看的特定文件,而非期望其自行发现这些文件。
包含技能和文件引用的提示词示例:
bash
claude --settings '{"sandbox":{"enabled":true,"autoAllowBashIfSandboxed":true}}' \
-p "Review the authentication logic in /home/user/project/src/auth.py \
for security vulnerabilities. Before starting, read the skill file at \
/home/user/.claude/skills/security-review/SKILL.md and follow its guidelines. \
Focus on input validation and session management."Background Execution
后台执行
Run the subagent in the background when your tooling supports it. This lets you run multiple subagents in parallel or continue other work while waiting.
-
Claude Code: Use theparameter on the Bash tool call. You will be notified when the command completes.
run_in_background -
Codex CLI / Gemini CLI: If your agent does not have a native background execution mechanism, redirect output to a temporary file and run in the background (e.g.,), then use
codex exec --full-auto "task" > /tmp/subagent-output.txt 2>&1 &and read the file when done. Alternatively, simply run in the foreground.wait
Parallel execution example: You can invoke two different agents simultaneously to get independent perspectives, then compare their outputs when both complete.
当你的工具支持时,在后台运行子代理。这让你可以并行运行多个子代理,或在等待子代理执行的同时继续处理其他工作。
-
Claude Code:在Bash工具调用中使用参数。命令完成时你会收到通知。
run_in_background -
Codex CLI / Gemini CLI:如果你的Agent没有原生后台执行机制,可将输出重定向到临时文件并在后台运行(例如:),然后使用
codex exec --full-auto "task" > /tmp/subagent-output.txt 2>&1 &命令等待完成并读取文件内容。或者,直接在前台运行命令。wait
并行执行示例:你可以同时调用两个不同的Agent以获取独立视角,待两者均完成后再比较它们的输出。
Reference
参考资料
For detailed flags, options, and additional examples for each CLI tool:
- See Claude Code reference for detailed flags.
- See Codex CLI reference for detailed flags.
- See Gemini CLI reference for detailed flags.
如需了解每个CLI工具的详细标志、选项和更多示例:
- 查看Claude Code参考文档获取详细标志说明。
- 查看Codex CLI参考文档获取详细标志说明。
- 查看Gemini CLI参考文档获取详细标志说明。