從寫程式碼,到創作下一幕

探索 字節跳動 - 火山方舟 的 AI 程式設計與影片創作活動。

Agent Plan & Coding Plan

一站體驗多款熱門模型,為 AI 程式設計與智能體開發提供更多選擇。新使用者可聯絡(微信: goo_lvyouyou)免費體驗 9.9 agent plan。

Seedance 2.5

讓創意,躍然成片。探索 30 秒影片、多模態參考與局部編輯,把腦海中的畫面變成下一支作品。

繁體中文

子智能體

在 ChatGPT 和 Codex 中使用子智能體,並設定自定義 Codex 智能體

ChatGPT Work 和 Codex 可以生成多個專用智能體並行執行任務, 然後將它們的結果彙總到一個響應中,從而執行子智能體工作流程。這對於 高度並行的複雜任務尤其有用,例如探索程式碼庫或實施多步驟功能計劃。

在本機 Codex 客戶端中,你還可以針對不同任務定義具有不同模型 設定和指令的自定義智能體。

可用性

當前 Codex 版本預設啟用子智能體工作流程。子智能體活動 會顯示在 ChatGPT 桌面應用、Codex CLI 和 IDE 擴充套件中。

由於每個子智能體都會獨立執行模型和工具相關工作,子智能體工作流程 比同類單智能體執行消耗更多 token。

在應用聊天中,請 Codex 將相互獨立的工作部分委派給 子智能體。當前本機 Codex 版本會在你直接提出要求,或適用的 AGENTS.md 或技能指令要求委派時執行委派。應用會顯示每個 子智能體執行緒,方便你檢查其工作以及傳回給主聊天的摘要。

子智能體工作流程為何有用

即使上下文視窗很大,模型仍有其限制。如果你讓主聊天(你在其中定義需求、約束和決策)充斥探索筆記、測試日誌、堆疊跟蹤和命令輸出等嘈雜的中間輸出,會話的可靠性可能會隨時間推移而下降。

這種現象通常稱為:

  • 上下文汙染:有用資訊被淹沒在嘈雜的中間輸出中。
  • 上下文腐化:隨著聊天中不太相關的細節不斷增多,效能逐漸下降。

有關背景資訊,請參閱 Chroma 關於上下文腐化的文章。

子智能體工作流程通過將嘈雜的工作移出主執行緒來提供幫助:

  • 讓主智能體 專注於需求、決策和最終輸出。
  • 並行執行專用子智能體,用於探索、測試或日誌分析。
  • 讓子智能體傳回摘要,而不是原始中間輸出。

當工作可以相互獨立地並行執行時,它們還能節省時間;通過將 規模更大的任務拆分成邊界明確的部分,也能使其更易處理。 例如,Codex 可以將對數百萬 token 文件的分析拆分成 多個較小的問題,並向主執行緒傳回提煉後的要點。

作為起點,可將並行智能體用於以讀取為主的任務,例如 探索、測試、問題分類和總結。對於以寫入為主的並行 工作流程則應更加謹慎,因為多個智能體同時編輯程式碼可能會產生 衝突並增加協調開銷。

核心術語

Codex 在子智能體工作流程中使用以下幾個相關術語:

  • 子智能體工作流程:Codex 執行多個並行智能體並合併其結果的工作流程。
  • 子智能體:Codex 啟動並委派其處理特定任務的智能體。
  • 智能體執行緒:子智能體執行工作的執行緒。受支援的客戶端允許你開啟這些執行緒以檢查進度或結果。

觸發子智能體工作流程

直接要求使用子智能體或並行智能體工作。當適用的專案或技能指令 要求委派時,Codex 也可以執行委派。

在實踐中,手動觸發是指使用直接指令,例如 “生成兩個智能體”、“並行委派這項工作”或“每個要點使用一個智能體”。 子智能體工作流程比同類單智能體執行消耗更多 token, 因為每個子智能體都會獨立執行模型和工具相關工作。

一條好的子智能體提示詞應說明如何劃分工作、Codex 是否應 等待所有智能體完成後再繼續,以及需要傳回什麼摘要或輸出。

Review this branch with parallel subagents. Spawn one subagent for security risks, one for test gaps, and one for maintainability. Wait for all three, then summarize the findings by category with file references.

選擇模型和推理級別

不同的智能體需要不同的模型和推理設定。

如果你沒有設定子智能體模型或 model_reasoning_effort, 子智能體會繼承父智能體的模型和推理強度。如果顯式 建立請求或 [agents] 預設設定選擇了模型,但沒有 顯式指定或設定推理強度,子智能體會使用該模型的預設 推理強度。要為每項任務平衡智慧水平、速度和價格, 可以在提示詞中請求使用特定模型或推理強度, 在 config.toml 中設定 [agents] 預設設定,或直接在自定義智能體檔案中設定 model 和 model_reasoning_effort。 例如,使用 gpt-6-luna 進行快速掃描,或使用推理強度更高的 gpt-6.1-sol 設定來處理要求更高的推理任務。

模型選擇

  • gpt-6.1-sol:對於需要處理高難度任務的智能體,請優先使用此模型。它適合需求不明確、包含多個步驟的工作,這類工作需要在較大的上下文中進行規劃、使用工具、驗證並持續跟進。
  • gpt-6-luna:適合需要快速處理明確、重複性或大批次工作的智能體,任務範圍應較窄。

推理強度(model_reasoning_effort)

對於 GPT-6.1 Sol,請使用你的客戶端和所選模型支援的 推理強度。顯式設定模型時,GPT-6 Luna 可先使用 high,GPT-6 Astra 可先使用 low。 再根據任務調整為所選模型支援的級別。

  • ultra:在所選模型支援時,用於最深入的 推理。
  • max 和 xhigh:在所選模型支援這些級別時,用於要求特別高的 推理任務。
  • high:用於智能體需要梳理複雜邏輯、檢查假設或處理邊界情況的任務(例如審查智能體或專注於安全的智能體)。
  • medium:平衡速度與深度。
  • low:用於任務簡單且速度最重要的情況。

更高的推理強度會增加響應時間和 token 用量,但可以提高複雜工作的質量。有關詳細資訊,請參閱模型、設定基礎和設定參考。

編排和執行緒控制

ChatGPT 或 Codex 負責跨智能體編排,包括生成新的 子智能體、轉發後續指令、等待結果以及關閉 智能體執行緒。

當多個智能體正在執行時,Codex 會等待所有請求的結果 就緒,然後傳回合併後的響應。

當前本機 Codex 版本會在收到直接請求或適用的 專案或技能指令後生成智能體。

要檢視實際效果,請在你的專案中嘗試以下提示詞:

I would like to review the following points on the current PR (this branch vs main). Spawn one agent per point, wait for all of them, and summarize the result for each point.
1. Security issue
2. Code quality
3. Bugs
4. Race
5. Test flakiness
6. Maintainability of the code

管理子智能體

  • 從主執行緒中顯示的活動開啟子智能體執行緒,以檢查 其工作。
  • 直接要求 Codex 引導正在執行的子智能體、停止它,或關閉已完成的 子智能體執行緒。

審批和沙箱控制

子智能體會繼承你當前的沙箱策略。

子智能體會繼承輸入框下方選擇的權限模式。在要求 Codex 委派工作之前, 請先為父輪次選擇權限模式。

在父會話中設定沙箱和審批控制。自定義智能體的 指令可以要求智能體避免編輯,但不會建立獨立的 唯讀沙箱。

自定義智能體

Codex 隨附以下內建智能體:

  • default:通用後備智能體。
  • worker:面向實施和修復、專注執行的智能體。
  • explorer:以讀取為主的程式碼庫探索智能體。

要定義自己的自定義智能體,請將獨立 TOML 檔案新增到 ~/.codex/agents/(個人智能體)或 .codex/agents/(專案範圍的 智能體)下。

每個檔案定義一個自定義智能體。自定義智能體可以更改模型設定和 指令,或停用受支援的功能。它們會繼承父會話的 權限、供應商和 MCP 伺服器;自定義智能體檔案無法覆蓋這些 設定。

每個獨立的自定義智能體檔案都必須定義:

  • name
  • description
  • developer_instructions

如果自定義智能體檔案設定了 model 或 model_reasoning_effort,則以 檔案中的值為準。在應用檔案之前,Codex 會依次從顯式指定的建立參數、 對應的 [agents] 預設值,以及 父會話的值中確定各項設定。如果顯式建立請求或 [agents] 預設值 選擇了模型,但兩者都未指定推理強度,Codex 會使用 該模型的預設推理強度。僅設定 model 的自定義智能體檔案 會保留此前確定的推理強度。如果所選模型不支援該推理強度, 或你希望使用其他推理強度,請在檔案中同時設定 model_reasoning_effort, 以指定所需的值。技能繼承自父會話,自定義智能體可以通過 skills.config 停用單個技能。

全域設定

全域子智能體設定仍位於設定中的 [agents] 下。

欄位 類型 必填 用途
agents.enabled boolean 否 啟用或停用多智能體工具。
agents.max_concurrent_threads_per_session number 否 限制並發開啟的已生成智能體執行緒數量,不包括主智能體。
agents.default_subagent_model string 否 設定已生成智能體的預設模型。
agents.default_subagent_reasoning_effort string 否 設定已生成智能體的預設推理強度。
agents.interrupt_message boolean 否 智能體輪次中斷時記錄一條模型可見訊息。

注意:

  • agents.enabled 預設為 true。將其設為 false 可停用多智能體工具。
  • 如果未設定 agents.max_concurrent_threads_per_session,Codex 會選擇預設值。現有設定可以繼續使用 agents.max_threads 作為舊版別名。
  • 顯式生成值會覆蓋 agents.default_subagent_model 和 agents.default_subagent_reasoning_effort。
  • agents.interrupt_message 預設為 true。將其設為 false 可從智能體上下文中省略模型可見的中斷訊息。
  • 如果自定義智能體名稱與 explorer 等內建智能體相同,則以自定義智能體為準。

自定義智能體檔案格式

欄位 類型 必填 用途
name string 是 Codex 在生成或引用該智能體時使用的智能體名稱。
description string 是 面向使用者的指南,說明 Codex 應在何時使用此智能體。
developer_instructions string 是 定義智能體行為的核心指令。

使用 model、model_reasoning_effort、model_reasoning_summary 和 model_verbosity 自定義模型行為。 有關這些設定的可用值,請參閱設定參考 中的說明。

要限制自定義智能體的功能,請將受支援的功能鍵設為 false: features.shell_tool、features.apps、features.plugins、 features.memories 或 features.request_permissions_tool。要停用單個 技能,請使用 skills.config;要停用內建技能,請使用 skills.bundled.enabled = false。設定 skills.include_instructions = false 可 省略自動新增的技能指令塊。受管理的要求仍然適用。

其他會話設定,包括 sandbox_mode、approval_policy、 model_provider 和 mcp_servers,即使寫入自定義智能體檔案, 也不會覆蓋父會話的設定。服務層級同樣遵循根 會話的偏好設定。請在父會話中設定 MCP 伺服器,以便智能體 使用它們。

Codex 通過自定義智能體的 name 欄位識別它。讓檔名與 智能體名稱保持一致是最簡單的約定,但 name 欄位才是最終 依據。

自定義智能體範例

最優秀的自定義智能體應當範圍明確且有鮮明傾向。為每個智能體分配清晰的職責, 提供與該職責匹配的工具範圍,並通過指令避免其 偏離到相鄰工作。

如果你的已登入帳戶或工作區擁有存取權限,這些範例會使用 GPT-6.1 Sol。 如果該模型不可用,請選擇你可以 使用的模型。

範例 1:PR 審查

此模式將審查工作拆分給三個各有側重的自定義智能體:

  • pr_explorer 梳理程式碼庫並收集證據。
  • reviewer 尋找正確性、安全性和測試風險。
  • docs_researcher 通過專用 MCP server檢查框架或 API 文件。

專案設定(.codex/config.toml):

[agents]
max_concurrent_threads_per_session = 8

[mcp_servers.openaiDeveloperDocs]
url = "https://developers.openai.com/mcp"

.codex/agents/pr-explorer.toml:

name = "pr_explorer"
description = "Read-only codebase explorer for gathering evidence before changes are proposed."
model = "gpt-6-luna"
model_reasoning_effort = "high"
developer_instructions = """
Stay in exploration mode.
Do not edit files.
Trace the real execution path, cite files and symbols, and avoid proposing fixes unless the parent agent asks for them.
Prefer fast search and targeted file reads over broad scans.
"""

.codex/agents/reviewer.toml:

name = "reviewer"
description = "PR reviewer focused on correctness, security, and missing tests."
model = "gpt-6.1-sol"
model_reasoning_effort = "medium"
developer_instructions = """
Review code like an owner.
Do not edit files.
Prioritize correctness, security, behavior regressions, and missing test coverage.
Lead with concrete findings, include reproduction steps when possible, and avoid style-only comments unless they hide a real bug.
"""

.codex/agents/docs-researcher.toml:

name = "docs_researcher"
description = "Documentation specialist that uses the docs MCP server to verify APIs and framework behavior."
model = "gpt-6-luna"
model_reasoning_effort = "high"
developer_instructions = """
Use the docs MCP server to confirm APIs, options, and version-specific behavior.
Return concise answers with links or exact references when available.
Do not make code changes.
"""

此設定非常適合以下提示詞:

Review this branch against main. Have pr_explorer map the affected code paths, reviewer find real risks, and docs_researcher verify the framework APIs that the patch relies on.

範例 2:前端整合除錯

此模式適用於 UI 迴歸、間歇性瀏覽器流程,或橫跨應用程式碼和執行中產品的整合錯誤。

專案設定(.codex/config.toml):

[agents]
max_concurrent_threads_per_session = 6

[mcp_servers.chrome_devtools]
url = "http://localhost:3000/mcp"
startup_timeout_sec = 20

.codex/agents/code-mapper.toml:

name = "code_mapper"
description = "Read-only codebase explorer for locating the relevant frontend and backend code paths."
model = "gpt-6-luna"
model_reasoning_effort = "high"
developer_instructions = """
Map the code that owns the failing UI flow.
Do not edit files.
Identify entry points, state transitions, and likely files before the worker starts editing.
"""

.codex/agents/browser-debugger.toml:

name = "browser_debugger"
description = "UI debugger that uses browser tooling to reproduce issues and capture evidence."
model = "gpt-6.1-sol"
model_reasoning_effort = "medium"
developer_instructions = """
Reproduce the issue in the browser, capture exact steps, and report what the UI actually does.
Use browser tooling for screenshots, console output, and network evidence.
Do not edit application code.
"""

.codex/agents/ui-fixer.toml:

name = "ui_fixer"
description = "Implementation-focused agent for small, targeted fixes after the issue is understood."
model = "gpt-6-luna"
model_reasoning_effort = "high"
developer_instructions = """
Own the fix once the issue is reproduced.
Make the smallest defensible change, keep unrelated files untouched, and validate only the behavior you changed.
"""

[[skills.config]]
path = "/Users/me/.agents/skills/docs-editor/SKILL.md"
enabled = false

此設定非常適合以下提示詞:

Investigate why the settings modal fails to save. Have browser_debugger reproduce it, code_mapper trace the responsible code path, and ui_fixer implement the smallest fix once the failure mode is clear.