
Claude Code's Memory System Explained
克劳德·科德的记忆系统解析
18 分钟阅读1 次阅读AI
这些来源详细解析了 Claude Code 的五层内存架构,揭示了该 AI 智能体如何高效地管理用户偏好与项目上下文。 系统通过会话历史、Markdown 文件以及自动记忆等模块,实现了从短期对话到跨项目长期记忆的无缝衔接。 为了平衡性能与成本,该架构采用了**“派生代理”模式在后台静默提取信息,并利用 MEMORY.md 索引文件进行结构化存储。 记忆的检索过程由专门的筛选模型执行,仅加载最相关的五条记录以节省上下文窗口,同时会对旧数据注入“过期警告”。 这种设计体现了高度约束与安全限制**的理念,通过沙盒环境和严格的读写权限确保 AI 在受控范围内自我优化。
章节 1:简介与五层内存架构
📝 本节摘要:
本节引出了关于 Claude Code 内存架构的探讨。Hesam 提出问题并引入了 Himanshu 的深度解析。Himanshu 指出内存对智能体(Agent)的身份与表现至关重要,并详细解析了 Claude 精密设计的“五层内存架构”,包括:对话历史、会话内存(后台压缩)、CLAUDE.md(用户手动配置)、自动内存(Auto-memory)以及团队内存,其中自动内存是全文的探讨核心。
[原文] [Hesam]: Claude Code's Memory System Explained how does Claude Code keep track of user preferences, manage it's memory or choose what to keep and what to forget?
[译文] [Hesam]: Claude Code 的内存系统原理解析:Claude Code 是如何追踪用户偏好、管理其内存,或者选择保留什么以及忘记什么的?
[原文] [Hesam]: the leaked source code is here to tell us.
[译文] [Hesam]: 泄露的源代码在这里为我们揭晓了答案。
[原文] [Hesam]: yesterday, @himanshustwts wrote a post about the overall architecture of Claude Code's memory system.
[译文] [Hesam]: 昨天,@himanshustwts 写了一篇关于 Claude Code 内存系统整体架构的帖子。
[原文] [Hesam]: I'm here to go more into detail about it.
[译文] [Hesam]: 我今天在这里要对它进行更详细的探讨。
[原文] [Himanshu]: Based on everything explored in the source code, here's the full technical recipe behind Claude Code's memory architecture:
[译文] [Himanshu]: 基于对源代码中探索到的所有内容,以下是 Claude Code 内存架构背后的完整技术配方:
[原文] [Himanshu]: Claude Code’s memory system is actually insanely well-designed.
[译文] [Himanshu]: Claude Code 的内存系统实际上设计得好到令人发指。
[原文] [Himanshu]: It isn't like “store everything” but constrained, structured and Show more Why Memory?
[译文] [Himanshu]: 它不是那种“存储所有内容”的设计,而是受约束的、结构化的。(展开更多)为什么需要内存(Memory)?
[原文] [Himanshu]: because it's a critical part of a an agent's both identity and performance.
[译文] [Himanshu]: 因为它是智能体(agent)的身份(identity)和表现(performance)的至关重要的一部分。
[原文] [Himanshu]: a great deal of my work at @CamelAIOrg has been about memory, so naturally I'm curious to know if Claude's approach to memory is fundamentally different.
[译文] [Himanshu]: 我在 @CamelAIOrg 的大量工作都与内存有关,所以我自然很好奇 Claude 处理内存的方式是否有根本上的不同。
[原文] [Himanshu]: Claude's five-layer Memory Architecture
[译文] [Himanshu]: Claude 的五层内存架构(five-layer Memory Architecture)
[原文] [Himanshu]: Claude doesn't have a memory system. it has five, each operating with a different purpose, cost, and end-user.
[译文] [Himanshu]: Claude 并没有一个单一的内存系统。它有五个,每一个都以不同的目的、成本(cost)和终端用户(end-user)来运作。
[原文] [Himanshu]: The conversation history is the most trivial one.
[译文] [Himanshu]: 对话历史(conversation history)是最基础的一个。
[原文] [Himanshu]: it's a jsonl of all the user prompts and Claude's responses in a conversation which you can export this memory by the /export command.
[译文] [Himanshu]: 它是包含对话中所有用户提示词(user prompts)和 Claude 响应的一个 jsonl 文件,你可以通过 /export 命令导出这个内存。
[原文] [Himanshu]: it's capped at 100 entries per project.
[译文] [Himanshu]: 它在每个项目中的上限是 100 条记录。
[原文] [Himanshu]: Large pastes are hashed and stored separately to keep the log lean.
[译文] [Himanshu]: 大型粘贴文本会被哈希处理(hashed)并单独存储,以保持日志的精简。
[原文] [Himanshu]: COST: ZERO.
[译文] [Himanshu]: 成本:零。
[原文] [Himanshu]: Session memory is a structured markdown memory that's built and updated by a background agent WHILE your conversation is growing.
[译文] [Himanshu]: 会话内存(Session memory)是一种结构化的 markdown 内存,它在你的对话不断增加的同时(WHILE),由一个后台智能体(background agent)构建并更新。
[原文] [Himanshu]: its end-consumer isn't the user, but it's a compaction strategy.
[译文] [Himanshu]: 它的最终消费者不是用户,而是一种压缩策略(compaction strategy)。
[原文] [Himanshu]: currently when the conversation grows, once it reaches ~85% of the context window, your conversation gets summarized.
[译文] [Himanshu]: 目前,当对话增长,一旦达到上下文窗口(context window)的 ~85% 时,你的对话就会被总结。
[原文] [Himanshu]: with the session memory, this would happen on the go.
[译文] [Himanshu]: 有了会话内存,这将会实时进行。
[原文] [Himanshu]: however, this is gated behind tengusessionmemory, one of the many mysterious Claude tengu flags.
[译文] [Himanshu]: 然而,它被限制在 tengusessionmemory 之后,这是 Claude 众多神秘的 tengu 标志(flags)之一。
[原文] [Himanshu]: i'm not sure this is a released feature, or a future one. however, it exists in the code.
[译文] [Himanshu]: 我不确定这是一个已发布的功能,还是未来的功能。不过,它存在于代码中。
[原文] [Himanshu]: CLAUDE.md which you must be familiar with.
[译文] [Himanshu]: CLAUDE.md 你一定很熟悉。
[原文] [Himanshu]: this is the human-controlled level of coding style, preferences, things to keep in memory, etc.
[译文] [Himanshu]: 这是一个由人类控制的层级,包含编码风格(coding style)、偏好,以及需要保存在内存中的事物等。
[原文] [Himanshu]: When you start a session with Claude, the contents of your Claude.md files get added right away.
[译文] [Himanshu]: 当你与 Claude 开始一个会话时,你的 Claude.md 文件的内容会立即被添加进去。
[原文] [Himanshu]: multiple Claude.md files can be loaded as Claude traverses the hierarchical folder structure to discover them.
[译文] [Himanshu]: 随着 Claude 遍历层级文件夹结构来发现它们,多个 Claude.md 文件可以被同时加载。
[原文] [Himanshu]: Auto-memory is what we mean when talking about Claude Code's memory.
[译文] [Himanshu]: 自动内存(Auto-memory)就是我们在谈论 Claude Code 内存时所指的东西。
[原文] [Himanshu]: This is where Claude Code learns about users from multiple conversations, things like their role, preferences, project context, and pointers to external systems.
[译文] [Himanshu]: 这是 Claude Code 从多次对话中学习关于用户信息的地方,比如他们的角色(role)、偏好、项目上下文(project context)以及指向外部系统的指针。
[原文] [Himanshu]: These are stored in ~/.claude/projects/<slug>/memory/.
[译文] [Himanshu]: 这些数据被存储在 ~/.claude/projects/<slug>/memory/ 中。
[原文] [Himanshu]: Team memory. boring. won't get into it.
[译文] [Himanshu]: 团队内存(Team memory)。很无聊。不打算深入探讨。
[原文] [Himanshu]: The focus of this article is on Auto-memory.
[译文] [Himanshu]: 本文的重点是自动内存(Auto-memory)。
[原文] [Himanshu]: These are pretty much all of the memory modules Claude keeps on you, all local and visible to the user.
[译文] [Himanshu]: 这基本上就是 Claude 在你身上保留的所有内存模块,全部都是本地的(local),且对用户可见。
- 章节 2:自动内存的管理与每轮提取机制
- 章节 3:定期整合与内存删除规则
- 章节 4:利用 Sonnet 的内存检索过滤器
- 章节 5:怀疑机制、路径安全与外围约束的意义
正在加载后续章节…




