AIsaAIsaStudio English 打开 AIsa Studio

TokenEconomizer 开源插件:让 AI 编码 agent 从历史会话学习以降低 token 成本

原标题:TokenEconomizer: Learning from Experience to Make AI Coding Agents More Efficient

UC Berkeley、Microsoft Research 与 Microsoft Security 发布开源插件 TokenEconomizer,通过学习 agent 在同一仓库的历史会话来降低后续会话成本,不修改 agent 本身和底层模型。在 277 个 SWE-Bench 真实修 bug 任务、五种 agent 与模型组合上,平均成本下降 18.4–30.9%,任务成功率基本保持稳定;在 Web-Bench 上省 16.0–21.2%,安全审计场景省 6.4–19.6%。插件通过微技能笔记、工具裁剪和失败循环干预三种机制工作,学习数据保留在开发者本机。

为什么值得看

原文给出三种减耗机制的实测数据和开箱条件,读者可以据此判断它能否叠加到现有编码 agent 工作流上。

阅读原文 ↗11月17日 AI 日报

本页的摘要、推荐理由与解读出自 AIsa Studio 的 AI 风向智能体,原文版权归原作者所有。

深度解读

发生了什么

微软研究院联合 UC Berkeley 与 Microsoft Security 开源了 TokenEconomizer,一个让 AI 编码 agent 从同一仓库的历史会话中学习、从而降低后续会话 token 成本的插件。其背后的论文题为「TokenEconomizer: Experience-Driven Black-Box Cost Control for Coding Agents」。

该插件不修改 agent 本身及其底层模型,只通过 agent 已有的接口介入,可与现有编码 agent 并行使用。

关键细节

研究首先对 token 的去向做了剖析:在 SWE-Bench 的 60 个任务上运行 Claude Code,发现三类反复出现的浪费:

  • 重复发现同样的事实:仓库累计至少完成五个任务后,30% 的工具调用重复了该仓库此前任务中的操作,而与其他仓库历史匹配的只有 7%。
  • 为未使用的工具付费:agent 每轮收到 23 个工具描述,其中 16 个从未被调用。
  • 卡在失败上:失败的 17% 任务花费了 32% 的成本。

TokenEconomizer 通过三个机制应对,每次会话结束后更新:

  • 微技能(micro-skills):以一行笔记形式沉淀仓库特定知识,需多次会话确认模式后才加入 agent 的起始上下文,若后续证据矛盾则撤销。
  • 工具裁剪:移除历史表明不太可能用到的工具,但始终保留运行命令和编辑文件的核心工具,需要时可恢复。
  • 失败循环干预:由 judge 模型判断反复失败的 agent 是真卡住还是仍在推进;若卡住则给出改换思路的简短提示,开源插件中若仍卡住会提醒开发者,而不是直接停止工作。

评估覆盖六个仓库的 277 个 SWE-Bench 任务、五种 agent–模型组合,平均成本下降 18.4–30.9%,全部 30 个仓库–组合组合中均实现节省,任务成功率大体稳定(五种组合中四种 Pass@1 上升)。作为对照,降低模型推理力度可省相近或更多成本,但性能明显下降。累计成本在每个仓库到第六个任务时都低于默认值。节省也迁移到了顺序网页开发(Web-Bench 上降低 16.0–21.2%)和安全审计(每个任务都是新仓库,成本降低 6.4–19.6%)。报告的节省已计入 TokenEconomizer 自身模型调用的开销。

为什么重要

核心洞见是成本控制需要适应仓库本身,而非到处套用固定策略——团队测试的三个固定策略开源插件,都只在部分仓库有效。仓库级经验能解决单任务优化看不到的浪费:开发者日复一日回到同一代码库,而 agent 每次会话都从零开始。

黑盒设计有实际意义:插件不需要自己的 API key,学习到的内容保留在开发者本地机器上,可以搭配团队已在用的任意 agent 运行。

对谁有影响

在长期维护的仓库中使用 AI 编码 agent 的开发者和团队受益最大,因为节省依赖同一代码库中积累的会话历史。研究者则获得一个开源起点,用于研究经验驱动的 agent 效率问题。安全审计的结果对任务经常跨越大量不同仓库的从业者值得一提——单任务节省较小但依然为正。

全文翻译

六次编码会话中不断缩减的成本柱状图将日志发送至 TokenEconomizer,后者利用微技能(micro-skills)、工具剪枝和失败循环干预,引导一个未作修改的编码代理进入下一次会话。

概览

  • 开发者日复一日地在相同的代码库中使用 AI 编码代理,但代理往往每次会话都从零开始:重新发现熟悉的事实、加载未使用的工具,并重复失败的尝试。
  • TokenEconomizer 是一个开源插件,它通过从代理在代码库中的过往会话中学习,来减少未来会话中的浪费,且无需修改代理或其底层模型。
  • 在 277 个真实的 bug 修复任务中,TokenEconomizer 使多种 AI 编码代理的成本降低了 18.4%–30.9%。在所有测试的代码库中成本均有所下降,而任务成功率保持基本稳定。

AI 编码代理正越来越多地帮助开发者独立修复 bug 和构建功能,但与模型的每一次交互都会消耗 令牌(tokens)。在每次会话中,代理会向其模型发送指令、工具描述和会话历史。大多数降低成本的尝试都集中在单个任务上。然而,开发者会反复回到同一个代码库,这为代理提供了从以往工作中学习的机会。如果没有这种学习,代理可能会反复重新发现如何运行测试、加载从未使用过的工具,或者花费数十个回合去重蹈覆辙。

在我们的论文 TokenEconomizer: Experience-Driven Black-Box Cost Control for Coding Agents 中,我们提出了一个简单的问题:代码库中的经验能否帮助代理更高效地解决未来的任务?由加州大学伯克利分校、微软研究院和微软安全部门共同开发的开源插件 TokenEconomizer,将已完成的会话转化为后续会话的指导。它在不改变代理或其底层模型的情况下减少浪费,因此开发者可以将其与自己已经依赖的编码代理配合使用。

每个完成的会话教会 TokenEconomizer 关于代码库的一些知识,因此后续会话消耗的令牌更少。

图 1. 每个完成的会话教会 TokenEconomizer 关于代码库的一些知识,因此后续会话消耗的令牌更少。

令牌的流向

我们首先对 SWE-Bench(一个针对开源 Python 项目中真实 bug 修复的基准测试)上的 60 个任务中的 Claude Code 进行了分析,按顺序运行每个代码库的任务。出现了三种 recurring 的浪费现象:

  • 重新发现相同的事实。在一个代码库积累了至少五个已完成的任务后,30% 的工具调用重复了该代码库中早期任务的某个动作。只有 7% 的动作匹配了其他代码库的历史记录。
  • 为未使用的工具付费。代理在每个回合都收到了 23 个工具描述,但其中 16 个从未被调用。
  • 陷入失败循环。占 17% 的失败任务却占据了 32% 的支出。

我们还测试了三个流行的开源插件,它们各自围绕固定的节省成本策略构建。每个插件在某些代码库中降低了成本,但在其他代码库中则没有效果。这些发现共同表明,有效的成本控制必须适应特定的代码库,而不是在所有地方应用同一种策略。

Microsoft Research StorY

抽象的青色调图像,俯视视角下的铝罐,上面叠加了一个由细线连接的流程图形状网络(矩形、圆角节点和菱形)。

OptiMind:当系统触及车间

中西部一家装瓶厂的三个月试点项目展示了当 AI 超越聊天并进入决策领域时会发生什么——在那里约束条件不断变化,风险真实存在,答案必须经得起考验。

TokenEconomizer 的工作原理

每次会话结束后,TokenEconomizer 会读取会话日志并更新其对代码库的认知。然后,它通过以下三种机制发挥作用,仅使用代理已提供的接口,如图 2 所示。

微技能(Micro-skills) 以一行笔记的形式捕获特定于代码库的知识。TokenEconomizer 识别跨会话的重复模式,并提出相应的笔记。只有在经过几个会话确认该模式后,它们才会被添加到代理的初始上下文中;如果后续证据与之矛盾,则会撤消该笔记。

工具剪枝(Tool pruning) 移除那些根据代码库历史记录表明代理不太可能使用的工具,同时始终保留用于运行命令和编辑文件的核心工具。如果需要,可以恢复被移除的工具。

失败循环干预(Failure-loop intervention) 监控那些不断遭遇相同失败的代理。一个裁判模型(judge model)决定代理是真正卡住了还是仍在取得进展。如果确实卡住,代理会收到一条简短的笔记,建议采取不同的方法。在该开源插件中,如果代理在收到笔记后仍然卡住,TokenEconomizer 会通知开发者,而不是自行停止工作。

TokenEconomizer 与未作修改的编码代理协同工作。微技能添加学到的事实,工具剪枝精简加载的工具,失败循环干预重定向卡住的工作。每个完成的会话都会更新这三项内容。

图 2. TokenEconomizer 与未作修改的编码代理协同工作。微技能添加学到的事实,工具剪枝精简加载的工具,失败循环干预重定向卡住的工作。每个完成的会话都会更新这三项内容。

控制器旨在随着新证据的出现而修订其决策。报告的节省金额也包含了 TokenEconomizer 自身模型调用的成本。

在每个代码库中节省成本

我们在来自六个代码库的 277 个 SWE-Bench 任务上评估了 TokenEconomizer,使用了五种不同的 AI 编码代理和模型组合。对于每个代码库,TokenEconomizer 开始时没有任何先前的会话历史记录。

如图 3 所示,TokenEconomizer 将平均成本降低了 18.4%–30.9%,并在所有 30 个“代码库-组合”配对中实现了最大程度的节省,且没有性能损失。相比之下,降低模型的推理努力虽然节省了相似或更多的费用,但导致了显著的性能下降。

在 SWE-Bench 上,TokenEconomizer 使五种代理-模型组合的平均每项任务成本降低了 18.4%–30.9%(左图),而在其中四种组合中 Pass@1 有所提升(右图)。

图 3. 在 SWE-Bench 上,TokenEconomizer 使五种代理-模型组合的平均每项任务成本降低了 18.4%–30.9%(左图),而在其中四种组合中 Pass@1 有所提升(右图)。

随着经验的积累,节省幅度也会增加。早期任务可能会因为 TokenEconomizer 的学习过程而成本更高,但到第六个任务时,每个代码库的累计成本都低于默认值,如图 4 所示。每种机制都能单独带来节省,而结合使用所有三种机制则能实现最大的降幅。

相对于默认值,每个代码库任务过程中的累计成本变化,使用 Claude Code。从没有历史记录开始,到第六个任务时,每个代码库的成本都变得更低。

图 4. 相对于默认值,每个代码库任务过程中的累计成本变化,使用 Claude Code。从没有历史记录开始,到第六个任务时,每个代码库的成本都变得更低。

节省效果也延续到了顺序式 Web 开发中,Web-Bench 上的成本降低了 16.0%–21.2%。在安全审计方面(每个任务都在一个新的代码库中),成本降低了 6.4%–19.6%。

展望未来

TokenEconomizer 开辟了一条路径,使编码代理不仅能学习如何解决任务,还能学习如何更有效地分配计算资源。其轻量级的控制器展示了经验如何在无需更改底层代理或模型的情况下提高效率。

该开源插件使开发人员和研究者能够轻松利用这些方向。它设计为与现有的 AI 编码代理并行运行,无需修改其底层模型,不需要自己的 API 密钥,并且将其所学内容保留在开发者的机器上。我们邀请社区将其作为起点,探索随着经验积累而变得更具资源的代理,使其工作方式以及所掌握的知识都能不断适应进化。

查看原文

Six coding sessions with shrinking cost bars send their logs to TokenEconomizer, which uses micro-skills, tool pruning and failure-loop intervention to guide an unchanged coding agent through the next session.

At a glance

  • Developers use AI coding agents in the same codebases day after day, yet agents often start each session from scratch: rediscovering familiar facts, loading unused tools, and repeating unsuccessful approaches.
  • TokenEconomizer is an open-source plugin that learns from an agent’s past sessions in a repository to reduce waste in future sessions, without modifying the agent or its underlying model.
  • Across 277 real bug-fixing tasks, TokenEconomizer reduced costs by 18.4–30.9% across multiple AI coding agents. Costs fell in every repository tested, while task success rates remained broadly stable.

AI coding agents are increasingly helping developers fix bugs and build features largely on their own, but every interaction with their models consumes tokens. In each session, an agent sends instructions, tool descriptions, and session history to its model. Most efforts to reduce this cost focus on individual tasks. Yet developers return to the same repositories, giving agents an opportunity to learn from previous work. Without that learning, an agent may repeatedly rediscover how to run tests, load tools it never uses, or spend dozens of turns revisiting the same error.

In our paper, TokenEconomizer: Experience-Driven Black-Box Cost Control for Coding Agents, we ask a simple question: can experience in a repository help an agent solve future tasks more efficiently? TokenEconomizer, an open-source plugin from UC Berkeley, Microsoft Research, and Microsoft Security, turns completed sessions into guidance for subsequent ones. It reduces waste without changing the agent or its underlying model, so developers can use it alongside the coding agents they already rely on.

Each finished session teaches TokenEconomizer something about the repository, so later sessions cost less tokens.

Figure 1. Each finished session teaches TokenEconomizer something about the repository, so later sessions cost less tokens.

Where the tokens go

We first profiled Claude Code on 60 tasks from SWE-Bench, a benchmark of real bug fixes in open-source Python projects, running each repository’s tasks in sequence. Three kinds of waste recurred:

  • Rediscovering the same facts. After a repository had accumulated at least five completed tasks, 30% of tool calls repeated an action from an earlier task in that repository. Only 7% matched an action in another repository’s history.
  • Paying for unused tools. The agent received 23 tool descriptions on every turn, but 16 were never called.
  • Getting stuck on failures. The 17% of tasks that failed accounted for 32% of the spending.

We also tested three popular open-source plugins, each built around a fixed cost-saving strategy. Each lowered costs in some repositories but not others. Together, these findings suggest that effective cost control must adapt to the repository, rather than apply the same strategy everywhere.

Microsoft Research StorY

Abstract teal-toned image of aluminum cans viewed from above, overlaid with a network of connected flowchart shapes (rectangles, rounded nodes, and diamonds) linked by thin line.

OptiMind: When the system meets the floor

A three‑month pilot in a Midwestern bottling plant shows what happens when AI moves beyond chat and into decision-making, where constraints shift, stakes are real, and answers must hold.

How TokenEconomizer works

After each session, TokenEconomizer reads the session’s log and updates what it knows about the repository. It then acts through three mechanisms, using only interfaces the agent already provides, as illustrated in Figure 2.

Micro-skills capture repository-specific knowledge in one-line notes. TokenEconomizer identifies recurring patterns across sessions and proposes corresponding notes. They are added to the agent’s starting context only after several sessions have confirmed the pattern, and retires the note if later evidence contradicts it.

Tool pruning removes tools that repository history suggests the agent is unlikely to use, while always retaining core tools for running commands and editing files. Removed tools can be restored if needed.

Failure-loop intervention watches for an agent that keeps hitting the same failure. A judge model decides whether the agent is truly stuck or still making progress. If it is stuck, the agent gets a short note suggesting a different approach. In the open-source plugin, if the agent is still stuck after the note, TokenEconomizer alerts the developer rather than stopping the work itself.

TokenEconomizer works alongside an unchanged coding agent. Micro-skills add learned facts, tool pruning trims the loaded tools, and failure-loop intervention redirects stuck work. Each finished session updates all three.

Figure 2. TokenEconomizer works alongside an unchanged coding agent. Micro-skills add learned facts, tool pruning trims the loaded tools, and failure-loop intervention redirects stuck work. Each finished session updates all three.

The controller is designed to revise its decisions as new evidence arrives. Reported savings also account for the cost of TokenEconomizer’s own model calls.

Saving costs in every repository

We evaluated TokenEconomizer on 277 SWE-Bench tasks from six repositories, using five different AI coding agent and model combinations. For each repository, TokenEconomizer started with no prior session history.

TokenEconomizer lowered the average cost by 18.4–30.9%, as shown in Figure 3, and saved the most in all 30 repository-combination pairs without performance loss. By contrast, lowering the model’s reasoning effort saved similar or larger amounts but led to substantial performance degradation.

On SWE-Bench, TokenEconomizer lowered mean cost per task by 18.4–30.9% across five agent–model combinations (left), while Pass@1 rose in four of them (right).

Figure 3. On SWE-Bench, TokenEconomizer lowered mean cost per task by 18.4–30.9% across five agent–model combinations (left), while Pass@1 rose in four of them (right).

Savings grow as experience accumulates. Early tasks can cost more while TokenEconomizer learns, but cumulative costs fell below the default in every repository by the sixth task, shown inFigure 4. Each mechanism contributes savings on its own, and combining all three delivers the largest reduction.

Cumulative cost change relative to default over each repository’s tasks, using Claude Code. Starting with no history, every repository becomes cheaper by the sixth task.

Figure 4. Cumulative cost change relative to default over each repository’s tasks, using Claude Code. Starting with no history, every repository becomes cheaper by the sixth task.

The savings also carried over to sequential web development, with costs 16.0–21.2% lower on Web-Bench. On security auditing, where every task is in a new repository, costs were 6.4–19.6% lower.

Looking ahead

TokenEconomizer opens a path toward coding agents that learn not only how to solve tasks, but also how to spend computation more effectively. Its lightweight controller shows how experience can improve efficiency without changing the underlying agent or model.

The open-source plugin makes these directions accessible to developers and researchers. It is designed to run alongside existing AI coding agents without modifying their underlying models, requires no API key of its own, and keeps what it learns on the developer’s machine. We invite the community to use it as a starting point for exploring agents that become more resourceful with experience, adapting how they work, as well as what they know.

原文版权归 Microsoft Research 及原作者所有,本站转载仅供阅读。如需删除,请联系 support@aisa.one。