Skip to content

Latest commit

 

History

History
108 lines (78 loc) · 3.91 KB

File metadata and controls

108 lines (78 loc) · 3.91 KB

Claude Code:Agent Memory Frontmatter

为子代理提供持久记忆 —— 使 Agent 能够跨会话学习、记忆和积累知识。

← 返回 Claude Code 最佳实践 Claude

概述

Claude Code v2.1.33(2026年2月)中引入,memory frontmatter 字段为每个子代理提供了自己的持久性基于 Markdown 的知识存储。在此之前,每次调用 Agent 都是从头开始的。

---
name: code-reviewer
description: Reviews code for quality and best practices
tools: Read, Write, Edit, Bash
model: sonnet
memory: user
---

You are a code reviewer. As you review code, update your agent memory with
patterns, conventions, and recurring issues you discover.

Memory 作用域

作用域 存储位置 版本控制 共享性 最佳用途
user ~/.claude/agent-memory/<agent-name>/ 跨项目知识(推荐的默认值)
project .claude/agent-memory/<agent-name>/ 团队应共享的项目特定知识
local .claude/agent-memory-local/<agent-name>/ 否(被 git 忽略) 个人的项目特定知识

这些作用域与设置层次结构(~/.claude/settings.json.claude/settings.json.claude/settings.local.json)相对应。


工作原理

  1. 启动时MEMORY.md 的前 200 行被注入到 Agent 的系统提示中
  2. 工具访问ReadWriteEdit 自动启用,使 Agent 能够管理其记忆
  3. 执行期间:Agent 自由地读取/写入其记忆目录
  4. 整理:如果 MEMORY.md 超过 200 行,Agent 会将详细信息移动到特定主题的文件中
~/.claude/agent-memory/code-reviewer/     # user 作用域示例
├── MEMORY.md                              # 主文件(加载前 200 行)
├── react-patterns.md                      # 特定主题文件
└── security-checklist.md                  # 特定主题文件

Agent Memory 与其他记忆系统对比

系统 谁来写 谁来读 作用域
CLAUDE.md 你(手动) 主 Claude + 所有 Agent 项目级
Auto-memory 主 Claude(自动) 仅主 Claude 每项目每用户
/memory 命令 你(通过编辑器) 仅主 Claude 每项目每用户
Agent memory Agent 自己 仅该特定 Agent 可配置(user/project/local)

这些系统是互补的 —— Agent 读取 CLAUDE.md(项目上下文)和它自己的记忆(Agent 特定知识)。


实际示例

---
name: api-developer
description: Implement API endpoints following team conventions
tools: Read, Write, Edit, Bash
model: sonnet
memory: project
skills:
  - api-conventions
  - error-handling-patterns
---

Implement API endpoints. Follow the conventions from your preloaded skills.
As you work, save architectural decisions and patterns to your memory.

这将技能(启动时的静态知识)与记忆(随时间积累的动态知识)结合起来。


提示

  • 提示记忆使用 —— 包含明确的指令: "Before starting, review your memory. After completing, update your memory with what you learned."
  • 调用 Agent 时请求记忆检查"Review this PR, and check your memory for patterns you've seen before."
  • 选择正确的作用域 —— user 用于跨项目,project 用于团队共享,local 用于个人

来源