微软开源TokenEconomizer:AI编程Agent从经验中学习降本
TokenEconomizer: Learning from Experience to Make AI Coding Agents More Efficient
做AI Coding Agent的同学必看,这是首个不改动模型本体、仅靠插件就能显著降低Token成本的工程实践,实测降本超30%且不影响成功率,极具复用价值。
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 编程代理,但代理往往每次会话都从零开始:重新发现已知事实、加载未使用的工具,并重复失败的尝试。
- TokenEconomizer 是一个开源插件,它通过学习代理在代码库中过去会话的经验,以减少未来会话中的浪费,且无需修改代理或其底层模型。
- 在 277 个真实的 bug 修复任务中,TokenEconomizer 使多种 AI 编程代理的成本降低了 18.4%–30.9%。在所有测试的代码库中成本均有所下降,而任务成功率总体保持稳定。
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.
AI 编程代理正越来越多地帮助开发者独立修复 bug 和构建功能,但与模型的每次交互都会消耗 token。在每次会话中,代理会向模型发送指令、工具描述和会话历史。大多数降低成本的尝试都聚焦于单个任务。然而,开发者会反复回到同一个代码库,这为代理提供了从先前工作中学习的机会。如果没有这种学习,代理可能会反复重新发现如何运行测试、加载从未使用过的工具,或花费数十轮对话来回顾同一个错误。
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.
在我们的论文《TokenEconomizer: Experience-Driven Black-Box Cost Control for Coding Agents》中,我们提出了一个简单的问题:代码库中的经验能否帮助代理更高效地解决未来任务?TokenEconomizer 是加州大学伯克利分校、微软研究院和微软安全部门联合推出的开源插件,它将已完成的会话转化为后续会话的指导。它在不改变代理或其底层模型的情况下减少浪费,因此开发者可以将其与自己依赖的编程代理配合使用。
Figure 1. Each finished session teaches TokenEconomizer something about the repository, so later sessions cost less tokens.
图 1. 每个已完成的会话都为 TokenEconomizer 提供关于该代码库的信息,从而使后续会话消耗的 token 更少。
Where the tokens go
token 的去向
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:
我们首先在 SWE-Bench(一个针对开源 Python 项目中真实 bug 修复的基准测试)的 60 个任务上对 Claude Code 进行了性能分析,按顺序运行每个代码库的任务。出现了三种反复出现的浪费现象:
- 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.
- 重新发现相同的事实。在一个代码库积累了至少五个已完成任务后,30% 的工具调用重复了该代码库中早期任务的某个操作。只有 7% 的操作匹配了其他代码库历史记录中的行为。
- 为未使用的工具付费。代理每轮都会收到 23 个工具描述,但其中 16 个从未被调用。
- 陷入失败困境。占 17% 的失败任务却占据了 32% 的花费。
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.
我们还测试了三个流行的开源插件,每个插件都围绕一种固定的成本节约策略构建。每个插件在某些代码库中降低了成本,但在其他代码库中则没有。综合这些发现表明,有效的成本控制必须适应具体的代码库,而不是在所有地方应用相同的策略。
PODCAST SERIES
播客系列
AI Testing and Evaluation: Learnings from Science and Industry
AI 测试与评估:来自科学与行业的经验教训
Discover how Microsoft is learning from other domains to advance evaluation and testing as a pillar of AI governance.
了解微软如何从其他领域汲取经验,将评估和测试作为 AI 治理的支柱加以推进。
Listen now
立即收听
Opens in a new tab
在新标签页中打开
How TokenEconomizer works
TokenEconomizer 的工作原理
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.
每次会话结束后,TokenEconomizer 会读取该会话的日志,并更新其对代码库的了解。随后,它通过三种机制发挥作用,仅使用代理(agent)已提供的接口,如图 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.
微技能(Micro-skills)以一行笔记的形式捕获特定于代码库的知识。TokenEconomizer 识别跨会话的重复模式,并提出相应的笔记。只有在多个会话确认该模式后,这些笔记才会被添加到代理的初始上下文中;如果后续证据与之矛盾,则会撤销该笔记。
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.
工具剪枝(Tool pruning)会移除那些根据代码库历史记录判断代理不太可能使用的工具,同时始终保留用于运行命令和编辑文件的核心工具。如果需要,可以恢复被移除的工具。
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.
失败循环干预(Failure-loop intervention)监控持续遭遇相同失败的代理。一个评判模型(judge model)决定代理是真正陷入僵局还是仍在取得进展。如果代理陷入僵局,它会收到一条简短的笔记,建议采取不同的方法。在开源插件中,如果代理在收到笔记后仍然陷入僵局,TokenEconomizer 会通知开发人员,而不是自行停止工作。
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.
图 2. TokenEconomizer 与未更改的编码代理协同工作。微技能添加学到的事实,工具剪枝减少加载的工具,失败循环干预重定向陷入僵局的工作。每个完成的会话都会更新这三项内容。
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.
控制器旨在随着新证据的出现而修订其决策。报告的节省成本也考虑了 TokenEconomizer 自身模型调用的成本。
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.
我们在六个代码库的 277 个 SWE-Bench 任务上评估了 TokenEconomizer,使用了五种不同的 AI 编码代理和模型组合。对于每个代码库,TokenEconomizer 开始时没有任何先前的会话历史记录。
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.
如图3所示,TokenEconomizer将平均成本降低了18.4–30.9%,在所有30个仓库-组合对中实现了最大节省,且未造成性能损失。相比之下,降低模型的推理努力程度虽然节省了相似或更多的费用,但导致了严重的性能下降。
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).
图3。在SWE-Bench上,TokenEconomizer在五种智能体-模型组合中(左图)将每项任务的平均成本降低了18.4–30.9%,其中四种组合的Pass@1有所提升(右图)。
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.
随着经验的积累,节省效果日益显著。在TokenEconomizer学习期间,早期任务的成本可能更高,但从第六个任务开始,每个仓库的累计成本均低于默认值,如图4所示。每种机制单独都能带来节省,而结合使用全部三种机制则能实现最大的成本降低。
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.
图4。使用Claude Code时,相对于默认设置,各仓库任务累计成本的变化情况。从没有历史记录开始,到第六个任务时,所有仓库的成本都变得更低。
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.
这种节省也延续到了顺序式Web开发中,Web-Bench上的成本降低了16.0–21.2%。在安全审计方面,由于每个任务都在新仓库中进行,成本降低了6.4–19.6%。
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.
TokenEconomizer为编码智能体开辟了一条新路径,使其不仅能学习如何解决问题,还能学习如何更有效地分配计算资源。其轻量级控制器展示了经验如何在无需更改底层智能体或模型的情况下提升效率。
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.
该开源插件使这些方向对开发者和研究人员变得触手可及。它设计用于与现有的AI编码智能体并行运行,无需修改其底层模型,不需要自己的API密钥,并将所学内容保留在开发者的机器上。我们邀请社区将其作为起点,探索那些随经验增长而变得更加足智多谋的智能体,适应其工作方式以及所掌握的知识。
Read the paper
阅读论文
Access the code
访问代码
View the project
查看项目
Opens in a new tab
在新标签页中打开
更进一步:量化金融体系
看懂新闻只是起点——沿量化金融路径,把它变成能交付的工程能力