
Experience-Driven Black-Box Cost Control for Coding Agents
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
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
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:
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.TokenEconomizer is a plugin for Claude Code, Codex, the GitHub Copilot CLI and Pi,
with experimental support for opencode. 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.
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.
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:
You need one of these agents, signed in any way it supports, and Python 3.10 or newer on
PATH. The plugin needs no API key of its own and never handles credentials, so any plan or
login works.
Run these commands in the folder where you want to keep the clone. Claude Code, Codex, the Copilot CLI and opencode run the plugin from the clone, so leave it in place. Then start the agent in one of your repositories.
Claude Code
git clone https://github.com/microsoft/TokenEconomizer.git
claude plugin marketplace add "$PWD/TokenEconomizer"
claude plugin install tokeneconomizer@tokeneconomizerStart claude, trust the folder (Yes, I trust this folder), and run
/tokeneconomizer:status, allowing its script if asked. Once per machine, and again after Claude
Code updates, run /tokeneconomizer:toolprune refresh-catalog.
Codex
git clone https://github.com/microsoft/TokenEconomizer.git
codex plugin marketplace add "$PWD/TokenEconomizer"
codex plugin add tokeneconomizer@tokeneconomizerStart codex, choose Trust and continue for the folder, then at Hooks need review
choose Trust all and continue. The hooks take effect from your next Codex session, so quit
(/exit), start codex again, and check with $tokeneconomizer:status.
Copilot CLI
git clone https://github.com/microsoft/TokenEconomizer.git
copilot plugin marketplace add "$PWD/TokenEconomizer"
copilot plugin install tokeneconomizer@tokeneconomizerStart copilot, choose Yes, and remember this folder for future sessions, and run
/tokeneconomizer:status, choosing Yes to run its script. Once per machine, and again after
Copilot updates, run /tokeneconomizer:toolprune refresh-catalog.
Pi
pi install git:github.com/microsoft/TokenEconomizerStart pi and run /te status.
opencode (experimental)
git clone https://github.com/microsoft/TokenEconomizer.git
mkdir -p ~/.config/opencode/plugins
ln -s "$PWD/TokenEconomizer/plugin/opencode/tokeneconomizer.ts" ~/.config/opencode/plugins/Start opencode and send a prompt, then check in the repository with
~/.tokeneconomizer/bin/te --host opencode status. opencode has no plugin commands, so the
plugin's commands run from a terminal.
If the install worked, the status command prints a short status block. refresh-catalog
records every tool a session loads (without a model call), so that tool pruning can consider all
of them. Keep the marketplace path absolute, as above: the Copilot CLI reads a relative path such
as ./TokenEconomizer as the name of a GitHub repository.
To update:
| Agent | Update |
|---|---|
| Claude Code, Copilot CLI, opencode | git pull in the clone |
| Codex | git pull in the clone, then the codex plugin add line again |
| Pi | pi update --extensions |
plugin/README.md walks through each agent in more detail, including
uninstalling, using Copilot with your own model key, and troubleshooting.
| Agent | Supported | Older versions |
|---|---|---|
| Claude Code | 2.1.300 to 2.1.310 | not supported |
| Codex | 0.153.1 to 0.164.0 | not supported |
| Copilot CLI | 1.0.83 to 1.0.102 | not supported |
| Pi | 1.2.0 to 1.3.0 | 0.99.x: load the extension by path (below) |
| opencode (experimental) | 1.19.5 and 2.2.0 | not tested |
The commands above were tested on the oldest and newest version in each range; newer versions have not been tested yet.
pi install, clone the repository and start each session with
pi --extension /path/to/TokenEconomizer/plugin/pi/tokeneconomizer.ts, using your clone's
full path; git pull in the clone updates it.Work as usual; the plugin needs nothing from you. To see or change what it does, use its commands. They have the same names on every agent; only the way you type them differs:
| Agent | Type a command as |
|---|---|
| Claude Code, Copilot CLI | /tokeneconomizer: |
| Codex | $tokeneconomizer: |
| Pi | /te |
| opencode | from a terminal: ~/.tokeneconomizer/bin/te --host opencode |
| a terminal, in the repository | ~/.tokeneconomizer/bin/te |
| Command | What it does |
|---|---|
status |
what it learned and did here, and what your sessions cost |
microskills |
the facts it injects, and those still forming |
toolprune |
the tools it left out, and the evidence (not on Pi or opencode) |
toolprune restore |
give them back; they will not be removed again |
config |
every setting in effect |
config set microskills=on |
turn a mechanism on (or =off) in this repository; add --global for every repository |
Inside an agent, a command costs one model turn; from a terminal it is free. The other commands
(repos, doctor, config unset, ...) and each agent's permission prompts are listed under
Commands.
Tool pruning and the loop guard are on by default; with these defaults, the plugin calls a model
only when a turn shows signs of looping: the judge, and the redirect it writes if it finds the
turn stuck. Micro-skills is off by default, because its proposer adds model calls and how much it
saves depends on the repository and the work you do there. Each mechanism has its own switch
(microskills, toolprune, loopguard) that takes on or off, and the italic letters above
are these settings:
| Setting | Default | What it sets | |
|---|---|---|---|
| k | microskills_propose_every |
3 | sessions between runs of micro-skills' fact proposer |
| p | toolprune_evidence |
10 | prompts a tool must go unused before it is left out |
| m | toolprune_min_sessions |
2 | sessions those prompts must span |
| s | toolprune_sessions |
5 | or this many sessions, of any length |
| q | toolprune_quiet_minutes |
60 | minutes your sessions here must be idle before a deny list is written |
| e | loopguard.envelope_min_samples |
5 | turns here that ended well (no failing check after the last edit) before the loop guard acts |
| c | loopguard.judge_confidence |
0.7 | how sure the judge must be that a turn is stuck |
| r | loopguard_nudges |
1 | redirects per turn before the guard only tells you |
| b | loopguard.backstop_mult |
2.5 | how many times its usual length a churning turn may run before it is redirected without the judge |
Change them with config set, shown here in Claude Code's form (other agents type it as in
Everyday commands):
/tokeneconomizer:config set microskills=on # turn micro-skills on
/tokeneconomizer:config set microskills_propose_every=5 # k = 5: propo