creating-an-agent
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCreating an Agent
创建Agent
REQUIRED BACKGROUND: Read ed3d-extending-claude:writing-claude-directives for foundational guidance on token efficiency, compliance techniques, and directive structure. This skill focuses on agent-specific patterns.
必备背景知识: 阅读ed3d-extending-claude:writing-claude-directives文档,获取关于令牌效率、合规技巧和指令结构的基础指导。本技能聚焦于Agent特有的模式。
What is an Agent?
什么是Agent?
An agent is a specialized Claude instance with:
- Defined tools (Read, Edit, Bash, etc.)
- Specific responsibilities (code review, security audit, research)
- A focused system prompt
Agents are spawned via the Task tool or defined in plugin directories.
agents/Agent是具备以下特性的专用Claude实例:
- 已定义的工具(Read、Edit、Bash等)
- 特定职责(代码审查、安全审计、研究)
- 聚焦的系统提示词
Agent可通过Task工具生成,或在插件的目录中定义。
agents/When to Create an Agent
何时创建Agent
Create when:
- Task requires specialized expertise
- Workflow benefits from tool restrictions
- You want consistent behavior across invocations
- Task is complex enough to warrant context isolation
Don't create for:
- Simple, one-off tasks
- Tasks the main Claude handles well
- Purely conversational interactions
适合创建的场景:
- 任务需要专业领域知识
- 工作流程可从工具限制中获益
- 希望在多次调用中保持一致的行为
- 任务足够复杂,需要上下文隔离
无需创建的场景:
- 简单的一次性任务
- 主Claude能够很好处理的任务
- 纯对话式交互
Agent File Structure
Agent文件结构
agents/
my-agent.mdTemplate:
markdown
---
name: agent-name
description: Use when [specific triggers] - [what agent does]
tools: Read, Grep, Glob, Bash
model: sonnet
---agents/
my-agent.md模板:
markdown
---
name: agent-name
description: Use when [specific triggers] - [what agent does]
tools: Read, Grep, Glob, Bash
model: sonnet
---Agent Name
Agent Name
[Agent system prompt - who they are, what they do]
[Agent system prompt - who they are, what they do]
Responsibilities
Responsibilities
- Task 1
- Task 2
- Task 1
- Task 2
Workflow
Workflow
- Step 1
- Step 2
undefined- Step 1
- Step 2
undefinedDescription: The Critical Field
描述字段:关键所在
The field determines when Claude auto-delegates to your agent. It's searched when matching tasks to available agents.
descriptiondescriptionWriting Effective Descriptions
撰写有效的描述
Format: "Use when [specific triggers/symptoms] - [what the agent does]"
Write in third person. Injected into system prompt.
yaml
undefined格式: "Use when [特定触发条件/场景] - [Agent的功能]"
使用第三人称撰写,该内容会被注入到系统提示词中。
yaml
undefinedBad: vague, no triggers
反面示例:模糊,无触发条件
description: Helps with code
description: Helps with code
Bad: first person
反面示例:第一人称
description: I review code for security issues
description: I review code for security issues
Good: specific triggers + action
正面示例:特定触发条件+明确动作
description: Use when reviewing code for security vulnerabilities, analyzing authentication flows, or checking for common security anti-patterns like SQL injection, XSS, or insecure dependencies
**Include:**
- Specific symptoms that trigger use
- Domain keywords (security, performance, testing)
- File types or patterns if relevant
- Actions the agent performs
**Length:** Max 1024 characters. Be specific, not verbose.description: Use when reviewing code for security vulnerabilities, analyzing authentication flows, or checking for common security anti-patterns like SQL injection, XSS, or insecure dependencies
**需包含:**
- 触发使用的特定场景
- 领域关键词(安全、性能、测试)
- 相关的文件类型或模式(如有)
- Agent执行的动作
**长度限制:** 最多1024字符。要具体,避免冗长。Tool Selection
工具选择
Choose tools based on agent responsibilities:
| Tool | When to Include |
|---|---|
| Read | Reading files, analyzing code |
| Grep | Searching code patterns |
| Glob | Finding files by pattern |
| Edit | Modifying existing files |
| Write | Creating new files |
| Bash | Running commands, git, tests |
| TaskCreate/TaskUpdate | Tracking multi-step workflows (TodoWrite in older versions) |
| Task | Spawning sub-agents |
| WebFetch/WebSearch | Research tasks |
Principle: Include only what the agent needs. Fewer tools = more focused behavior.
Example restrictions:
- Code reviewer: (no write access)
Read, Grep, Glob - Implementor:
Read, Edit, Write, Bash, Grep, Glob - Researcher:
Read, WebFetch, WebSearch, Glob
根据Agent的职责选择工具:
| 工具 | 使用场景 |
|---|---|
| Read | 读取文件、分析代码 |
| Grep | 搜索代码模式 |
| Glob | 根据模式查找文件 |
| Edit | 修改现有文件 |
| Write | 创建新文件 |
| Bash | 运行命令、git操作、测试 |
| TaskCreate/TaskUpdate | 跟踪多步骤工作流(旧版本为TodoWrite) |
| Task | 生成子Agent |
| WebFetch/WebSearch | 研究类任务 |
原则: 仅包含Agent所需的工具。工具越少,行为越聚焦。
示例限制:
- 代码审查Agent:(无写入权限)
Read, Grep, Glob - 实现Agent:
Read, Edit, Write, Bash, Grep, Glob - 研究Agent:
Read, WebFetch, WebSearch, Glob
Agent Prompt Structure
Agent提示词结构
Role Definition
角色定义
Start with who the agent is:
markdown
You are a security expert specializing in web application security and secure coding practices.从明确Agent的身份开始:
markdown
You are a security expert specializing in web application security and secure coding practices.Responsibilities
职责说明
Explicit, numbered list:
markdown
undefined使用明确的编号列表:
markdown
undefinedYour Responsibilities
Your Responsibilities
- Identify security vulnerabilities
- Review authentication logic
- Check for insecure dependencies
- Report findings with severity ratings
undefined- Identify security vulnerabilities
- Review authentication logic
- Check for insecure dependencies
- Report findings with severity ratings
undefinedWorkflow
工作流程
Step-by-step process:
markdown
undefined分步说明流程:
markdown
undefinedWorkflow
Workflow
- Scan: Use Grep to find common vulnerability patterns
- Analyze: Use Read to examine flagged files
- Verify: Use Bash to run security audit tools
- Report: Provide structured findings
undefined- Scan: Use Grep to find common vulnerability patterns
- Analyze: Use Read to examine flagged files
- Verify: Use Bash to run security audit tools
- Report: Provide structured findings
undefinedOutput Format
输出格式
Define expected structure:
markdown
undefined定义预期的输出结构:
markdown
undefinedReporting Format
Reporting Format
For each finding:
- Severity: Critical/High/Medium/Low
- Location:
file:line - Issue: What's vulnerable
- Impact: What attacker could do
- Fix: How to remediate
undefinedFor each finding:
- Severity: Critical/High/Medium/Low
- Location:
file:line - Issue: What's vulnerable
- Impact: What attacker could do
- Fix: How to remediate
undefinedConstraints
约束条件
What the agent should NOT do:
markdown
undefined明确Agent不应做的事情:
markdown
undefinedConstraints
Constraints
- Report findings only; do not modify code
- Ask for clarification if scope is unclear
- Escalate to human for ambiguous security decisions
undefined- Report findings only; do not modify code
- Ask for clarification if scope is unclear
- Escalate to human for ambiguous security decisions
undefinedModel Selection
模型选择
| Model | Use For |
|---|---|
| haiku | Simple tasks, fast iteration, high volume |
| sonnet | Balanced capability/cost, most tasks |
| opus | Complex reasoning, critical decisions, code review |
Specify in frontmatter:
yaml
model: opus| 模型 | 使用场景 |
|---|---|
| haiku | 简单任务、快速迭代、高吞吐量 |
| sonnet | 能力与成本平衡,适用于大多数任务 |
| opus | 复杂推理、关键决策、代码审查 |
在前置元数据中指定:
yaml
model: opusTesting Agents
测试Agent
1. Baseline Test
1. 基准测试
Run the task WITHOUT the agent. Document:
- What went wrong
- What was missing
- How long it took
不使用Agent执行任务。记录:
- 出现的问题
- 缺失的内容
- 耗时
2. Agent Test
2. Agent测试
Run with agent. Verify:
- Agent is auto-delegated (description triggers correctly)
- Workflow is followed
- Output matches expected format
- Tool restrictions are respected
使用Agent执行任务。验证:
- Agent是否被自动委托(描述触发条件是否正确)
- 是否遵循工作流程
- 输出是否符合预期格式
- 是否遵守工具限制
3. Edge Case Testing
3. 边缘案例测试
Test with:
- Ambiguous inputs
- Missing context
- Large/complex inputs
- Tasks outside scope (should refuse gracefully)
测试以下场景:
- 模糊输入
- 缺失上下文
- 大型/复杂输入
- 超出范围的任务(应优雅拒绝)
4. Iteration
4. 迭代优化
If agent fails:
- Identify root cause (description? workflow? constraints?)
- Update agent definition
- Re-test
如果Agent测试失败:
- 确定根本原因(描述?工作流程?约束条件?)
- 更新Agent定义
- 重新测试
Common Patterns
常见模式
Code Reviewer
代码审查Agent
markdown
---
name: code-reviewer
description: Use when reviewing code changes, pull requests, or verifying implementation quality - analyzes for bugs, style issues, and best practices
tools: Read, Grep, Glob, Bash
model: opus
---markdown
---
name: code-reviewer
description: Use when reviewing code changes, pull requests, or verifying implementation quality - analyzes for bugs, style issues, and best practices
tools: Read, Grep, Glob, Bash
model: opus
---Code Reviewer
Code Reviewer
You are a senior engineer reviewing code for correctness, readability, and maintainability.
You are a senior engineer reviewing code for correctness, readability, and maintainability.
Responsibilities
Responsibilities
- Identify bugs and edge cases
- Check error handling
- Verify naming and style consistency
- Suggest improvements
- Identify bugs and edge cases
- Check error handling
- Verify naming and style consistency
- Suggest improvements
Workflow
Workflow
- Read the changed files
- Analyze for issues
- Provide structured feedback
- Read the changed files
- Analyze for issues
- Provide structured feedback
Output Format
Output Format
For each issue:
- File:Line: location
- Severity: Critical/Major/Minor
- Issue: description
- Suggestion: how to fix
undefinedFor each issue:
- File:Line: location
- Severity: Critical/Major/Minor
- Issue: description
- Suggestion: how to fix
undefinedResearch Agent
研究Agent
markdown
---
name: researcher
description: Use when gathering information from the web, investigating APIs, or synthesizing documentation from multiple sources
tools: Read, WebFetch, WebSearch, Glob
model: sonnet
---markdown
---
name: researcher
description: Use when gathering information from the web, investigating APIs, or synthesizing documentation from multiple sources
tools: Read, WebFetch, WebSearch, Glob
model: sonnet
---Research Agent
Research Agent
You are a research specialist gathering and synthesizing information.
You are a research specialist gathering and synthesizing information.
Responsibilities
Responsibilities
- Search for relevant sources
- Extract key information
- Synthesize findings
- Cite sources
- Search for relevant sources
- Extract key information
- Synthesize findings
- Cite sources
Workflow
Workflow
- WebSearch for relevant sources
- WebFetch promising results
- Extract and organize findings
- Return structured synthesis with citations
undefined- WebSearch for relevant sources
- WebFetch promising results
- Extract and organize findings
- Return structured synthesis with citations
undefinedImplementor Agent
任务实现Agent
markdown
---
name: task-implementor
description: Use when implementing specific tasks from plans - writes code, runs tests, commits changes following TDD workflow
tools: Read, Edit, Write, Bash, Grep, Glob, TaskCreate, TaskUpdate, TaskList
model: sonnet
---markdown
---
name: task-implementor
description: Use when implementing specific tasks from plans - writes code, runs tests, commits changes following TDD workflow
tools: Read, Edit, Write, Bash, Grep, Glob, TaskCreate, TaskUpdate, TaskList
model: sonnet
---Task Implementor
Task Implementor
You implement tasks following TDD principles.
You implement tasks following TDD principles.
Responsibilities
Responsibilities
- Write failing test first
- Implement minimal code to pass
- Refactor if needed
- Commit with descriptive message
- Write failing test first
- Implement minimal code to pass
- Refactor if needed
- Commit with descriptive message
Constraints
Constraints
- Never write implementation before test
- Run tests after each change
- Commit atomic, working changes only
undefined- Never write implementation before test
- Run tests after each change
- Commit atomic, working changes only
undefinedCommon Mistakes
常见错误
| Mistake | Fix |
|---|---|
| Vague description | Include specific triggers and symptoms |
| Too many tools | Restrict to what's needed |
| No workflow | Add step-by-step process |
| No output format | Define expected structure |
| First-person description | Write in third person |
| Overly broad scope | Narrow to specific responsibility |
| No testing | Test auto-delegation and output quality |
| 错误 | 修复方案 |
|---|---|
| 描述模糊 | 包含特定触发条件和场景 |
| 工具过多 | 仅保留必要工具 |
| 无工作流程 | 添加分步流程 |
| 无输出格式定义 | 明确预期结构 |
| 使用第一人称描述 | 改用第三人称撰写 |
| 范围过于宽泛 | 缩小至特定职责 |
| 未进行测试 | 测试自动委托功能和输出质量 |
Checklist
检查清单
- Description starts with "Use when...", third person
- Description includes specific triggers/symptoms
- Tools restricted to necessary set
- Model appropriate for task complexity
- Responsibilities clearly listed
- Workflow is step-by-step
- Output format defined
- Constraints/limitations stated
- Tested for auto-delegation
- Tested for output quality
- 描述以"Use when..."开头,使用第三人称
- 描述包含特定触发条件/场景
- 工具限制为必要集合
- 模型与任务复杂度匹配
- 职责明确列出
- 工作流程为分步式
- 定义了输出格式
- 说明了约束条件/限制
- 测试了自动委托功能
- 测试了输出质量