# How to give your Claude agent a memory in 12 steps: from first setup to self-improving.
**作者**: Codez
**日期**: 2026-05-23T12:01:46.000Z
**来源**: [https://x.com/0xCodez/status/2058156429559636069](https://x.com/0xCodez/status/2058156429559636069)
---

I have combined everything that actually works about Claude memory -official docs, the Dreaming research preview, real production setups - into one walkthrough.
> Follow my Substack to get fresh AI alpha: movez.substack.com
Bookmark this. Save it. By the end you will have an agent that remembers permanently - not "within a chat," but across weeks.
That is not hyperbole. Until March 2026 this was impossible for normal users. Now it takes 12 steps.
## Why every agent you have built is a goldfish.
Every time you open a new Claude chat, it starts from zero.
It does not know your name. It does not remember the three corrections you made yesterday, the workaround it discovered last week, or the fact that you hate bullet-point summaries.
Each session it rediscovers the same things, makes the same mistakes, and asks the same questions you already answered.
This is not a flaw in how Claude is marketed. It is a fundamental property of how language models work: each session starts fresh unless you explicitly feed context back in.
For a chatbot, that is fine. For an agent running real, repeating work, it is the single biggest reason your setup plateaus. An agent with no memory is exactly as useful on run 100 as it was on run 1.

Layer 1 is a sticky note. Layer 4 is an employee who reflects on the week every Friday and comes back sharper. These 12 steps build all four, in three parts.
## Foundation - Layers 1 & 2
> 01. Turn on Claude's built-in memory
Start with the thing most people do not realize already exists. In March 2026, Anthropic rolled out persistent memory, called "Chat Memory" - to every Claude account, free and Pro alike.
Claude now remembers your preferences, ongoing projects, and working style across every conversation, automatically, until you tell it to forget.
Click your profile icon → Settings → Capabilities → scroll to the Memory section, and confirm "Generate memory from chat history" is on.

Under the hood it runs Memory Synthesis - Claude distills your conversations into a profile roughly every 24 hours. This is the baseline everything else builds on.
> 02. Seed your memory deliberately instead of waiting
The counterintuitive part the docs are explicit about: do not wait for Claude to infer your preferences from history - that synthesis only runs about once every 24 hours.
Tell it directly in a fresh conversation. Explicit beats inferred every time, and it lands immediately instead of a day later.
```
Remember the following about me for future conversations:
- I work in [field] and my main projects are [X, Y]
- I prefer [direct prose / no bullet points / short replies]
- My writing style is [describe it]
- Never [the thing you always have to correct]
```
Claude writes these into memory. The next conversation starts already knowing them. One message eliminates an entire category of repeated explanation.
> 03. Create a Project as your agent's home
A Project is a persistent workspace where the instructions stay loaded across every conversation inside it. This is Layer 1 in its strongest form.

Go to Projects → New Project. Name it after the job, not the topic. Then fill the custom instructions box with the agent's role, standards, and constraints. Every chat inside that Project inherits them.
> 04. Understand what Projects do NOT remember
This is where most people get burned. Projects persist instructions. They do not persist conversation memory by default.

You set up a Project, give it detailed context, work for a few conversations - then start a new chat in the same Project, and everything you discussed before is gone.
The architecture decisions, the half-finished task, the debugging session -vanished. The Project remembers its instructions, not your history.
## Persistent Memory - Layer 3 (for coders)
> 05. Add a living memory file
The simplest persistent memory that actually works is a single file the agent reads at the start and appends to at the end.

CLAUDE.md example
In Claude Code this is CLAUDE.md; for any agent it can be a memory.md in Project Knowledge.
The rule the docs hammer on: keep it lean. A fresh session can spend roughly 20,000 tokens loading instructions before you type anything.
Do not treat this file like a wiki dump. If you use Claude Code's /init to generate a starter file, the counterintuitive next step is to delete most of what it generates - it states obvious things the model already sees.
> 06. Turn on auto memory
Claude Code has an auto-memory mechanism: it writes notes to itself based on your corrections and preferences, and loads them at the start of every session.
```
Toggle it with /memory in a session, or set autoMemoryEnabled in project settings.
```
Now the agent does light self-documentation. When you correct it, the correction can survive into the next session instead of evaporating.
> 07. Structure the memory file so it stays useful
A memory file that grows without structure becomes noise. Give it sections:
```
## Preferences
- Bullet summaries over prose for status updates
- Always cite the source file for any claim
## Decisions
- 2026-04-18 - chose Postgres over Mongo (relational reporting)
## Known workarounds
- Export tool chokes on files >50MB; split first
## Recurring mistakes to avoid
- Do not auto-approve PRs touching the auth module
```
Each entry earns its place. This is the file the agent consults to stop repeating yesterday's mistakes.
> 08. Decide what is worth remembering
Not everything should be saved. The discipline here is the whole game. After each significant session, the agent reviews what happened and extracts only what is worth keeping: a decision, a workaround, a preference, a failure mode. Everything else is forgotten on purpose.
A good filter: would this change how the agent acts next time? If yes, store it. If no, let it go. Memory that stores everything is as useless as memory that stores nothing.
## Self-Improving Memory - Layer 4: Dreaming
> 09. Understand what Dreaming actually is
On May 6, 2026, at Code with Claude, Anthropic shipped Dreaming as a research preview for Managed Agents. The name is borrowed from neuroscience on purpose: when humans sleep, the brain consolidates the day's experiences into long-term memory. Dreaming does the same for an agent.
It is a scheduled background process. It reads the agent's existing memory store plus past session transcripts, then produces a new, reorganized memory store: duplicates merged, stale entries replaced with the latest value, and genuinely new insights surfaced.

One condition matters before you bother: Dreaming only helps agents that run the same kind of task repeatedly. It consolidates patterns across many sessions, so a one-off agent has nothing to consolidate. Run it on a workhorse, not a tourist.
> 10. Run a dream - the exact API procedure
Dreaming is a research preview, so there are three prerequisites before you write any code: a Managed Agents API key, access to Dreaming requested through Anthropic's form (it ships gated), and a Python or TypeScript environment with the latest Anthropic SDK.
Every dream call needs two beta headers stacked together - the SDK sets both automatically if you are on the dreaming-aware version:
```
anthropic-beta: managed-agents-2026-04-01,dreaming-2026-04-21
```
The call itself takes two kinds of input: the existing memory store you want to consolidate, and up to 100 session IDs - recent agent runs Claude will mine for patterns.
You can also pass instructions to steer what the dream focuses on:
```
dream = client.beta.dreams.create(
inputs=[
{"type": "memory_store", "memory_store_id": store_id},
{"type": "sessions", "session_ids": [session_a, session_b]},
],
model="claude-opus-4-7",
instructions="Focus on coding-style preferences; "
"ignore one-off debugging notes.",
)
print(dream.id) # drm_01...
```
Supported models during the preview are claude-opus-4-7 and claude-sonnet-4-6. Dreams are billed at standard API token rates for the model you pick, and cost scales roughly linearly with the number and length of input sessions.
The docs are explicit: start with a small batch of sessions, scale up once you are happy with the curation quality.
> 11. Inspect the output store before you commit
The input memory store stays read-only the entire time. The dream produces a separate output store, and its ID appears in the dream's outputs[] array once the run starts:
```
# After the dream ends, the output holds the rebuilt store
output_store_id = next(
output.memory_store_id
for output in dream.outputs
if output.type == "memory_store"
)
```
Now read it. Check that merged entries are correct, that replaced entries actually were stale, that the surfaced insights are real and not noise.
This review step is the difference between an agent that gets smarter and one that quietly drifts. Because the output is a brand-new store you opt in to, a dream can never silently corrupt what you already have.
> 12. Swap it in, schedule it, let it compound
Once you trust the output, the switch to production is a one-line change - point your agent at the new store ID instead of the old one.
Then put dreaming on a schedule: nightly or weekly, depending on how much the agent runs.
Now the loop is closed. The agent works during the day, dreams between runs, and comes back sharper each cycle, with no retraining and no manual reconfiguration.
Archiving an old dream never touches its output store -you manage those separately through the Memory Stores API.
The proof it works at scale: legal-AI company Harvey reported roughly a 6x increase in agent task-completion rates after enabling Dreaming for legal-drafting workflows.
The same jobs that used to fail because Claude kept forgetting filetype quirks and tool workarounds between sessions suddenly started finishing reliably.
## The mistakes that break agent memory.
- Treating Projects as memory. Projects persist instructions, not conversation history. Assume otherwise and you will lose context without understanding why.
- Dumping everything into CLAUDE.md. A bloated memory file wastes tokens and buries the signal. Lean and structured beats long and complete.
- Storing memory with no filter. If everything is worth remembering, nothing is. Save only what would change future behavior.
- Auto-deploying dream output. The whole point of the separate output store is review. Skip it and you lose the safety net.
- Running Dreaming on a low-frequency agent. Dreaming consolidates patterns across many sessions. An agent that runs twice a month never accumulates enough.
## Conclusion:
Most people will keep opening Claude the way they always have - a fresh, forgetful chat every time, re-explaining themselves on every run, wondering why their agent never gets better.
The ones who build the four layers will have something different: an agent that knows them, accumulates what it learns, and rewrites its own memory to get sharper every week.
Pick the first four steps. Set them up tonight. That alone will change how your next session feels.
## 相关链接
- [Codez](https://x.com/0xCodez)
- [@0xCodez](https://x.com/0xCodez)
- [186K](https://x.com/0xCodez/status/2058156429559636069/analytics)
- [movez.substack.com](https://movez.substack.com/)
- [Upgrade to Premium](https://x.com/i/premium_sign_up)
- [8:01 PM · May 23, 2026](https://x.com/0xCodez/status/2058156429559636069)
- [186.1K Views](https://x.com/0xCodez/status/2058156429559636069/analytics)
- [View quotes](https://x.com/0xCodez/status/2058156429559636069/quotes)
---
*导出时间: 2026/5/24 09:26:04*
---
## 中文翻译
# 如何通过 12 个步骤赋予你的 Claude 智能体记忆:从初始设置到自我进化
**作者**: Codez
**日期**: 2026-05-23T12:01:46.000Z
**来源**: [https://x.com/0xCodez/status/2058156429559636069](https://x.com/0xCodez/status/2058156429559636069)
---

我将关于 Claude 记忆所有真正有效的部分——官方文档、“Dreaming”研究预览版、真实的生产环境设置——整合成了一份操作指南。
> 关注我的 Substack 获取最新 AI 资讯:movez.substack.com
收藏这篇文章。保存下来。读完之后,你将拥有一个具备永久记忆的智能体——不是“在聊天窗口内”,而是跨越数周的持久记忆。
这并非夸大其词。直到 2026 年 3 月,这对普通用户来说还是不可能实现的。现在只需要 12 个步骤。
## 为什么你构建的每个智能体都只有七秒记忆。
每次你打开一个新的 Claude 对话,它都是从零开始。
它不知道你的名字。它不记得你昨天做的三次修正,上周发现的变通方法,或者你讨厌要点总结的事实。
在每个会话中,它都在重新发现相同的事情,犯同样的错误,并询问你已经回答过的问题。
这不是 Claude 营销方式上的缺陷。这是语言模型工作方式的基本属性:除非你明确地将上下文反馈回去,否则每个会话都是全新的。
对于聊天机器人来说,这没问题。但对于运行真实、重复性工作的智能体来说,这是你的设置陷入瓶颈的最大原因。没有记忆的智能体,在第 100 次运行时与第 1 次运行时的作用完全一样。

第 1 层是一张便利贴。第 4 层是一位每周五反思工作并变得更敏锐的员工。这 12 个步骤分为三个部分,构建了所有这四层。
## 基础 - 第 1 层和第 2 层
> 01. 开启 Claude 的内置记忆功能
从大多数人没有意识到已经存在的东西开始。2026 年 3 月,Anthropic 向所有 Claude 账户(免费版和专业版)推出了持久记忆功能,称为“聊天记忆”。
Claude 现在会自动记住你的偏好、正在进行的项目和工作风格,涵盖每一次对话,直到你告诉它忘记为止。
点击你的个人资料图标 → 设置 → 功能 → 滚动到记忆部分,并确认“从聊天历史生成记忆”已开启。

在底层,它运行记忆综合——Claude 大约每 24 小时将你的对话提炼成一份个人档案。这是其他一切构建的基础。
> 02. 主动植入记忆,而不是等待
文档中明确指出的一个反直觉部分:不要等待 Claude 从历史记录中推断你的偏好——这种综合大约每 24 小时才运行一次。
在一个新的对话中直接告诉它。显式表达总是胜过推断,并且会立即生效,而不是一天后才生效。
```
在未来的对话中请记住关于我的以下事项:
- 我在 [领域] 工作,我的主要项目是 [X, Y]
- 我更喜欢 [直接散文 / 无要点 / 简短回复]
- 我的写作风格是 [描述一下]
- 永远不要 [你总是不得不纠正的那件事]
```
Claude 会将这些写入记忆。下一次对话开始时它就已经知道了。一条信息就消除了整整一类重复的解释。
> 03. 创建一个项目作为智能体的基地
项目是一个持久的工作空间,其中的指令会在其内部的每一次对话中保持加载。这是第 1 层最强大的形式。

前往 项目 → 新建项目。以工作命名,而不是主题。然后在自定义指令框中填写智能体的角色、标准和约束。该项目内的每个聊天都会继承这些内容。
> 04. 理解项目不记住什么
这是大多数人吃亏的地方。项目持久化指令。默认情况下,它们不持久化对话记忆。

你建立了一个项目,给出了详细的上下文,进行了几次对话——然后在同一个项目中开始一个新的聊天,之前讨论的所有内容都消失了。
架构决策、未完成的任务、调试会话——全都没了。项目记住的是它的指令,而不是你的历史记录。
## 持久记忆 - 第 3 层(面向开发者)
> 05. 添加一个鲜活的记忆文件
实际可行的最简单的持久记忆是一个单一文件,智能体在开始时读取它,在结束时追加内容。

CLAUDE.md 示例
在 Claude Code 中,这是 CLAUDE.md;对于任何智能体,它可以是项目知识库中的 memory.md。
文档强调的规则:保持精简。一个新会话在你输入任何内容之前,可能会花费大约 20,000 个 token 来加载指令。
不要把这个文件当作维基垃圾场。如果你使用 Claude Code 的 /init 生成初始文件,接下来的反直觉步骤是删除它生成的大部分内容——它陈述了模型已经看到的显而易见的事情。
> 06. 开启自动记忆
Claude Code 有一个自动记忆机制:它根据你的修正和偏好为自己写笔记,并在每次会话开始时加载它们。
```
在会话中使用 /memory 切换,或在项目设置中设置 autoMemoryEnabled。
```
现在智能体会进行轻度自我文档记录。当你纠正它时,这种修正可以留存到下一个会话,而不是消失殆尽。
> 07. 构建记忆文件的结构以保持其有用性
一个没有结构而不断增长的记忆文件会变成噪音。给它分几个部分:
```
## 偏好
- 状态更新优先使用要点而非散文
- 任何声明都要引用源文件
## 决策
- 2026-04-18 - 选择 Postgres 而非 Mongo(关系型报表)
## 已知的变通方法
- 导出工具处理大于 50MB 的文件会卡死;先拆分
## 需避免的反复错误
- 不要自动批准涉及 auth 模块的 PR
```
每个条目都必须赢得它的位置。这是智能体查阅以停止重蹈覆辙的文件。
> 08. 决定什么值得被记住
并非所有事情都应该被保存。这里的纪律是关键。在每次重要的会话后,智能体会审查发生的事情,并只提取值得保留的内容:一个决策、一个变通方法、一个偏好、一个失败模式。其他所有事情都会被故意遗忘。
一个好的过滤器:这会改变智能体下次的行动方式吗?如果是,存储它。如果否,随它去。存储所有内容的记忆与什么都不存储的记忆一样无用。
## 自我进化记忆 - 第 4 层:Dreaming
> 09. 理解 Dreaming 到底是什么
2026 年 5 月 6 日,在 Code with Claude 大会上,Anthropic 发布了 Dreaming 作为托管智能体的研究预览版。这个名字是有意从神经科学借用的:当人类睡觉时,大脑会将白天的经历巩固成长期记忆。Dreaming 对智能体起着同样的作用。
它是一个定期的后台进程。它读取智能体现有的记忆库和过去的会话记录,然后生成一个新的、经过重组的记忆库:重复项被合并,过时的条目被最新值替换,真正的新见解被浮现出来。

在费心之前,有一个条件很重要:Dreaming 只对反复运行同类型任务的智能体有帮助。它整合了许多会话中的模式,所以一次性智能体没有什么可整合的。在“主力军”上运行它,而不是“游客”。
> 10. 运行一次 Dream——确切的 API 流程
Dreaming 是一个研究预览版,因此在编写任何代码之前有三个先决条件:一个托管智能体 API 密钥、通过 Anthropic 表单申请的 Dreaming 访问权限(它是受限发布的),以及安装了最新 Anthropic SDK 的 Python 或 TypeScript 环境。
每次 dream 调用需要两个叠加的 beta 头——如果你使用的是支持 dreaming 的版本,SDK 会自动设置这两个:
```
anthropic-beta: managed-agents-2026-04-01,dreaming-2026-04-21
```
调用本身接受两种输入:你想要整合的现有记忆库,以及最多 100 个会话 ID——最近的智能体运行记录,Claude 将从中挖掘模式。
你还可以传递指令来引导 dream 的关注点:
```
dream = client.beta.dreams.create(
inputs=[
{"type": "memory_store", "memory_store_id": store_id},
{"type": "sessions", "session_ids": [session_a, session_b]},
],
model="claude-opus-4-7",
instructions="Focus on coding-style preferences; "
"ignore one-off debugging notes.",
)
print(dream.id) # drm_01...
```
预览期间支持的模型是 claude-opus-4-7 和 claude-sonnet-4-6。Dream 按你所选模型的标准 API token 费率计费,成本与输入会话的数量和长度大致呈线性关系。
文档明确指出:从一小批会话开始,一旦你对策展质量满意,再扩大规模。
> 11. 在提交之前检查输出库
输入记忆库在整个过程中保持只读状态。dream 会生成一个单独的输出库,一旦运行开始,它的 ID 会出现在 dream 的 outputs[] 数组中:
```
# dream 结束后,output 包含重建后的库
output_store_id = next(
output.memory_store_id
for output in dream.outputs
if output.type == "memory_store"
)
```
现在读取它。检查合并的条目是否正确,被替换的条目是否真的过时了,浮现出的见解是否真实而非噪音。
这个审查步骤是智能体变聪明与悄悄“漂移”之间的区别。因为输出是一个全新的、你需要选择加入的库,所以 dream 永远不会静默破坏你已经拥有的内容。
> 12. 切换使用,定时运行,让其复利增长
一旦你信任输出内容,切换到生产环境只需要一行代码的更改——将你的智能体指向新的库 ID 而不是旧的。
然后将 dreaming 提上日程:每晚或每周,取决于智能体的运行频率。
现在循环闭合了。智能体白天工作,在运行间隙做梦,并在每个周期回来时变得更敏锐,无需重新训练,也无需手动重新配置。
归档旧的 dream 不会触及它的输出库——你需要通过记忆库 API 单独管理它们。
这在规模上有效的证明:法律 AI 公司 Harvey 报告称,在为法律起草工作流启用 Dreaming 后,智能体任务完成率提高了约 6 倍。
那些过去因为 Claude 总是在会话之间忘记文件类型怪癖和工具变通方法而失败的工作,现在突然开始可靠地完成了。
## 破坏智能体记忆的常见错误。
- 将项目视为记忆。项目持久化指令,而不是对话历史。如果假设相反,你会在不知情的情况下丢失上下文。
- 把所有东西都倒进 CLAUDE.md。臃肿的记忆文件会浪费 token 并埋没信号。精简和结构化胜过长而完整。
- 无过滤地存储记忆。如果一切都值得记住,那就没有什么是值得记住的。只保存那些会改变未来行为的内容。
- 自动部署 dream 输出。单独的输出库的全部意义就在于审查。跳过这一步,你就失去了安全网。
- 在低频智能体上运行 Dreaming。Dreaming 整合许多会话中的模式。一个月运行两次的智能体永远无法积累足够的数据。
## 结论:
大多数人会继续像往常一样打开 Claude——每次都是一个新的、健忘的聊天,每次运行都要重新解释自己,想知道为什么他们的智能体从未变得更好。
那些构建了这四层的人将拥有不同的东西:一个了解他们的智能体,积累它学到的东西,并重写自己的记忆以每周变得更敏锐。
选择前四个步骤。今晚就设置好。仅此一项就会改变你下一次会话的感觉。
## 相关链接
- [Codez](https://x.com/0xCodez)
- [@0xCodez](https://x.com/0xCodez)
- [186K](https://x.com/0xCodez/status/2058156429559636069/analytics)
- [movez.substack.com](https://movez.substack.com/)
- [Upgrade to Premium](https://x.com/i/premium_sign_up)
- [8:01 PM · May 23, 2026](https://x.com/0xCodez/status/2058156429559636069)
- [186.1K Views](https://x.com/0xCodez/status/2058156429559636069/analytics)
- [View quotes](https://x.com/0xCodez/status/2058156429559636069/quotes)
---
*导出时间: 2026/5/24 09:26:04*