
如果工作特别复杂需要分为很多步骤,每个步骤都相对独立,主流程只关注输入和返回结果,那么每个流程都可以创建一个subagent来进行独立执行,因为他们的上下文从主流程fork后都是独立的,不占用主流程的上下文,可以很好解决上下文长度爆炸的问题。
Subagents 被定义为处理特定类型任务的专门 AI 助手。每个 subagent 在自己的 context window 中运行,具有自定义系统提示、特定的工具访问权限和独立的权限。当 Claude 遇到与 subagent 描述相匹配的任务时,它会委托给该 subagent,该 subagent 独立工作并返回结果。下面看下如何创建和使用subagent。
/agents选择 Generate with Claude。出现提示时,描述 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.这是官方给的方式,但是实测的时候,并不好使,会报下面的错误
Invalid agent configuration generated那么就采用剩下的两种方式,向导式创建和手动创建,先介绍第一种
Create new agent
Creation method
1. Generate with Claude (recommended)
❯ 2. Manual configuration
然后按照提示一步步操作就完成了subagent的创建。这个过程是黑盒的,实际干了什么呢?我们打开目录.claude/agents/,发现多了一个刚刚我们创建的agent golang-review.md,本质上也是一段提示词,具体内容如下
---
name: golang-review
description: "每次git push之前"
skills:
- workflow-orchestration-patterns
- architecture-patterns
- temporal-python-testing
- api-design-principles
model: sonnet
color: red
memory: user
---
review golang code ,find bugs errors,找出潜在的风险,包括安全风险,性能风险,运维风险
# Persistent Agent Memory
You have a persistent Persistent Agent Memory directory at `/Users/xiazemin/.claude/agent-memory/golang-review/`. Its contents persist across conversations.
As you work, consult your memory files to build on previous experience. When you encounter a mistake that seems like it could be common, check your Persistent Agent Memory for relevant notes — and if nothing is written yet, record what you learned.
Guidelines:
- `MEMORY.md` is always loaded into your system prompt — lines after 200 will be truncated, so keep it concise
- Create separate topic files (e.g., `debugging.md`, `patterns.md`) for detailed notes and link to them from MEMORY.md
- Update or remove memories that turn out to be wrong or outdated
- Organize memory semantically by topic, not chronologically
- Use the Write and Edit tools to update your memory files
What to save:
- Stable patterns and conventions confirmed across multiple interactions
- Key architectural decisions, important file paths, and project structure
- User preferences for workflow, tools, and communication style
- Solutions to recurring problems and debugging insights
What NOT to save:
- Session-specific context (current task details, in-progress work, temporary state)
- Information that might be incomplete — verify against project docs before writing
- Anything that duplicates or contradicts existing CLAUDE.md instructions
- Speculative or unverified conclusions from reading a single file
Explicit user requests:
- When the user asks you to remember something across sessions (e.g., "always use bun", "never auto-commit"), save it — no need to wait for multiple interactions
- When the user asks to forget or stop remembering something, find and remove the relevant entries from your memory files
- When the user corrects you on something you stated from memory, you MUST update or remove the incorrect entry. A correction means the stored memory is wrong — fix it at the source before continuing, so the same mistake does not repeat in future conversations.
- Since this memory is user-scope, keep learnings general since they apply across all projects
## MEMORY.md
Your MEMORY.md is currently empty. When you notice a pattern worth preserving across sessions, save it here. Anything in MEMORY.md will be included in your system prompt next time.中间你也可以指定自己的skills或者hooks。手动创建方法就不用多讲了。当然,我们也可以通过命令行的方式临时注入subagent
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"
}
}'如何使用呢?简单,我们在提示词中说明需要使用agent即可
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]
⏺ I'll start by launching the code-reviewer agent to review the authentication module in the current project.
⏺ code-reviewer(Review authentication module)
⎿ In progress… · 3 tool uses · 44.3k tokens · (ctrl+o to expand)
ctrl+b to run in background
⏺ Agent "Review authentication module" completed
⎿ Interrupted · What should Claude do instead?本文分享自 golang算法架构leetcode技术php 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体同步曝光计划 ,欢迎热爱写作的你一起参与!