5步构建低Token消耗的自治Agent系统 ✍ codila🕐 2026-07-17📦 8.9 KB 🟢 已读 𝕏 文章列表 文章介绍了构建消耗Token减少90%的自管理Agent系统的五个步骤:将上下文移出窗口、使用子代理处理脏活、利用自管理内存、避免破坏缓存以及手动管理上下文。强调了通过优化上下文管理和缓存机制来降低成本和提升效率的重要性。 AgentToken优化上下文管理缓存机制Claude Code子代理自管理内存 # Build self-managed agent system that uses 90% fewer tokens in 5 steps : subagents, memory, context **作者**: codila **日期**: 2026-07-13T23:37:32.000Z **来源**: [https://x.com/0xCodila/status/2077780537016488197](https://x.com/0xCodila/status/2077780537016488197) ---  Your agents forgets what you told it ten minutes ago - and charges you more for the privilege Every turn, it re-sends everything it's holding: the whole codebase, the whole doc, the whole history You pay for all of it, again, on every single message, then it forgets anyway > I put together 4 steps that fix the memory and token-spend problem for good and +1 step that will create self managed agent system for you > before the alpha - subscribe to my substack for more fresh alpha ↓ > https://substack.com/@0xcodila ## Step 1 - Get the context out of the window  The instinct is to load everything up front so the model "has what it needs" That's the trap - everything you load is something you pay for on every turn until the conversation ends - Put the big stuff in a file instead - the book, the codebase, the docs, the logs Then let the agent reach into that file only when it actually needs a piece of it - It can grep for a keyword, read one section, and move on The 108,000-token book sits on disk. The question costs you the question. → This one move is where most of the savings come from. The rest of the steps are about not undoing it How Claude Code uses prompt caching ## Step 2 - Send the messy work to a subagent  Some jobs generate a wall of tokens you don't want in your main thread - running a test suite, reading a stack trace, crawling a doc site. - All of that noise lands in your context window and stays there, costing you on every following turn. - Hand those jobs to a subagent It does the dirty work in its own separate window, burns through the noise there, and hands you back a clean summary. Your main thread never sees the 4,000 lines of test output - it sees "three tests failed, here's why" → Think of the subagent as a filter- everything loud goes in, one quiet answer comes out Subagents build their own cache and context # Step 3 - memory self-managed agent  (the crazy part) Your agent starts every session from zero. You re-explain the same context, and you pay tokens to reload it Claude Code fixes this natively -and most people don't know it's already on > Auto Memory means Claude writes its own notes between sessions - build commands, bug fixes, the workarounds you found - and loads them at the start of the next one > you don't write anything - It decides what's worth keeping Do this today: → Run /memory to see what Claude has already recorded on its own. You'll usually find it's captured conventions and fixes you never documented. → Delete those lines from your CLAUDE.md. Anything Claude learns after one session doesn't belong in the file you pay for on every turn - the memory holds it for free. → For bigger projects, make MEMORY.md a table of contents, not a dump One line per topic file (gotchas.md, conventions.md), so Claude pulls in only the file the current task needs - not all 200 lines. - That last move is the whole game in miniature: the agent carries its knowledge forward, but only the slice it needs enters the prefix Small prefix, small bill, and you stopped re-explaining yourself. → It's already running. Your only job is to stop duplicating in CLAUDE.md what memory already holds. Source: Claude Code auto memory docs and Anthropic - Agent memory & "dreaming ## Step 4 - Stop breaking the cache  (my favorite step) Here's the part most people don't know is running Everything stable at the front of your prompt - tools, system prompt, that tidy CLAUDE.md - gets cached after the first turn. A cached token costs a tenth of a fresh one > The catch: the cache matches an exact prefix. Change one character before the cache point and everything after it goes back to full price > Silently. No error. The usual culprits: - A timestamp or date baked into the system prompt - changes constantly, breaks the cache every time. - Switching models mid-session - each model keeps its own cache, so a /model swap re-reads the whole thing from scratch. - A stray space, a reordered tool list, or an MCP tool added halfway through. The rule is simple, static stuff at the top, in a fixed order. Anything that changes - the date, the user's turn, session junk - goes at the bottom, below the cache point Anthropic treats this as serious enough that internal teams declare an incident when their hit rate drops Anthropic prompt caching docs GitHub keeps theirs above 94% and calls a drop to 70% a bug. Their CPO put it well - it's like high-frequency trading, where 1% of efficiency is millions of dollars → Most people are sitting at 40% and have no idea ## Step 5 - Manage the context by hand  > Claude Code will compact your context on its own when it fills up > The problem is it does this on its schedule, not yours, and compaction can quietly eat most of your remaining tokens at the worst moment Two commands give you the wheel back: ``` /context ``` - - shows you what's actually taking up space right now - you'll usually find something bloated you forgot about. ``` /compact ``` - - lets you squash the conversation at a clean break, like when you finish one task and start another, instead of waiting for the automatic version to trigger mid-thought. → The habit is small, Check /context when a session feels heavy. Compact between tasks, not in the middle of one # Part 6 · Honestly The five steps above genuinely cut your bill - you just learned the thing most people never touch - and that alone puts you ahead of them > One thing worth saying out loud, so this keeps working None of it is set-and-forget. Caching lives on a number you only see if you go look - your hit rate, your cache_read_input_tokens. Setting it up once is great - Glance at it once a week, and a stray timestamp in your prefix won't quietly steal the savings back. A cheap agent is a state you hold, and holding it is easy now, because you know where to look > Here's the good news: you're in good company. Andrew Ng and Anthropic arrived at the same place from the other side - Ng teaches you to move an agent's memory out of the window - that's step one, just from the memory angle - Anthropic went further: an agent that cleans its own memory in the background, so the prefix stays small on its own > **codila@0xCodila**: [原文链接](https://x.com/0xCodila/status/2076813306128404833) > > Anthropic and Andrew Ng built an agent that uses 90% fewer tokens from scratch: > they dropped the entire book of Frankenstein into a prompt - 108,000 tokens asked one question > the input dropped from 108,000 tokens to 11 > here's how: > step 1 → put everything that never changes > >  Three different doors, one room - stop carrying everything, start carrying what the task needs - you just walked in > The tools will change: cache, subagents, memory stores - half these names will be different in six months > One thing won't: the engineer who knows what their agent is holding, and why, always beats the one who dumped everything into the window and hoped Now you're the first one, so run your agent like an engineer. @0xCodila ## 相关链接 - [codila](https://x.com/0xCodila) - [@0xCodila](https://x.com/0xCodila) - [15K](https://x.com/0xCodila/status/2077780537016488197/analytics) - [https://substack.com/@0xcodila](https://substack.com/@0xcodila) - [How Claude Code uses prompt caching](https://code.claude.com/docs/en/prompt-caching) - [Subagents build their own cache and context](https://code.claude.com/docs/en/prompt-caching) - [Claude Code auto memory docs](https://code.claude.com/docs/en/memory) - [Anthropic - Agent memory & "dreaming](https://youtu.be/geUv4CjPpxI?si=SdlxnX2BZFNWPKtb) - [Anthropic prompt caching docs](https://platform.claude.com/docs/en/build-with-claude/prompt-caching) - [Jul 14](https://x.com/0xCodila/status/2076813306128404833) - [524K](https://x.com/0xCodila/status/2076813306128404833/analytics) - [@0xCodila](https://x.com/@0xCodila) - [Upgrade to Premium](https://x.com/i/premium_sign_up) - [11:40 PM · Jul 16, 2026](https://x.com/0xCodila/status/2077780537016488197) - [15.1K Views](https://x.com/0xCodila/status/2077780537016488197/analytics) - [View quotes](https://x.com/0xCodila/status/2077780537016488197/quotes) --- *导出时间: 2026/7/17 15:16:01* --- ## 中文翻译 # 通过 5 个步骤构建可减少 90% Token 使用的自管理 Agent 系统:子代理、记忆、上下文 **作者**: codila **日期**: 2026-07-13T23:37:32.000Z **来源**: [https://x.com/0xCodila/status/2077780537016488197](https://x.com/0xCodila/status/2077780537016488197) ---  你的 Agent 会忘记你十分钟前告诉它的事情——而且还为此向你收取更多费用 每一轮对话,它都会重新发送它持有的所有内容:整个代码库、整个文档、整个历史记录 每条消息你都要为所有这些内容再次付费,然后它还是照样会忘记 > 我整理了 4 个步骤,可以彻底解决记忆和 Token 消耗问题,还有 +1 个步骤可以为你创建自管理的 Agent 系统 > 在获取 alpha 资讯之前——订阅我的 Substack 以获取更多新鲜资讯 ↓ > https://substack.com/@0xcodila ## 第 1 步 - 将上下文移出窗口  人的本能是在一开始就加载所有内容,以便模型“拥有它所需的东西” 这就是陷阱——你加载的每一样东西,都会在每一轮对话中让你付费,直到对话结束 - 把大东西放在文件里——书、代码库、文档、日志 然后让 Agent 只在真正需要其中一部分时才去读取该文件——它可以搜索关键词,阅读其中一个章节,然后继续处理 那本 108,000 Token 的书静静地躺在磁盘上。你只需为你的问题付费。 → 这一步操作是节省大部分成本的关键所在。其余的步骤都是为了不破坏这一成果 Claude Code 如何使用提示词缓存 ## 第 2 步 - 将杂乱的工作交给子代理  有些工作会产生一大堆你不希望出现在主线程中的 Token——运行测试套件、阅读堆栈跟踪、爬取文档网站。 - 所有这些噪音都会进入你的上下文窗口并停留在那里,让你在随后的每一轮对话中都付出代价。 - 将这些工作交给子代理 它在自己的独立窗口里干脏活累活,在那里消耗掉噪音,然后给你交回一份干净的摘要。 你的主线程永远看不到那 4,000 行测试输出——它看到的只是“三个测试失败,原因如下” → 把子代理想象成一个过滤器——所有嘈杂的输入进去,一个安静的答案出来 子代理构建自己的缓存和上下文 # 第 3 步 - 记忆自管理 Agent  (疯狂的部分) 你的 Agent 每次会话都从零开始。你重新解释相同的上下文,并支付 Token 来重新加载它 Claude Code 在原生层面解决了这个问题——而大多数人不知道它已经开启了 > 自动记忆意味着 Claude 会在会话之间自己写笔记——构建命令、Bug 修复、你发现的变通方法——并在下一次会话开始时加载它们 > 你不需要写任何东西——它决定什么值得保留 今天就这样做: → 运行 /memory 查看 Claude 已经自行记录了什么。通常你会发现它记录了你从未编写成文档的约定和修复方法。 → 从你的 CLAUDE.md 中删除那些行。Claude 在一次会话后学到的任何东西都不属于那个你在每一轮都要付费的文件——记忆会免费保存它。 → 对于更大的项目,让 MEMORY.md 成为一个目录,而不是一个垃圾堆 每个主题文件一行(gotchas.md, conventions.md),这样 Claude 只引入当前任务需要的文件——而不是全部 200 行。 - 最后这招是整个游戏的缩影:Agent 将其知识向前传递,但只有它需要的那个切片会进入前缀 前缀小,账单少,而且你不再重复解释自己。 → 它已经在运行了。你唯一的工作就是停止在 CLAUDE.md 中重复记忆已经保存的内容。 来源:Claude Code 自动记忆文档以及 Anthropic - Agent 记忆与“做梦” ## 第 4 步 - 停止破坏缓存  (我最喜欢的一步) 这是大多数人不知道正在运行的部分 你提示词前面所有稳定的内容——工具、系统提示词、那整洁的 CLAUDE.md——在第一轮后都会被缓存。一个被缓存的 Token 成本仅为全新 Token 的十分之一 > 问题是:缓存匹配的是精确的前缀。在缓存点之前更改一个字符,它之后的所有内容都会恢复全价 > 静悄悄地。没有错误。 常见的罪魁祸首: - 烘焙在系统提示词中的时间戳或日期——不断变化,每次都破坏缓存。 - 会话中途切换模型——每个模型都有自己的缓存,所以 /model swap 会从头重新读取所有内容。 - 一个多余的空格、一个重新排序的工具列表,或者中途添加的一个 MCP 工具。 规则很简单,静态的东西放在顶部,按固定顺序。 任何会变化的东西——日期、用户的回合、会话垃圾——放在底部,在缓存点之下 Anthropic 对此非常重视,以至于当内部团队的命中率下降时,他们会宣布为一个事故 Anthropic 提示词缓存文档 GitHub 将其保持在 94% 以上,并将下降到 70% 视为一个 Bug。 他们的首席产品官说得好——这就像高频交易,1% 的效率就是数百万美元 → 大多数人停留在 40%,却毫不知情 ## 第 5 步 - 手动管理上下文  > Claude Code 会在上下文填满时自动压缩它 > 问题是它是按自己的时间表进行的,而不是你的,而且压缩可能会在最糟糕的时刻悄悄吃掉你剩余的大部分 Token 两个命令让你重新掌舵: ``` /context ``` - - 让你看到现在实际占用空间的是什么——你通常会发现一些你忘记了的臃肿内容。 ``` /compact ``` - - 让你在一个干净的断点压缩对话,比如当你完成一个任务开始另一个任务时,而不是等待自动版本在思维中途触发。 → 这个习惯虽小,但当会话感觉沉重时检查 /context。在任务之间压缩,而不是在一个任务中间压缩 # 第 6 部分 · 说实话 上面的五个步骤确实能减少你的账单——你刚刚学到了大多数人从未接触过的东西——仅此一点就让你领先于他们 > 有一点值得大声说出来,以便这能持续发挥作用 没有什么是可以一劳永逸的。缓存依赖于一个数字,你只有去查看才能看到——你的命中率、你的 cache_read_input_tokens。 一次设置好固然很棒——每周看一眼,这样前缀中一个游荡的时间戳就不会悄悄偷回你的节省。 一个便宜的 Agent 是一种你保持的状态,而现在保持这种状态很容易,因为你知道去哪里看 > 好消息是:你有很好的同伴。 Andrew Ng 和 Anthropic 从另一端殊途同归 - Ng 教你将 Agent 的记忆移出窗口——这就是第一步,只是从记忆的角度来看 - Anthropic 走得更远:一个在后台清理自己记忆的 Agent,这样前缀自己保持很小 > **codila@0xCodila**: [原文链接](https://x.com/0xCodila/status/2076813306128404833) > > Anthropic 和 Andrew Ng 从零开始构建了一个使用 90% 更少 Token 的 Agent: > 他们将整本《弗兰肯斯坦》书扔进了一个提示词——108,000 Token 问了一个问题 > 输入从 108,000 Token 降到了 11 > 方法如下: > 第 1 步 → 把所有从不变化的东西放进去 > >  三扇不同的门,同一个房间——停止携带所有东西,开始携带任务需要的东西——你刚刚走进来 > 工具会变:缓存、子代理、记忆存储——这些名字有一半在六个月内会不同 > 有一件事不会变:知道自己的 Agent 持有什么以及为什么的工程师,总是打败那些把所有东西都扔进窗口并寄予希望的人 现在你是前者了,所以像个工程师一样运行你的 Agent。@0xCodila ## 相关链接 - [codila](https://x.com/0xCodila) - [@0xCodila](https://x.com/0xCodila) - [15K](https://x.com/0xCodila/status/2077780537016488197/analytics) - [https://substack.com/@0xcodila](https://substack.com/@0xcodila) - [How Claude Code uses prompt caching](https://code.claude.com/docs/en/prompt-caching) - [Subagents build their own cache and context](https://code.claude.com/docs/en/prompt-caching) - [Claude Code auto memory docs](https://code.claude.com/docs/en/memory) - [Anthropic - Agent memory & "dreaming](https://youtu.be/geUv4CjPpxI?si=SdlxnX2BZFNWPKtb) - [Anthropic prompt caching docs](https://platform.claude.com/docs/en/build-with-claude/prompt-caching) - [Jul 14](https://x.com/0xCodila/status/2076813306128404833) - [524K](https://x.com/0xCodila/status/2076813306128404833/analytics) - [@0xCodila](https://x.com/@0xCodila) - [Upgrade to Premium](https://x.com/i/premium_sign_up) - [11:40 PM · Jul 16, 2026](https://x.com/0xCodila/status/2077780537016488197) - [15.1K Views](https://x.com/0xCodila/status/2077780537016488197/analytics) - [View quotes](https://x.com/0xCodila/status/2077780537016488197/quotes) --- *导出时间: 2026/7/17 15:16:01*
如 如何优化你的 AI Harness:配置、框架与子代理指南 文章指出工程师的关注点已从 IDE 转向 AI Harness,并介绍了提升 Harness 输出质量的关键方法。核心策略包括:保持配置文件(如 .md)精简,采用渐进式披露原则以节省上下文;利用 R.P.I 框架(Research, Plan, Implement)像资深工程师一样分解问题;以及通过子代理清理主上下文窗口。文章强调,Harness 的核心在于工程判断力。 技术 › 工具与效率 ✍ Alex Ker🕐 2026-04-19 AIAgentHarnessLLM工程化Prompt上下文管理子代理Claude Code最佳实践
构 构建智能代理的三层架构:Loop、Graph与Harness 本文提出解决Agent重复读取数据和Token浪费的三层架构方案:Loop负责单元工作的收集-行动-验证闭环;Graph通过分发和并行管理复杂任务;Harness作为运行时环境提供工具和上下文隔离。文中提供了具体的代码实现思路和实战演示。 技术 › Agent ✍ Archive🕐 2026-07-25 AgentLoopGraphHarness架构设计上下文管理Token优化验证机制代码实现
推 推荐这期 Pi 插件合集 文章推荐了6个实用的Pi插件,包括必装的pi-agent-extensions、多代理协作的pi-agents-team、跨会话记忆增强插件、上下文工程工具以及连接Claude Code生态的桥梁插件,旨在提升Pi作为Agent Harness的扩展能力和效率。 技术 › 工具与效率 ✍ yibie🕐 2026-07-24 PiAgent插件Multi-AgentClaude Code上下文管理
A Agent Loop 很火,但基座才是关键:从单代理到自我进化系统的 14 步路线图 文章指出,尽管循环工程备受关注,但决定代理上限的核心是其下层的“基座工程”。作者提出了从单代理到自我进化系统的 14 步路线图,涵盖基座定义、核心配置(记忆、权限、子代理)、技能封装、钩子强制执行、循环自动化、动态工作流编排,以及通过状态文件实现系统自我进化的方法。 技术 › Claude Code ✍ huangserva🕐 2026-06-18 Agent基座工程路线图Claude Code自我进化子代理技能钩子DevOps架构设计
H Hermes Harness 架构深度解析 本文深入分析了 NousResearch 的 Hermes 开源 Agent Harness 架构。文章指出 Hermes 全面实现了包括上下文管理、子代理管理、会话持久化等在内的九大核心组件。作者详细探讨了其独特优势:统一的运行时支持多种模型提供商、复杂的三层系统提示词组装、基于 SQLite 的会话即基础设施设计,以及内置的消息网关和 Cron 系统。文章最后展望了其向一级编排能力演进的方向。 技术 › Hermes ✍ Aparna Dhinakaran🕐 2026-05-30 AgentHarness架构分析LLM上下文管理开源子代理系统设计
C Claude Code 高效实战指南:5个场景掌握50个命令 文章针对用户对 Claude Code 使用停留在初级阶段的问题,通过 5 个实战场景(上下文压缩、模型切换、低成本试错、任务隔离、工作流自动化)讲解了如何组合使用 Slash 命令。文章强调通过手动压缩上下文、动态切换模型、使用分支和代理等技巧,可以实现成本减半并大幅提升编程效率。 技术 › Claude Code ✍ sitin🕐 2026-05-06 Claude CodeAgent效率提升编程技巧DevOps实战教程Token优化命令行工具工作流自动化AI编程
开 开源「洁癖.Skill」,让你的Agent越用越聪明 文章介绍了一款名为“洁癖.Skill”的开源工具,旨在解决Agent长期使用后因上下文和文档混乱导致的“越用越笨”问题。该Skill通过自动审查和迭代项目的文档、记忆及配置文件(如CLAUDE.md),确保知识库的准确性,适用于Claude Code、Codex等多个平台。作者提出“合并优于追加,删除优于保留”的原则,强调在AI协作中保持信息纯净比信息海量更重要,这能让开发者通过“存档”机制实现Agent的持续进化。 技术 › Skill ✍ 数字生命卡兹克🕐 2026-04-29 AgentClaude开源工具与效率文档管理Claude Code洁癖.Skill提示工程上下文管理自动化
A Agent 工程方法实践:Claude Code 与 OpenClaw 的深度对比 文章深入对比了 Claude Code 和 OpenClaw 两个 Agent 框架的工程实现。两者虽都采用 Agent = Model + Harness 的理念,但架构迥异:Claude Code 采用分层堆叠架构,服务于单线程深度编程场景,追求精确可控;OpenClaw 采用管道架构,面向多并发的生活场景,追求自主进化。文章从架构、循环、工具、指令、上下文、记忆等九个维度详细拆解了各自的工程设计权衡。 技术 › Harness Engineering ✍ Mr Panda🕐 2026-04-26 AgentClaude CodeOpenClaw架构设计上下文管理记忆系统工程实践
搞 搞懂缓存机制,从Gemma4到Claude Code省80%Token 本文通过本地实验与源码分析,深入解析了大模型的 KV 缓存机制。作者从 Transformer 注意力机制讲起,解释了为何多轮对话会出现 100 倍的加速差异。通过逆向 Claude Code,揭示了 Anthropic 精密的缓存工程策略(前缀匹配、TTL 机制)。最后,给出了具体的 Claude Code 使用姿势(如保护缓存前缀、避免切换模型),帮助用户将 Token 消耗降低 80%,实现同样的套餐多干 3-5 倍的活。 技术 › Claude ✍ 实践哥MinLi🕐 2026-04-20 Claude CodeKV CacheToken优化缓存机制Transformer成本优化大模型原理AI开发源码分析效率提升
使 使用 Claude Code:会话管理与 100 万上下文 本文详细介绍了 Claude Code 的会话管理策略。在 100 万 Token 上下文窗口的背景下,文章探讨了上下文衰减、压缩与清空的区别、回溯功能的优势以及子智能体的应用。文章指出,理解何时开新会话、如何有效利用 /rewind 修正错误,以及通过压缩或清空管理上下文,是提升 Claude Code 使用体验的关键。 技术 › Claude Code ✍ 宝玉🕐 2026-04-16 Claude Code上下文管理AI编程LLMAgent开发工具最佳实践
搞 搞懂缓存机制,从Gemma4到Claude Code省80%Token 文章通过本地 Gemma4 实验发现大模型对话中存在 100 倍加速的现象,深入剖析了 Transformer 的 KV 缓存原理,并逆向分析了 Claude Code 的精密缓存工程。文章解释了为何连续对话比频繁开启新 Session 更省钱,指出破坏缓存的关键行为(如切换模型、修改 System Prompt),并提供了保护缓存以节省 80% Token 的具体使用姿势。 技术 › LLM ✍ 实践哥MinLi🕐 2026-04-07 Claude Code缓存机制Token优化KV Cache成本优化Transformer源码解析ClaudeGemma提效
深 深度解析: Claude Code 51万行源码背后的5个炸裂发现 本文基于Anthropic误暴露的51万行Claude Code源码,深度解析了其背后的产品哲学。核心发现包括:通过四维记忆系统解决AI遗忘问题;Coordinator模式揭示了AI管理逻辑与人类管理的一致性;23道安全检查与YOLO分类器构建的安全体系;极致的Token缓存策略优化;以及KAIROS等未发布的隐藏功能。文章指出,Prompt和自然语言指令体系才是AI产品的真正护城河。 技术 › Claude Code ✍ 傅盛🕐 2026-04-01 Claude Code源码解析Prompt工程AI产品架构设计傅盛AgentAI记忆安全机制Token优化