Command Palette
Search for a command to run...
TokenEconomizer: Experience-Driven Black-Box Cost Control for Coding Agents
TokenEconomizer: Experience-Driven Black-Box Cost Control for Coding Agents
Abstract
Advances in frontier coding agents have sharply increased the token costs of AI-assisted software engineering. Their token eficiency is typically evaluated on isolated tasks, whereas real-world developers use them to continuously build, update, and maintain large-scale projects. This mismatch motivates us to evaluate agents over sequences of realistic coding tasks, where we find that agents exhibit recurring environment-dependent token costs that existing methods struggle to reduce. To minimize these costs, we introduce TokenEconomizer, an external controller that cuts environmentspecific token costs over a sequence of tasks. These cuts come from determining (i) which operational micro-skills to inject, (ii) which persistently irrelevant tools to prune, and (iii) when to intervene in unproductive failure loops. Across Claude Code, Codex, and GitHub Copilot CLI paired with frontier models, TokenEconomizer reduces the average token cost by 18.4–30.9% with comparable task success. Savings hold in every repository under every tested agent–model setting and generalize to diverse domains such as sequential web development and security auditing, where costs fall by 6.4–21.2% depending on task structure. Our work establishes adaptive black-box cost control as a promising direction for reducing token costs of coding agents in realistic settings.
One-sentence Summary
Researchers from UC Berkeley, Microsoft Research, and Microsoft Security propose TokenEconomizer, an external black-box controller that reduces coding agents' environment-specific token costs over sequences of realistic coding tasks by injecting operational micro-skills, pruning persistently irrelevant tools, and intervening in unproductive failure loops, achieving 18.4–30.9% average token-cost savings with comparable task success across Claude Code, Codex, and GitHub Copilot CLI paired with frontier models and yielding 6.4–21.2% savings in sequential web development and security auditing.
Key Contributions
- The paper introduces TokenEconomizer, an external black-box controller that learns from completed task traces to reduce environment-specific token costs in coding agents by selecting operational micro-skills to inject, pruning persistently irrelevant tools, and intervening in unproductive failure loops.
- The method formulates these choices as environment-specific meta-strategies applied through the agent's prompt, tool configuration, and event stream, with recovery paths that retire stale skills, restore pruned tools, and redirect uncertain cases instead of stopping them.
- Results show that across Claude Code, Codex, and GitHub Copilot CLI paired with frontier models, TokenEconomizer lowers average token cost by 18.4% to 30.9% with comparable task success, with reductions in every tested repository and generalization to sequential web development and security auditing where costs fall by 6.4% to 21.2%.
Introduction
Frontier coding agents such as Claude Code, Codex, and GitHub Copilot CLI can complete long-horizon software engineering tasks, but they incur substantial and compounding token costs because persistent instructions and growing tool histories are charged repeatedly across turns. Prior cost-reduction methods mostly optimize individual tasks through fixed prompt compression, tool restriction, repository maps, or routing, but these approaches often require agent modifications, trade away task success, produce inconsistent savings across repositories, and overlook the reusable experience that accumulates across tasks in a shared codebase. The authors analyze default agent traces and identify three recurring sources of removable cost: operational knowledge that is repeatedly rediscovered, loaded tools that remain unused, and failed trajectories that consume disproportionate spend. To address these issues, they introduce TokenEconomizer, an external black-box controller that uses completed execution traces to learn environment-specific policies for activating micro-skills, pruning irrelevant tools, and intervening in unproductive failure loops, yielding consistent cost reductions while maintaining comparable task quality across multiple agents and benchmarks.
Method
The authors begin by profiling agent traces to identify three primary sources of removable cost: repeated operational work within a repository, a large number of loaded tools that remain unused, and failed trajectories that consume a disproportionate share of the total spend.
As shown in the figure below:
To address these inefficiencies, the authors introduce TokenEconomizer, an external controller that applies three repository-agnostic meta-strategies to learn repository-specific policies from completed traces. This controller operates without modifying the underlying agent, instead acting through the instruction prefix, loaded tools, and live event stream.
Refer to the framework diagram:
The system optimizes a chronological task stream τ1,…,τn within an environment. Before a task τi begins, the controller C determines the active policies based on the task description and the accumulated repository state Si−1:
(ΔPi,Ui,Bi)=C(τi,Si−1).Here, ΔPi represents the set of micro-skills activated for the task, Ui is the restricted set of loaded tools, and Bi is a turn budget for failure-loop intervention. After the agent executes the task to produce a trajectory ξi, the controller updates the repository state to Si.
Meta-strategy I: Experience-grounded Micro-skills This module synthesizes operational knowledge from past trajectories. The controller condenses completed traces into digests of operational evidence, such as command templates and error patterns. An LLM proposer drafts candidate micro-skills, each pairing an operational instruction with executable detectors. A scope detector checks if the skill's situation arose in a trajectory, while a need detector records if the task actually required the skill. The support of a skill f at task i is calculated as:
qi(f)=j<i∑γi−j(1[ξj confirms f]−λ1[ξj contradicts f]),where γ<1 discounts older evidence and λ>1 penalizes contradictions more heavily. A skill is activated only if it is confirmed across tasks with non-overlapping edited files and its need rate meets a threshold. If qi(f)<0, the skill is retired.
Meta-strategy II: Tool Pruning To reduce the recurring cost of the tool interface, this module omits schemas unlikely to help. Core tools for shell execution and file manipulation are always retained. Other tools are removed if they remain uncalled for a fixed number of consecutive tasks or if an LLM judge deems them irrelevant to the current task description. To recover from mistaken exclusions, the controller periodically restores the full tool catalog for a re-audit; any tool called during this audit is permanently unmasked.
Meta-strategy III: Failure-loop Intervention This module derives a repository-specific turn budget to identify unproductive loops. The budget for task τi is defined as:
Bi=β⋅Qp({∣ξj∣:j<i,yj=1}),where Qp is the empirical p-th percentile of turn counts from successful prior tasks, and β>1 is a margin. During execution, a rule-based check monitors for repeated failure signatures or if the trajectory exceeds Bi. If triggered, an LLM judge reviews a digest of recent actions to distinguish a stuck loop from a difficult but progressing task. A confident stuck verdict prompts a redirection with a summary of recent actions. If the pattern recurs, the rollout is terminated. A forced stop acts as a backstop at κBi turns if strict stall conditions are met.
Experiment
The authors first profile 60 SWE-Bench tasks across six repositories and find that fixed strategies such as RTK, Serena, and RepoMap produce mixed repository-level cost changes, motivating adaptive learned control. They then evaluate TokenEconomizer across five agent model settings on 277 SWE-Bench tasks, plus Web-Bench and two security benchmarks, comparing it with default execution, reduced reasoning effort, and fixed strategies. TokenEconomizer consistently reduces cache-aware cost while largely maintaining or improving Pass@1, with savings accumulating after a few tasks and ablations attributing most gains to tool pruning and micro-skill injection. Generalization results show substantial savings on Web-Bench and CredData, smaller but consistent savings on ReposVul, indicating that benefits vary with reusable structure and cost-reduction headroom.
Fixed cost-control strategies such as RTK and RepoMap show inconsistent cost effects, sometimes raising cost while leaving pass rates close to default. Lower reasoning effort saves cost but consistently reduces Pass@1. TokenEconomizer provides consistent cost reductions across settings and repository cells while generally preserving or improving pass rates, and its variants define the pass-preserving Pareto frontier. Lower reasoning effort delivers large cost savings but reduces Pass@1 in every setting. RTK and RepoMap produce mixed cost changes, with each increasing cost in many repository setting cells. TokenEconomizer alone lowers cost in every repository setting cell while preserving or improving pass rates in most settings. TokenEconomizer variants, with or without RTK or RepoMap, cover all pass-preserving Pareto frontier points.
Across the reported SWE-Bench runs, the combined meta-strategy stack achieves substantial cost savings relative to default execution while keeping Pass@1 close to or slightly above baseline. Tool pruning and micro-skill injection provide the largest individual savings, and all individual variants maintain pass rates within a few points of default. Savings generalize to other task structures but scale with reusable procedure and available cost headroom. Combining micro-skill injection, tool pruning, and the failure-loop guard produces the largest SWE-Bench cost reduction while keeping Pass@1 at or slightly above default. Tool pruning and micro-skill injection are the largest single contributors, both delivering double-digit savings, while the failure-loop guard contributes smaller single-digit savings. Each individual meta-strategy keeps SWE-Bench Pass@1 within three points of default, so the cost reductions do not come from a large quality sacrifice. Adaptive control variants occupy the Pareto frontier points that maintain or improve default pass rate, whereas reduced reasoning effort trades larger savings for notable pass-rate losses. Savings generalize across Web-Bench and CredData but are much smaller on ReposVul, where shorter trajectories leave less removable cost.
The first set of experiments evaluates fixed cost-control methods, reduced reasoning effort, and TokenEconomizer, finding that only TokenEconomizer consistently lowers cost across settings while preserving or improving pass rates, whereas RTK and RepoMap show inconsistent cost effects and reduced reasoning effort regularly lowers Pass@1. The second set evaluates a combined meta-strategy stack on SWE-Bench and generalization tasks, showing that micro-skill injection, tool pruning, and a failure-loop guard together deliver substantial cost savings while keeping Pass@1 near or slightly above default, with tool pruning and micro-skill injection as the largest contributors. Adaptive control variants occupy the pass-preserving Pareto frontier, while reduced reasoning effort trades larger savings for notable pass-rate losses, and savings generalize to Web-Bench and CredData but are smaller on ReposVul due to shorter trajectories.