TokenEconomizer:让 AI 编程智能体更高效

AI 编程智能体正越来越多地帮助开发者修复 bug、构建功能,且在很大程度上可自主完成,但它们与其模型的每一次交互都会消耗 Token。在每次会话中,智能体会向模型发送指令、工具描述和会话历史。大多数降低这一成本的努力都聚焦于单个任务。然而开发者会反复回到相同的代码仓库,这就给了智能体一个从先前工作中学习的机会。若缺乏这种学习,智能体可能会反复重新摸索如何运行测试、加载它根本用不到的工具,或者花上数十轮往返重复面对同一个错误。
在我们的论文《TokenEconomizer: Experience-Driven Black-Box Cost Control for Coding Agents》中,我们提出了一个简单的问题:在一个代码仓库中积累的经验,能否帮助智能体更高效地解决未来的任务?TokenEconomizer 是来自 UC Berkeley、Microsoft Research 和 Microsoft Security 的开源插件,它把已完成的会话转化为后续会话的指引。它无需改动智能体或其底层模型即可减少浪费,因此开发者可以将其与已在使用的编程智能体搭配使用。
我们首先在 SWE-Bench 的 60 项任务上对 Claude Code 做了性能剖析,SWE-Bench 是一个针对开源 Python 项目中真实 bug 修复的基准测试,我们按顺序运行每个代码仓库的任务。结果发现有三类浪费反复出现:
我们还测试了三款流行的开源插件,每款都围绕一种固定的成本节省策略构建。它们各自在一些代码仓库中降低了成本,但在另一些仓库中却没有。综合来看,这些发现表明,有效的成本控制必须因仓库而异,而不是在所有地方套用同一种策略。
学术会议
学术界与工业界的参会者提交研究与经验论文,内容涵盖计算机系统软件理论与实践的各个方面。
每次会话结束后,TokenEconomizer 会读取该会话日志,并更新它对代码仓库的了解。随后,它仅使用智能体已提供的接口,通过三种机制发挥作用,如图 2 所示。
微技能以单行笔记的形式记录仓库专属知识。TokenEconomizer 会识别跨会话反复出现的模式,并提出相应的笔记。只有在多次会话确认该模式后,这些笔记才会被加入智能体的起始上下文;若后续证据与之相矛盾,则会将其退役。
工具剪枝会移除仓库历史显示该智能体不太可能使用的工具,同时始终保留用于运行命令和编辑文件的核心工具。被移除的工具如有需要可以恢复。
失败循环干预会监测持续遭遇同一失败的智能体。由评判模型判断该智能体是真的陷入困境,还是仍在取得进展。如果确实卡住,智能体会收到一条简短提示,建议换一种方法。在开源插件中,如果智能体收到提示后仍然卡住,TokenEconomizer 会提醒开发者,而不会自行中止工作。
该控制器的设计使其能够随着新证据的出现修正自身决策。所报告的节省量也已计入 TokenEconomizer 自身模型调用所产生的成本。
我们在来自六个仓库的 277 项 SWE-Bench 任务上评估了 TokenEconomizer,使用了五种不同的 AI 编程智能体与模型组合。对于每个仓库,TokenEconomizer 均在没有任何先前会话历史的情况下启动。
如图 3 所示,TokenEconomizer 将平均成本降低了 18.4–30.9%,并在全部 30 个仓库—组合配对中都实现最大节省,且性能没有下降。相比之下,降低模型的推理投入虽能节省相近甚至更多的成本,却会导致性能大幅下降。
随着经验不断积累,节省的成本也会增加。在 TokenEconomizer 学习的过程中,早期任务的开销可能更高,但如图 4 所示,到第六个任务时,每个仓库的累计成本都已降至默认水平以下。每个机制本身都能带来成本节省,而三者结合使用带来的降幅最大。
这种节省效果也延续到了顺序式 Web 开发场景中,在 Web-Bench 上成本降低了 16.0–21.2%。在安全审计场景中,由于每项任务都处于新的仓库,成本降低了 6.4–19.6%。
TokenEconomizer 为编码智能体开辟了一条路径:不仅能学会如何解决任务,还能学会如何更有效地使用算力。其轻量级控制器表明,无需改动底层智能体或模型,经验就能提升效率。
这款开源插件让开发者和研究人员能够接触到这些方向。它的设计目标是与现有 AI 编码智能体并行运行,无需修改后者的底层模型,自身也不需要 API key,并将学习到的内容保留在开发者本机。我们邀请社区以它为起点,探索那些随着经验积累而变得更具资源调度能力、既能调整做事方式也能改变自身知识的智能体。
文章《TokenEconomizer:从经验中学习,让 AI 编码智能体更高效》最初发布于 Microsoft Research。
译文由英文原文机器翻译,术语按行业惯例统一,可在文末展开原文核对。
查看英文原文(TokenEconomizer: Learning from Experience to Make AI Coding Agents More Efficient)

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 help 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.

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.
ACADEMIC CONFERENCE
Microsoft at SOSP 2026
Academic and industrial participants present research and experience papers that cover the full range of theory and practice of computer systems software.
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.

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.

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.

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.
The post TokenEconomizer: Learning from Experience to Make AI Coding Agents More Efficient appeared first on Microsoft Research.
正文保留英文原文,标题与摘要由机器翻译。
原文发布于 Microsoft Research(TokenEconomizer: Learning from Experience to Make AI Coding Agents More Efficient),标题、摘要与正文由机器翻译,术语按行业惯例统一,未经人工逐句校订。