save

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Save Session as Agent

将会话保存为Agent

Generate a reusable agent file from the current conversation and save it to
.claude/agents/
.
从当前会话生成一个可复用的Agent文件,并保存到
.claude/agents/
目录下。

Instructions

操作说明

Step 1: Generate the agent file

步骤1:生成Agent文件

Analyze the entire conversation — the original task, every user correction, every tool call, and the final output — then distill it into a reusable agent file. The agent file is NOT a session log. It is a system prompt that a subagent will receive with no prior context.
Key priorities:
  1. User corrections are the most important signal. Every correction implies a rule the agent got wrong initially. Each correction MUST become an explicit rule.
  2. Only capture what worked. If approach A failed and approach B worked — only document approach B.
  3. Generalize — replace session-specific values (file names, URLs, credentials) with descriptive placeholders. The agent must work for similar tasks, not just this exact one.
  4. Keep it concise — this is a system prompt for a subagent. Shorter is better.
Output the agent file with YAML frontmatter followed by a system prompt body:
---
name: "<kebab-case-name>"
description: "<one-liner, max 200 chars>"
tools: Read, Glob, Grep, Bash, Write, Edit
model: sonnet
---

You are an agent that <role description — what this agent does>.
分析整个会话内容——包括初始任务、每一处用户修正、每一次工具调用以及最终输出——然后将其提炼为一个可复用的Agent文件。该Agent文件并非会话日志,而是一个供子Agent使用的无前置上下文的系统提示词。

Behavior

核心要点:

  1. <First step the agent should take>
  2. <Next step>
  3. <...>
  1. **用户修正是最重要的信号。**每一处修正都意味着Agent最初违反了某条规则,所有修正都必须转化为明确的规则。
  2. **只保留有效的方法。**如果方法A失败而方法B有效,仅记录方法B。
  3. 进行泛化处理——将会话特有的值(如文件名、URL、凭证)替换为描述性占位符。该Agent需能处理同类任务,而非仅适用于当前这一项任务。
  4. 保持简洁——这是供子Agent使用的系统提示词,越简短越好。
输出的Agent文件需包含YAML前置元数据,后跟系统提示词主体:
---
name: "<kebab-case-name>"
description: "<one-liner, max 200 chars>"
tools: Read, Glob, Grep, Bash, Write, Edit
model: sonnet
---

You are an agent that <role description — what this agent does>.

Rules

Behavior

  • <Rule derived from user correction or session learning>
  • <Another rule>
  1. <First step the agent should take>
  2. <Next step>
  3. <...>

Output

Rules

<What the agent should produce — format, structure, location.>
undefined
  • <Rule derived from user correction or session learning>
  • <Another rule>

Frontmatter Constraints

Output

  • name
    — required, kebab-case, max 100 characters
  • description
    — required, max 200 characters
  • tools
    — required, comma-separated list of tools the agent needs (choose from: Read, Glob, Grep, Bash, Write, Edit, WebFetch, WebSearch)
  • model
    — required, use
    sonnet
    unless the task clearly needs stronger reasoning (then use
    opus
    )
<What the agent should produce — format, structure, location.>
undefined

Body Constraints

前置元数据约束

  • Start with a one-sentence role description: "You are an agent that..."
  • Behavior section: numbered steps describing what the agent does, in order
  • Rules section: bullet list of constraints and guidelines — every user correction from the session MUST appear here
  • Output section: what the agent produces and in what format
  • All sections are required
  • name
    — 必填项,采用kebab-case格式,最长100字符
  • description
    — 必填项,最长200字符
  • tools
    — 必填项,为Agent所需工具的逗号分隔列表(可选工具:Read, Glob, Grep, Bash, Write, Edit, WebFetch, WebSearch)
  • model
    — 必填项,默认使用
    sonnet
    ,除非任务明确需要更强的推理能力(此时使用
    opus

Guidelines

主体内容约束

  • Write natural language instructions, not formal SHALL/MUST requirements
  • Be specific — "Use openpyxl for Excel files" not "Use the right tool"
  • Do NOT include session-specific details (specific file names, URLs, credentials, data values from this run)
  • DO generalize patterns — replace specific values with descriptive placeholders like
    <input-file>
    ,
    <target-url>
  • Only include steps that succeeded, not failed attempts
  • The agent file MUST be self-contained — the subagent needs nothing beyond this prompt and its input
  • 以一句角色描述开头:"You are an agent that..."
  • **Behavior(行为)**部分:按顺序列出Agent执行的步骤,采用编号格式
  • **Rules(规则)**部分:以项目符号列出约束和准则——会话中的每一处用户修正都必须包含在此
  • **Output(输出)**部分:说明Agent需要生成的内容及其格式
  • 所有部分均为必填项

Step 2: Save the agent file

编写指南

After generating the agent file content (starting with
---
):
  1. Extract the
    name
    from the YAML frontmatter. Use it as the slug directly (it's already kebab-case).
  2. Create the
    .claude/agents/
    directory if it doesn't exist:
    bash
    mkdir -p .claude/agents
  3. Write the agent file content to
    .claude/agents/{name}.md
    using the Write tool.
  • 使用自然语言编写指令,而非正式的SHALL/MUST类强制表述
  • 表述要具体——比如写"Use openpyxl for Excel files"而非"Use the right tool"
  • 请勿包含会话特有的细节(如本次会话中的具体文件名、URL、凭证、数据值)
  • 务必对模式进行泛化——将特定值替换为描述性占位符,如
    <input-file>
    <target-url>
  • 仅保留成功的步骤,不要包含失败的尝试
  • Agent文件必须是自包含的——子Agent仅需该提示词和输入即可运行

Step 3: Display the result

步骤2:保存Agent文件

Tell the user the agent was saved to
.claude/agents/{name}.md
. Let them know they can invoke it with
@{name}
in any conversation. Since the agent lives in the repo, it's automatically shared with anyone who has access to the repository.
生成Agent文件内容(以
---
开头)后:
  1. 从YAML前置元数据中提取
    name
    字段,直接将其作为文件名(已为kebab-case格式)。
  2. 如果
    .claude/agents/
    目录不存在则创建:
    bash
    mkdir -p .claude/agents
  3. 使用Write工具将Agent文件内容写入
    .claude/agents/{name}.md

步骤3:展示结果

告知用户Agent已保存至
.claude/agents/{name}.md
。同时说明他们可在任意会话中通过
@{name}
调用该Agent。由于Agent存储在代码仓库中,所有拥有仓库访问权限的用户都能自动获取该Agent。