Repository iconcurepo.dev
Blog
TokenEconomizer preview

microsoft / TokenEconomizer

coding-agentscost-optimizationtoken-efficiencyclaude-code

A Microsoft Research toolkit that learns from a coding agent's past sessions in a repository to cut token cost in the sessions that follow.

5.6k Stars
visibility20 Watchers
fork_right349 Forks
Python
historyUpdated recently

description README.md

TokenEconomizer: Experience-Driven Black-Box Cost Control for Coding Agents

Aaron J. Li1, Weijia Xu2, Chandan Singh2, Zixiao Chen3, Jeevana Priya Inala2, Charlotte Siska3,
Acar Tamersoy3, Bin Yu1, Ion Stoica1, Michel Galley2, Jianfeng Gao2

1UC Berkeley   2Microsoft Research   3Microsoft Security

arXiv Project page X post Python 3.10+ Apache 2.0 license

#1 Python repository of the day on Trendshift #10 Python repository of the week on Trendshift

Our measurement on SWE-Bench tasks: 18.4–30.9% lower mean cost across five agent–model combinations, cheaper in all 30 repository × combination cells, and Pass@1 up in 4 of 5. Results

An animation that opens on a title card: TokenEconomizer, experience-driven black-box cost control for coding agents, with its three mechanisms (micro-skills, tool pruning, failure-loop intervention), the agents it works with (Claude Code, Codex, GitHub Copilot CLI, Pi) and the UC Berkeley and Microsoft logos. Then a schematic of six sessions in one repository, each a bar of what it cost: the work itself plus three kinds of waste, namely re-learning the repo, tools never used, and getting stuck in loops. Each of the first three sessions teaches TokenEconomizer one thing, in order: 1 micro-skills remember how the repo works (run tests with python -m pytest), 2 tool pruning skips tools never used here (Read and Edit kept, Notebook and Browser left out), and 3 failure-loop intervention spots loops and steps in (the same error three times meets a nudge to try another way). In the session after each lesson that waste shrinks, so the bars step down below a dashed line marking the cost without TokenEconomizer. It ends on results measured on SWE-Bench tasks: 18.4–30.9% lower mean cost across five agent–model combinations, cheaper than default in 30 of 30 repository × combination cells, and Pass@1 up in 4 of 5; per combination, cost 23.6% lower and Pass@1 +1.3 for Claude Code with Sonnet 5, 30.9% and −1.0 with Opus 4.8, 24.7% and +0.9 for Codex with GPT-5.6 Terra, 26.1% and +1.6 with GPT-5.6 Sol, and 18.4% and +2.6 for Copilot CLI with Sonnet 5.

TokenEconomizer makes coding agents cheaper to run in the repositories you work in often, without changing the agent itself. It learns from the sessions an agent has already run in a repository, and uses that experience to cut waste in the sessions that follow. It has three mechanisms:

  • Micro-skills: remember how the repository works. Sessions keep rediscovering the same facts: a build step that must run first, the spelling of a command that works here (python -m pytest where pytest is missing, npx tsc where tsc is), an environment variable a command needs, where generated files live. Micro-skills learns such facts, in any language, from two sources: failures that repeat across sessions, and a model that reviews the recent sessions every k sessions (k = 3) and proposes facts. A fact is stated at the start of each session once three sessions have confirmed it, and dropped when later sessions contradict it.
  • Tool pruning: stop loading tools that go unused. Every loaded tool's definition is sent with every request. A tool that goes unused over p prompts spread across at least m sessions, or over s sessions of any length (p = 10, m = 2, s = 5), is left out of the next sessions through the agent's own settings. One command brings it back.
  • Failure-loop intervention (the loop guard): step in when a turn is stuck. When a turn (the agent's work on one prompt) keeps hitting the same failures, a judge model checks whether it is stuck. If the judge is at least c sure (c = 0.7), the agent gets a redirect (a short note, added to its context, to try another approach), at most r times per turn (r = 1). If the turn is still stuck after that, TokenEconomizer tells you how to stop it; it never stops a turn itself.

TokenEconomizer is a plugin for Claude Code, Codex, the GitHub Copilot CLI and Pi, with experimental support for opencode, Cursor Agent, Grok Build, Hermes Agent and the Antigravity CLI. Each italic letter above is a setting, shown with its default (Settings), and each mechanism can be turned on or off. The plugin announces each action in one line, and if any part of it fails, your session carries on as if it were not installed. Its model calls (the fact proposer, and the judge with the redirect it writes) run through your own agent, on the same plan or login, and their cost is included in its reports. What it learns stays on your machine, in ~/.tokeneconomizer.

Results

The paper ran five agent–model combinations on the same 277 SWE-Bench tasks from six repositories. Each task ran as its own session, and each repository's tasks ran in order, starting with nothing learned. Costs include every model call TokenEconomizer made. Within each repository and combination, cost fell by 5–40%. The savings held on Web-Bench (16–21% lower) and on two security-auditing benchmarks where every task is in a new repository (6–20% lower). The project page has interactive charts, per-repository numbers and nine annotated runs, and experiments/ reruns the SWE-Bench and Web-Bench experiments.

How it works

TokenEconomizer: the plugin keeps what it knows about a repository and applies micro-skills, tool pruning and failure-loop intervention to your unchanged coding agent; each finished session's log updates all three

Coding agents are built to finish tasks, not to keep them cheap. Every turn pays again for the agent's instructions, every loaded tool, and the conversation so far. Developers use the same agent on the same repository again and again, so much of that cost repeats: the same facts rediscovered, the same unused tools sent, the same kinds of failing loops. TokenEconomizer leaves the agent's model, weights and code alone and learns across a repository's sessions instead of treating each one on its own, so whatever the plugin spends is paid back over the sessions that follow. Four principles guide the mechanisms:

  • Work through what the agent already reads. Facts enter the context at session start, and tools are removed through the agent's own settings, so nothing depends on the agent learning a new tool.
  • Count cost as it is billed. Most tokens in a sessio