Agent Development for Claude Code
Build effective custom agents for Claude Code with proper delegation, tool access, and prompt design.
Agent Description Pattern
The description field determines whether Claude will automatically delegate tasks.
Strong Trigger Pattern
CODEBLOCK0
Weak vs Strong Descriptions
| Weak (won't auto-delegate) | Strong (auto-delegates) |
|---|
| "Analyzes screenshots for issues" | "Visual QA specialist. MUST BE USED when analyzing screenshots. Use PROACTIVELY for visual QA." |
| "Runs Playwright scripts" |
"Playwright specialist. MUST BE USED when running Playwright scripts. Use PROACTIVELY for browser automation." |
Key phrases:
- - "MUST BE USED when..."
- "Use PROACTIVELY for..."
- Include trigger keywords
Delegation Mechanisms
- 1. Explicit:
Task tool subagent_type: "agent-name" - always works - Automatic: Claude matches task to agent description - requires strong phrasing
Session restart required after creating/modifying agents.
Tool Access Principle
If an agent doesn't need Bash, don't give it Bash.
| Agent needs to... | Give tools | Don't give |
|---|
| Create files only | Read, Write, Edit, Glob, Grep | Bash |
| Run scripts/CLIs |
Read, Write, Edit, Glob, Grep, Bash | — |
| Read/audit only | Read, Glob, Grep | Write, Edit, Bash |
Why? Models default to cat > file << 'EOF' heredocs instead of Write tool. Each bash command requires approval, causing dozens of prompts per agent run.
Allowlist Pattern
Instead of restricting Bash, allowlist safe commands in .claude/settings.json:
CODEBLOCK1
Model Selection (Quality First)
Don't downgrade quality to work around issues - fix root causes instead.
| Model | Use For |
|---|
| Opus | Creative work (page building, design, content) - quality matters |
| Sonnet |
Most agents - content, code, research (default) |
|
Haiku | Only script runners where quality doesn't matter |
Memory Limits
Root Cause Fix (REQUIRED)
Add to ~/.bashrc or ~/.zshrc:
CODEBLOCK2
Increases Node.js heap from 4GB to 16GB.
Parallel Limits (Even With Fix)
| Agent Type | Max Parallel | Notes |
|---|
| Any agents | 2-3 | Context accumulates; batch then pause |
| Heavy creative (Opus) |
1-2 | Uses more memory |
Recovery
- 1.
source ~/.bashrc or restart terminal - INLINECODE6
- Check what files exist, continue from there
Sub-Agent vs Remote API
Always prefer Task sub-agents over remote API calls.
| Aspect | Remote API Call | Task Sub-Agent |
|---|
| Tool access | None | Full (Read, Grep, Write, Bash) |
| File reading |
Must pass all content in prompt | Can read files iteratively |
| Cross-referencing | Single context window | Can reason across documents |
| Decision quality | Generic suggestions | Specific decisions with rationale |
| Output quality | ~100 lines typical | 600+ lines with specifics |
CODEBLOCK3
Declarative Over Imperative
Describe what to accomplish, not how to use tools.
Wrong (Imperative)
CODEBLOCK4 bash
grep -r "PLACEHOLDER:" build/*.html
CODEBLOCK5
Right (Declarative)
CODEBLOCK6
What to Include
| Include | Skip |
|---|
| Task goal and context | Explicit bash/tool commands |
| Input file paths |
"Use X tool to..." |
| Output file paths and format | Step-by-step tool invocations |
| Success/failure criteria | Shell pipeline syntax |
| Blocking checks (prerequisites) | Micromanaged workflows |
| Quality checklists | |
Self-Documentation Principle
"Agents that won't have your context must be able to reproduce the behaviour independently."
Every improvement must be encoded into the agent's prompt, not left as implicit knowledge.
What to Encode
| Discovery | Where to Capture |
|---|
| Bug fix pattern | Agent's "Corrections" or "Common Issues" section |
| Quality requirement |
Agent's "Quality Checklist" section |
| File path convention | Agent's "Output" section |
| Tool usage pattern | Agent's "Process" section |
| Blocking prerequisite | Agent's "Blocking Check" section |
Test: Would a Fresh Agent Succeed?
Before completing any agent improvement:
- 1. Read the agent prompt as if you have no context
- Ask: Could a new session follow this and produce the same quality?
- If no: Add missing instructions, patterns, or references
Anti-Patterns
| Anti-Pattern | Why It Fails |
|---|
| "As we discussed earlier..." | No prior context exists |
| Relying on files read during dev |
Agent may not read same files |
| Assuming knowledge from errors | Agent won't see your debugging |
| "Just like the home page" | Agent hasn't built home page |
Agent Prompt Structure
Effective agent prompts include:
CODEBLOCK7
Pipeline Agents
When inserting a new agent into a numbered pipeline (e.g., HTML-01 → HTML-05 → HTML-11):
| Must Update | What |
|---|
| New agent | "Workflow Position" diagram + "Next" field |
| Predecessor agent |
Its "Next" field to point to new agent |
Common bug: New agent is "orphaned" because predecessor still points to old next agent.
Verification:
CODEBLOCK8
The Sweet Spot
Best use case: Tasks that are repetitive but require judgment.
Example: Auditing 70 skills manually = tedious. But each audit needs intelligence (check docs, compare versions, decide what to fix). Perfect for parallel agents with clear instructions.
Not good for:
- - Simple tasks (just do them)
- Highly creative tasks (need human direction)
- Tasks requiring cross-file coordination (agents work independently)
Effective Prompt Template
CODEBLOCK9
Key elements:
- - "FIX issues found" - Without this, agents only report. With it, they take action.
- Exact file paths - Prevents ambiguity
- Output format template - Ensures consistent, parseable reports
- Batch size ~5 items - Enough work to be efficient, not so much that failures cascade
Workflow Pattern
CODEBLOCK10
Why agents don't commit: Allows human review, batching, and clean commit history.
Signs a Task Fits This Pattern
Good fit:
- - Same steps repeated for many items
- Each item requires judgment (not just transformation)
- Items are independent (no cross-item dependencies)
- Clear success criteria (score, pass/fail, etc.)
- Authoritative source exists to verify against
Bad fit:
- - Items depend on each other's results
- Requires creative/subjective decisions
- Single complex task (use regular agent instead)
- Needs human input mid-process
Quick Reference
Agent Frontmatter Template
CODEBLOCK11
Fix Bash Approval Spam
- 1. Remove Bash from tools if not needed
- Put critical instructions FIRST (right after frontmatter)
- Use allowlists in INLINECODE10
Memory Crash Recovery
CODEBLOCK12
Claude Code的Agent开发
通过适当的委派、工具访问和提示设计,为Claude Code构建有效的自定义Agent。
Agent描述模式
描述字段决定Claude是否会自动委派任务。
强触发模式
yaml
name: agent-name
description: |
[角色]专家。当[特定触发条件]时必须使用。
主动用于[任务类别]。
关键词:[触发词]
tools: Read, Write, Edit, Glob, Grep, Bash
model: sonnet
弱描述与强描述对比
| 弱描述(不会自动委派) | 强描述(自动委派) |
|---|
| 分析截图中的问题 | 视觉QA专家。分析截图时必须使用。主动用于视觉质量检查。 |
| 运行Playwright脚本 |
Playwright专家。运行Playwright脚本时必须使用。主动用于浏览器自动化。 |
关键短语:
- - 当...时必须使用
- 主动用于...
- 包含触发关键词
委派机制
- 1. 显式委派:Task tool subagent_type: agent-name - 始终有效
- 自动委派:Claude将任务与Agent描述匹配 - 需要强描述措辞
创建/修改Agent后需要重启会话。
工具访问原则
如果Agent不需要Bash,就不要给它Bash。
| Agent需要... | 给予的工具 | 不给予的 |
|---|
| 仅创建文件 | Read, Write, Edit, Glob, Grep | Bash |
| 运行脚本/CLI |
Read, Write, Edit, Glob, Grep, Bash | — |
| 仅读取/审计 | Read, Glob, Grep | Write, Edit, Bash |
为什么? 模型默认使用cat > file << EOF heredoc而不是Write工具。每个bash命令都需要批准,导致每次Agent运行产生数十个提示。
白名单模式
与其限制Bash,不如在.claude/settings.json中白名单安全命令:
json
{
permissions: {
allow: [
Write, Edit, WebFetch(domain:*),
Bash(cd ), Bash(cp ), Bash(mkdir ), Bash(ls ),
Bash(cat ), Bash(head ), Bash(tail ), Bash(grep ),
Bash(diff ), Bash(mv ), Bash(touch ), Bash(file )
]
}
}
模型选择(质量优先)
不要为了绕开问题而降低质量——应该修复根本原因。
| 模型 | 用途 |
|---|
| Opus | 创意工作(页面构建、设计、内容)- 质量至关重要 |
| Sonnet |
大多数Agent - 内容、代码、研究(默认) |
|
Haiku | 仅用于质量不重要的脚本运行器 |
内存限制
根本原因修复(必需)
添加到~/.bashrc或~/.zshrc:
bash
export NODE_OPTIONS=--max-old-space-size=16384
将Node.js堆从4GB增加到16GB。
并行限制(即使有修复)
| Agent类型 | 最大并行数 | 说明 |
|---|
| 任何Agent | 2-3 | 上下文会累积;分批处理然后暂停 |
| 重度创意(Opus) |
1-2 | 使用更多内存 |
恢复
- 1. source ~/.bashrc或重启终端
- NODE_OPTIONS=--max-old-space-size=16384 claude
- 检查存在哪些文件,从中断处继续
子Agent vs 远程API
始终优先使用Task子Agent而非远程API调用。
| 方面 | 远程API调用 | Task子Agent |
|---|
| 工具访问 | 无 | 完整(Read, Grep, Write, Bash) |
| 文件读取 |
必须在提示中传递所有内容 | 可以迭代读取文件 |
| 交叉引用 | 单一上下文窗口 | 可以跨文档推理 |
| 决策质量 | 通用建议 | 带有理由的具体决策 |
| 输出质量 | 通常约100行 | 600+行,包含具体内容 |
typescript
// ❌ 错误 - 远程API调用
const response = await fetch(https://api.anthropic.com/v1/messages, {...})
// ✅ 正确 - 使用Task工具
// 使用subagent_type: general-purpose调用Task
声明式优于命令式
描述要完成什么,而不是如何使用工具。
错误(命令式)
markdown
检查占位符
bash
grep -r PLACEHOLDER: build/*.html
正确(声明式)
markdown
检查占位符
搜索build/中的所有HTML文件,查找:
- - PLACEHOLDER: 注释
- TODO或TBD标记
- 模板括号如[Client Name]
任何匹配 = 内容不完整。
应包含的内容
| 包含 | 跳过 |
|---|
| 任务目标和上下文 | 明确的bash/工具命令 |
| 输入文件路径 |
使用X工具... |
| 输出文件路径和格式 | 逐步的工具调用 |
| 成功/失败标准 | Shell管道语法 |
| 阻塞检查(前置条件) | 微观管理工作流 |
| 质量检查清单 | |
自我文档化原则
没有你上下文的Agent必须能够独立复现行为。
每个改进都必须编码到Agent的提示中,而不是作为隐式知识留下。
需要编码的内容
| 发现 | 记录位置 |
|---|
| Bug修复模式 | Agent的修正或常见问题部分 |
| 质量要求 |
Agent的质量检查清单部分 |
| 文件路径约定 | Agent的输出部分 |
| 工具使用模式 | Agent的流程部分 |
| 阻塞前置条件 | Agent的阻塞检查部分 |
测试:新Agent能成功吗?
在完成任何Agent改进之前:
- 1. 假设没有上下文,阅读Agent提示
- 问:新会话能遵循这个并产生相同的质量吗?
- 如果不能:添加缺失的指令、模式或引用
反模式
| 反模式 | 为什么失败 |
|---|
| 正如我们之前讨论的... | 没有先前的上下文存在 |
| 依赖开发期间读取的文件 |
Agent可能不读取相同的文件 |
| 假设从错误中获取知识 | Agent看不到你的调试过程 |
| 就像主页一样 | Agent没有构建过主页 |
Agent提示结构
有效的Agent提示包括:
markdown
你的角色
[Agent做什么]
阻塞检查
[必须存在的前置条件]
输入
[要读取哪些文件]
流程
[逐步操作,包含编码的学习经验]
输出
[确切的文件路径和格式]
质量检查清单
[验证步骤,包括学到的陷阱]
常见问题
[开发过程中发现的模式]
流水线Agent
当将新Agent插入编号流水线时(例如,HTML-01 → HTML-05 → HTML-11):
| 必须更新 | 内容 |
|---|
| 新Agent | 工作流位置图 + 下一个字段 |
| 前驱Agent |
其下一个字段指向新Agent |
常见Bug:新Agent被孤立,因为前驱仍然指向旧的下一个Agent。
验证:
bash
grep -n Next:.→\|Then.runs next .claude/agents/*.md
最佳平衡点
最佳用例:重复但需要判断的任务。
示例:手动审计70个技能很繁琐。但每次审计都需要智能(检查文档、比较版本、决定修复什么)。非常适合具有清晰指令的并行Agent。
不适合:
- - 简单任务(直接做就行)
- 高度创意的任务(需要人类指导)
- 需要跨文件协调的任务(Agent独立工作)
有效提示模板
对于每个[项目]:
- 1. 读取[源文件]
- 使用[外部检查 - npm view, API调用等]验证
- 检查[权威来源]
- 评分/评估
- 修复发现的问题 ← 关键指令
关键要素:
- - 修复发现的问题 - 没有这个,Agent只报告。有了它,Agent会采取行动。
- 确切的文件路径 - 防止歧义
- 输出格式模板 - 确保一致、可解析的报告
- 批次大小约5个项目 - 足够高效工作,又不会让失败级联
工作流模式
- 1. 我:启动2-3个并行Agent,