通俗版 Claude Code 文档

结构照原 docs,内容改成真能照着做的人话版。

查看原始文档 目录顺序与官方保持一致

Agents

创建自定义子代理

创建自定义子代理 这一页讲的,就是 创建自定义子代理 这件事在 Claude Code 里到底怎么用。

页面信息

对应原页

创建自定义子代理

页面性质

第三方中文解释页

使用建议

先看人话解释,再对照原页命令和代码

这页不是官方原文,而是顺着官方文档结构做的中文解释版。命令、参数、配置名这些硬东西尽量保留,解释部分则尽量讲成人能照着做的话。

如果你碰到特别敏感的配置、权限或企业环境差异,最好顺手点上面的“查看原始文档”再核一遍。

这一页先讲明白

子代理就是把一个大帮工拆成几个专门干单项活的小帮工。

每个子代理有自己的职责、口气和工具权限,遇到合适的活就能自动接手。

这东西像村里分工:一个人专门查毛病,一个人专门写测试,一个人专门改文档。别让所有活都挤到同一个脑袋里。

好处是上下文更干净,干活边界更清楚,不容易今天修 bug 修着修着又跑去改文案。

先从最容易分工的活开始,比如 `reviewer`、`test-writer`、`docs-helper`。

给每个子代理写清楚“负责什么、不负责什么、能用哪些工具”,不然它们容易抢活。

常见分工

先从固定套路开始,最容易见效。

bug-hunter
reviewer
test-writer
docs-helper

预留广告位

正文中段响应式广告 等你后面真接 AdSense,这里再放正式广告。

Documentation Index

这里不是让你背"Documentation Index"这个词,而是让你看它真干活时怎么使。

这里还牵扯作用域,意思就是这条规则到底管当前项目、你个人,还是只管这一趟会话。

Built-in subagents

这一节先把系统自带的子代理认清楚:它们各自擅长什么活、会继承什么权限、什么时候会被 Claude 自动叫出来。

看这段时别只盯名字,重点是分清哪类代理偏查资料、哪类能动手改、哪类只适合在计划阶段帮你摸底。

Quickstart: create your first subagent

这一节是手把手做第一个子代理,核心不是炫配置,而是先把入口、存放位置、权限和模型这几步走通。

最稳的顺序就是先进 `/agents`,先做一个最简单、边界很清楚的子代理,跑通以后再往里加复杂能力。

直接对 Claude 说

Quickstart: create your first subagent 1

到这里其实就剩一句话,把下面这句发出去就行。

/agents
关键片段

Quickstart: create your first subagent 2

"Quickstart: create your first subagent"这一段里最要紧的原始写法在下面,先看它怎么落地。

A code improvement agent that scans files and suggests improvements
for readability, performance, and best practices. It should explain
each issue, show the current code, and provide an improved version.
直接对 Claude 说

Quickstart: create your first subagent 3

到这里其实就剩一句话,把下面这句发出去就行。

Use the code-improver agent to suggest improvements in this project

Configure subagents

这里讲的是子代理那些开关该怎么拧。你主要看它放哪儿、认不认得出来、权限有没有给对。

别一下子把所有花样都加上,先让最小那版跑通。

Use the /agents command

这一节讲 `/agents` 这个总入口。以后你想看现有代理、建新代理、改权限、删旧代理,基本都从这里进。

如果不是为了批量自动化,优先用这个交互入口,比你自己手搓文件稳得多。

Choose the subagent scope

这一节讲子代理到底放哪儿。项目级放仓库里,适合团队共用;个人级放 `~/.claude/agents/`,适合你自己到处带着走。

如果只是临时试一下,也可以用 `--agents` 这种会话级方式;但临时的东西不会长期留着,别把正式规则全压在这上面。

终端里敲

Choose the subagent scope 1

看到这里,别光点头,下面这条命令先跑起来再说。

claude --agents '{
  "code-reviewer": {
    "description": "Expert code reviewer. Use proactively after code changes.",
    "prompt": "You are a senior code reviewer. Focus on code quality, security, and best practices.",
    "tools": ["Read", "Grep", "Glob", "Bash"],
    "model": "sonnet"
  },
  "debugger": {
    "description": "Debugging specialist for errors and test failures.",
    "prompt": "You are an expert debugger. Analyze errors, identify root causes, and provide fixes."
  }
}'
终端里敲

Choose the subagent scope 2

看到这里,别光点头,下面这条命令先跑起来再说。

claude --agents @'
{
  "code-reviewer": {
    "description": "Expert code reviewer. Use proactively after code changes.",
    "prompt": "You are a senior code reviewer. Focus on code quality, security, and best practices.",
    "tools": ["Read", "Grep", "Glob", "Bash"],
    "model": "sonnet"
  },
  "debugger": {
    "description": "Debugging specialist for errors and test failures.",
    "prompt": "You are an expert debugger. Analyze errors, identify root causes, and provide fixes."
  }
}
'@

Write subagent files

这一节讲子代理文件怎么写,重点是 frontmatter 放什么、正文提示词怎么写、改完以后怎么让 Claude 重新认到它。

别把子代理文件写成散文,名字、描述、工具、模型和正文职责要分得清清楚楚。

改配置

Write subagent files

这一段说完,最后还得写到配置里才算真的生效。

---
name: code-reviewer
description: Reviews code for quality and best practices
tools: Read, Glob, Grep
model: sonnet
---

You are a code reviewer. When invoked, analyze the code and provide
specific, actionable feedback on quality, security, and best practices.

Choose a model

这一节讲子代理用什么模型。说白了,就是这类活到底该让快一点的工种来,还是让更能深想的工种来。

如果这类子代理主要是查资料、扫目录、跑轻活,通常不用一上来就上最重的模型。

Control subagent capabilities

这一节讲怎么收放子代理的手脚,比如它能用哪些工具、能不能再叫别的代理、能不能挂 MCP、skills、memory 这些外接能力。

这里的核心原则就是只给它完成这类活必须的能力,不要为了省事一口气全放开。

改配置

Control subagent capabilities 1

这会儿轮到改配置了,字段名和关键字别自己乱换。

---
name: safe-researcher
description: Research agent with restricted capabilities
tools: Read, Grep, Glob, Bash
---
改配置

Control subagent capabilities 2

这会儿轮到改配置了,字段名和关键字别自己乱换。

---
name: no-writes
description: Inherits every tool except file writes
disallowedTools: Write, Edit
---
改配置

Control subagent capabilities 3

这会儿轮到改配置了,字段名和关键字别自己乱换。

---
name: coordinator
description: Coordinates work across specialized agents
tools: Agent(worker, researcher), Read, Bash
---
改配置

Control subagent capabilities 4

这会儿轮到改配置了,字段名和关键字别自己乱换。

---
name: browser-tester
description: Tests features in a real browser using Playwright
mcpServers:
  # Inline definition: scoped to this subagent only
  - playwright:
      type: stdio
      command: npx
      args: ["-y", "@playwright/mcp@latest"]
  # Reference by name: reuses an already-configured server
  - github
---

Use the Playwright tools to navigate, screenshot, and interact with pages.

Define hooks for subagents

这一节讲怎么给子代理挂 hooks,也就是让它在某些动作前后自动做检查、校验或收尾。

如果你想让子代理改文件后自动跑检查,或者在执行危险命令前先过一遍门禁,这一节就是干这个的。

改配置

Define hooks for subagents 1

光知道意思还不够,这里得把规矩落进配置里,下面这块照着填。

---
name: code-reviewer
description: Review code changes with automatic linting
hooks:
  PreToolUse:
    - matcher: "Bash"
      hooks:
        - type: command
          command: "./scripts/validate-command.sh $TOOL_INPUT"
  PostToolUse:
    - matcher: "Edit|Write"
      hooks:
        - type: command
          command: "./scripts/run-linter.sh"
---
改配置

Define hooks for subagents 2

光知道意思还不够,这里得把规矩落进配置里,下面这块照着填。

{
  "hooks": {
    "SubagentStart": [
      {
        "matcher": "db-agent",
        "hooks": [
          { "type": "command", "command": "./scripts/setup-db-connection.sh" }
        ]
      }
    ],
    "SubagentStop": [
      {
        "hooks": [
          { "type": "command", "command": "./scripts/cleanup-db-connection.sh" }
        ]
      }
    ]
  }
}

Work with subagents

这里是在讲平时怎么把子代理真正用起来,不是光把文件写完就算。

要点就两件事:什么时候该叫它上,叫上来以后它负责哪一块。

Understand automatic delegation

这一节讲 Claude 什么时候会主动把活派给某个子代理。关键不只在你的提问,还在子代理描述写得够不够清楚。

如果你希望某类子代理"该出手时就出手",描述里就得把触发场景写明白,别只写一串空话。

Invoke subagents explicitly

这里讲的是你手动点名叫某个子代理干活。这样做最适合你心里已经清楚"这活就该它来"。

手动点名的好处是稳,不会让 Claude 自己猜错工种。

关键片段

Invoke subagents explicitly 1

先看下面这块原始片段,等会儿再回头看解释会顺得多。

Use the test-runner subagent to fix failing tests
Have the code-reviewer subagent look at my recent changes
关键片段

Invoke subagents explicitly 2

先看下面这块原始片段,等会儿再回头看解释会顺得多。

@"code-reviewer (agent)" look at the auth changes
终端里敲

Invoke subagents explicitly 3

这一段不是只让你理解意思,下面这条命令就是现在要跑的。

claude --agent code-reviewer
改配置

Invoke subagents explicitly 4

这一段说完,最后还得写到配置里才算真的生效。

{
  "agent": "code-reviewer"
}

Run subagents in foreground or background

这一段主要是在把"Run subagents in foreground or background"讲实,不是只摆个标题给你看。

这里还牵扯作用域,意思就是这条规则到底管当前项目、你个人,还是只管这一趟会话。

Common patterns

看到这里,就当是前人踩完坑留下来的顺手做法。

直接对 Claude 说

Common patterns 1

这里不用敲命令,直接把下面这句话发给 Claude 就行,让它按"Common patterns"这一段的意思去办。

Use a subagent to run the test suite and report only the failing tests with their error messages
直接对 Claude 说

Common patterns 2

这里不用敲命令,直接把下面这句话发给 Claude 就行,让它按"Common patterns"这一段的意思去办。

Research the authentication, database, and API modules in parallel using separate subagents
直接对 Claude 说

Common patterns 3

这里不用敲命令,直接把下面这句话发给 Claude 就行,让它按"Common patterns"这一段的意思去办。

Use the code-reviewer subagent to find performance issues, then use the optimizer subagent to fix them

Choose between subagents and main conversation

这里是在帮你做选择题,不是只告诉你"有这个选项"。先看取舍,再决定怎么选。

看这段时要特别盯工具和权限边界,别为了省事一把全开。

Manage subagent context

这段主要是在说平时怎么管,不是光教你怎么开。

这里还牵扯作用域,意思就是这条规则到底管当前项目、你个人,还是只管这一趟会话。

关键片段

Manage subagent context 1

先看下面这块原始片段,等会儿再回头看解释会顺得多。

Use the code-reviewer subagent to review the authentication module
[Agent completes]

Continue that code review and now analyze the authorization logic
[Claude resumes the subagent with full context from previous conversation]
改配置

Manage subagent context 2

这一段说完,最后还得写到配置里才算真的生效。

{
  "type": "system",
  "subtype": "compact_boundary",
  "compactMetadata": {
    "trigger": "auto",
    "preTokens": 167189
  }
}

Fork the current conversation

这一块主要是在说"Fork the current conversation"真到手上该怎么用,哪里最容易踩坑。

这里还牵扯作用域,意思就是这条规则到底管当前项目、你个人,还是只管这一趟会话。

直接对 Claude 说

Fork the current conversation

到这里其实就剩一句话,把下面这句发出去就行。

/fork draft unit tests for the parser changes so far

Observe and steer running forks

这里不是让你背"Observe and steer running forks"这个词,而是让你看它真干活时怎么使。

这里还牵扯作用域,意思就是这条规则到底管当前项目、你个人,还是只管这一趟会话。

How forks differ from named subagents

这一段主要是在把"How forks differ from named subagents"讲实,不是只摆个标题给你看。

这里还牵扯作用域,意思就是这条规则到底管当前项目、你个人,还是只管这一趟会话。

Limitations

这一块主要是在说"Limitations"真到手上该怎么用,哪里最容易踩坑。

这里还牵扯作用域,意思就是这条规则到底管当前项目、你个人,还是只管这一趟会话。

Example subagents

这里是样板段。重点不是全文照抄,而是学它怎么把职责、步骤和边界写清楚。

这里还牵扯作用域,意思就是这条规则到底管当前项目、你个人,还是只管这一趟会话。

Code reviewer

这一段主要是在把"Code reviewer"讲实,不是只摆个标题给你看。

看这段时要特别盯工具和权限边界,别为了省事一把全开。

改配置

Code reviewer

这会儿轮到改配置了,字段名和关键字别自己乱换。

---
name: code-reviewer
description: Expert code review specialist. Proactively reviews code for quality, security, and maintainability. Use immediately after writing or modifying code.
tools: Read, Grep, Glob, Bash
model: inherit
---

You are a senior code reviewer ensuring high standards of code quality and security.

When invoked:
1. Run git diff to see recent changes
2. Focus on modified files
3. Begin review immediately

Review checklist:
- Code is clear and readable
- Functions and variables are well-named
- No duplicated code
- Proper error handling
- No exposed secrets or API keys
- Input validation implemented
- Good test coverage
- Performance considerations addressed

Provide feedback organized by priority:
- Critical issues (must fix)
- Warnings (should fix)
- Suggestions (consider improving)

Include specific examples of how to fix issues.

Debugger

这段就是排错时拿来照着查的,先一项项看清楚,再动手改。

看这段时要特别盯工具和权限边界,别为了省事一把全开。

改配置

Debugger

想把这条规矩固定住,就把下面这块老老实实写进去。

---
name: debugger
description: Debugging specialist for errors, test failures, and unexpected behavior. Use proactively when encountering any issues.
tools: Read, Edit, Bash, Grep, Glob
---

You are an expert debugger specializing in root cause analysis.

When invoked:
1. Capture error message and stack trace
2. Identify reproduction steps
3. Isolate the failure location
4. Implement minimal fix
5. Verify solution works

Debugging process:
- Analyze error messages and logs
- Check recent code changes
- Form and test hypotheses
- Add strategic debug logging
- Inspect variable states

For each issue, provide:
- Root cause explanation
- Evidence supporting the diagnosis
- Specific code fix
- Testing approach
- Prevention recommendations

Focus on fixing the underlying issue, not the symptoms.

Data scientist

这一段主要是在把"Data scientist"讲实,不是只摆个标题给你看。

改配置

Data scientist

这会儿轮到改配置了,字段名和关键字别自己乱换。

---
name: data-scientist
description: Data analysis expert for SQL queries, BigQuery operations, and data insights. Use proactively for data analysis tasks and queries.
tools: Bash, Read, Write
model: sonnet
---

You are a data scientist specializing in SQL and BigQuery analysis.

When invoked:
1. Understand the data analysis requirement
2. Write efficient SQL queries
3. Use BigQuery command line tools (bq) when appropriate
4. Analyze and summarize results
5. Present findings clearly

Key practices:
- Write optimized SQL queries with proper filters
- Use appropriate aggregations and joins
- Include comments explaining complex logic
- Format results for readability
- Provide data-driven recommendations

For each analysis:
- Explain the query approach
- Document any assumptions
- Highlight key findings
- Suggest next steps based on data

Always ensure queries are efficient and cost-effective.

Database query validator

这一块主要是在说"Database query validator"真到手上该怎么用,哪里最容易踩坑。

这里还牵扯作用域,意思就是这条规则到底管当前项目、你个人,还是只管这一趟会话。

改配置

Database query validator 1

想把这条规矩固定住,就把下面这块老老实实写进去。

---
name: db-reader
description: Execute read-only database queries. Use when analyzing data or generating reports.
tools: Bash
hooks:
  PreToolUse:
    - matcher: "Bash"
      hooks:
        - type: command
          command: "./scripts/validate-readonly-query.sh"
---

You are a database analyst with read-only access. Execute SELECT queries to answer questions about the data.

When asked to analyze data:
1. Identify which tables contain the relevant data
2. Write efficient SELECT queries with appropriate filters
3. Present results clearly with context

You cannot modify data. If asked to INSERT, UPDATE, DELETE, or modify schema, explain that you only have read access.
终端里敲

Database query validator 2

看到这里,别光点头,下面这条命令先跑起来再说。

#!/bin/bash
# Blocks SQL write operations, allows SELECT queries

# Read JSON input from stdin
INPUT=$(cat)

# Extract the command field from tool_input using jq
COMMAND=$(echo "$INPUT" | jq -r '.tool_input.command // empty')

if [ -z "$COMMAND" ]; then
  exit 0
fi

# Block write operations (case-insensitive)
if echo "$COMMAND" | grep -iE '\b(INSERT|UPDATE|DELETE|DROP|CREATE|ALTER|TRUNCATE|REPLACE|MERGE)\b' > /dev/null; then
  echo "Blocked: Write operations not allowed. Use SELECT queries only." >&2
  exit 2
fi

exit 0
关键片段

Database query validator 3

"Database query validator"这一段里最要紧的原始写法在下面,先看它怎么落地。

chmod +x ./scripts/validate-readonly-query.sh

Next steps

这里不是让你背"Next steps"这个词,而是让你看它真干活时怎么使。

这里还牵扯作用域,意思就是这条规则到底管当前项目、你个人,还是只管这一趟会话。

照着做一遍

如果你不想来回翻,就先照这几步顺着做。

每做完一步就看一下结果,再决定要不要继续往下。

第 1 步:常见分工

先从固定套路开始,最容易见效。

bug-hunter
reviewer
test-writer
docs-helper

一眼看懂这一页

这页的作用,就是把原本偏专业的话题,拆成能直接照着走的明白话。

创建自定义子代理
   |
   v
创建自定义子代理 这一页讲的,就是 创建自定义子代理 这件事在 Claude Code 里到底怎么用。
   |
   v
照着步骤去做

文末提醒

这站会按官方 docs 的导航和内容变化继续重生成,原站加页、删页、改页时,这里会跟着更新。

人话解释会尽量顺着原页往下讲,但命令、参数名、配置名这些硬东西还是保留原样,免得你抄过去跑不起来。

顺手再看

Sub-agents、Plugins、MCP 怎么选

先分清 sub-agents 是拿来分工,不是拿来堆花活。

小团队落地手册

想在团队里用多人协作前,先看这页。