跳到主内容
@wquguru
精选88r/Rag(Reddit)技巧与观点

实测Claude Code上下文窗口:61天数据揭示记忆注入与压缩真相

I measured what actually reaches Claude Code's context window over 61 days of my own transcripts

原文
发到 X
推荐理由

做 Agent 的同学必看,这篇实测直接揭示了 Claude Code 上下文管理的底层行为,给出的验证清单和分片策略能直接落地解决信息丢失问题。

I run several projects alone with Claude Code, often with two or more sessions open at once. Each session starts with nothing from the previous one, and compaction replaces the history with a summary the product writes. Over the last two months I built a set of pieces around that, which I call Hipocampo, to decide what goes into the window of each session, at what moment, and how to check that it got in.

我独自使用 Claude Code 运行多个项目,通常同时打开两个或更多会话。每个会话都从零开始,而压缩(compaction)会用产品生成的摘要替换历史记录。在过去两个月里,我围绕这一机制构建了一套组件,称之为 Hipocampo,用于决定哪些内容进入每个会话的上下文窗口、在何时进入,以及如何验证其是否成功载入。

What it is

它是什么

Everything uses native features: hook events, settings, the MEMORY.md index, CLAUDE.md, skills, subagents and compaction. Nothing installed, no MCP. The commit guards run in the git pre-commit hook.

所有功能均使用原生特性:钩子事件、设置、MEMORY.md 索引、CLAUDE.md、技能(skills)、子代理(subagents)和压缩。无需安装任何额外软件,也不依赖 MCP。提交守卫(commit guards)在 git pre-commit 钩子中运行。

The organizing idea is that each thing lives in one of three regimes:

核心组织理念是每项内容归属于以下三种状态之一:

  • Resident: always arrives, before the first decision (a map loaded by hook, the memory index, a state file).
  • Paged: only arrives if something opens it (CLAUDE.md in subfolders, docs, the memory files themselves).
  • Interrupt: arrives when the command or the file touched matches a registered source, on that event and only on it.
  • 常驻(Resident):始终载入,在首次决策之前(由钩子加载的项目地图、记忆索引、状态文件)。
  • 分页(Paged):仅在有人主动打开时才载入(如子文件夹中的 CLAUDE.md、文档、记忆文件本身)。
  • 中断(Interrupt):当命令或触碰的文件匹配已注册的来源时,在该事件发生时才载入,且仅在此事件中。

What I measured

我的测量结果

61 days of transcripts from this installation (190 main sessions, 918 subagent files), with a positive and a negative control and the population declared for every number.

来自该部署的 61 天对话记录(190 个主会话,918 个子代理文件),包含正负对照组,且每个数据点的人口统计信息均已声明。

0 injected recalls in 190 conversations. A memory only got in when the agent opened its file. 209 of 432 memories were opened at least once (a lower bound).

在 190 次对话中注入的回忆触发次数为 0。记忆仅在代理打开其对应文件时才会被载入。432 条记忆中至少有 209 条被打开过至少一次(此为下限估计)。

Compaction is aggressive. In one event in August, 96.6% of the context was removed (607,378 → 20,766 tokens), and 8 of my 36 messages from before the summary left no trace in it.

压缩非常激进。在 8 月的一次事件中,96.6% 的上下文被移除(从 607,378 降至 20,766 个 token),而在摘要之前的 36 条消息中有 8 条未留下任何痕迹。

Three green instruments, 11% delivered. On Aug 13, the hook that loads my project map emitted 18,057 bytes and about 11% reached the model. The runtime said hook success, the script log said complete, the script's selftest passed 9 of 9. Hook output above 10,000 units per command goes to a file, and only a preview of about 2 KB reaches the model. The only check that caught it was comparing, byte by byte, what the script emitted with what the transcript recorded. The map now goes in 5 slices, each under the ceiling.

三个绿色指标,送达率 11%。8 月 13 日,加载项目地图的钩子输出了 18,057 字节,其中约 11% 到达模型。运行时报告钩子成功,脚本日志显示完成,脚本自检通过 9/9。单命令钩子输出超过 10,000 单位时会写入文件,仅有约 2 KB 的预览能到达模型。唯一发现此问题的方法是将脚本输出的内容与转录记录逐字节比对。现在地图分 5 次载入,每次均低于上限。

The compaction ceiling setting changed the shape of sessions. Until Sep 18, conversations went up to almost 1M tokens before the summary. Since Sep 21, 64 of 65 compactions happened at or below 365K.

压缩上限设置改变了会话的结构。9 月 18 日之前,对话在生成摘要前可接近 1M token。自 9 月 21 日起,65 次压缩中有 64 次发生在 365K token 及以下。

A guard only counts after it has rejected a defect planted on purpose. On Sep 25, 52 of 74 guards had that proof. The rest are declared as debt.

守卫仅在成功拦截一次故意植入的缺陷后才被计入有效。9 月 25 日,74 个守卫中有 52 个具备此类证明。其余的则被记为技术债务。

How to build it

如何构建

The article ends with the order I would follow, in 7 steps, each with the native feature it relies on and the red that proves it works. Every step ends by breaking the piece on purpose and waiting for the failure.

文章最后列出了我将遵循的7个步骤的顺序,每一步都说明了其依赖的原生特性以及证明其有效的红色标记。每个步骤都以故意破坏该部分并等待失败作为结束。

Limits

限制

One operator, one installation. The study does not say whether the memory that arrives is correct or whether it was useful.

一个操作符对应一个安装实例。研究并未说明传入的内存是否正确,或者它是否有效。

Links

链接

  • Full article (architecture piece by piece, the measurement bench, 33 figures, limitations): https://ksmit.com.br/en/blog/hipocampo
  • Code (hooks as a settings example, the memory guards with their failing fixtures, the measuring scripts, memory and map templates; MIT): https://github.com/JhouCode/hipocampo
  • 完整文章(逐块解析架构、测量基准、33张图表、局限性):https://ksmit.com.br/en/blog/hipocampo
  • 代码(以 hooks 为例展示配置、带有失败测试用例的内存守卫、测量脚本、内存和映射模板;MIT 协议):https://github.com/JhouCode/hipocampo

Written and tested on Linux with bash and python3. If you run the measuring scripts on your own transcripts, I'd like to know what number you get for injected recalls.

在 Linux 环境下使用 bash 和 python3 编写并测试。如果你在自己的转录数据上运行测量脚本,我想知道你得到的注入召回率数值是多少。

更进一步:量化金融体系

看懂新闻只是起点——沿量化金融路径,把它变成能交付的工程能力

进入量化体系 →

相似阅读

关联信息,但可能不是同一事件