TokenEconomizer Uses Cross-Session Repository Memory to Cut AI Coding Agent Token Costs up to 30%
Microsoft Research and UC Berkeley have open-sourced TokenEconomizer, a black-box plugin for Claude Code, Codex, and Copilot CLI that uses micro-skills, tool pruning, and failure-loop intervention to reduce multi-turn token costs by 18.4% to 30.9%.
When developers work in a repository, they carry forward accumulated context: how tests run, which linters fail on generated files, and which build commands work in local containers. Frontier AI coding agents, by contrast, routinely suffer from session amnesia. In every fresh run, an agent re-explores the filesystem, queries build environments, parses large tool manifests, and occasionally burns through half its token budget stuck in repetitive test-and-repair loops.
To tackle this persistent inefficiency, researchers from Microsoft Research, Microsoft Security, and UC Berkeley have released TokenEconomizer, an open-source, repository-scoped optimization plugin. Detailed in their announcement and accompanying research paper, the system operates entirely as an external black box. Rather than altering model weights or modifying agent harnesses, TokenEconomizer observes finished developer sessions and progressively trims wasted context in subsequent tasks.

Where the Tokens Go: Profiling Multi-Turn Sessions
To quantify token waste, the researchers profiled Anthropic’s Claude Code across 60 sequential tasks from the SWE-Bench benchmark, running tasks repository by repository. The empirical traces exposed three chronic sources of token inflation:
- Rediscovering Familiar Facts: After a repository accumulated five completed tasks, 30% of all agent tool calls simply duplicated an exploratory action taken during an earlier task. In contrast, only 7% overlapped with actions in different codebases, showing that discovery costs are heavily repository-specific.
- Paying for Unused Tool Schemas: The agent received 23 full tool definitions on every single model turn, yet 16 of those tools were never invoked across the entire session. In modern long-context agent turns, sending uncalled tool schemas on every request quietly burns input tokens.
- Runaway Failure Loops: While only 17% of tasks ended in failure, those stalled tasks consumed 32% of total token expenditures. Agents repeatedly edited files and re-executed broken test commands without re-evaluating their core assumptions.
Fixed-strategy cost reduction tools frequently backfire across heterogeneous projects. TokenEconomizer replaces static rules with three adaptive mechanisms that tune their intervention to the target codebase.
Mechanism 1: Operational Micro-Skills
Instead of loading extensive documentation into the prompt, TokenEconomizer synthesizes single-line operational notes called micro-skills. The plugin examines cross-session logs and detects environment-dependent quirks, such as substitute command invocations (like using python -m pytest instead of pytest), mandatory build steps, required environment variables, or paths to generated assets.
Micro-skills are subject to a strict evidence threshold: a candidate fact is proposed by an evaluation model every k sessions (k = 3) or identified from cross-session error patterns. A fact is only prepended to the agent’s initial prompt context after three sessions validate the pattern. If downstream sessions contradict the fact, TokenEconomizer promptly deprecates it, preventing stale guidance from poisoning context.
Mechanism 2: Persistent Tool Pruning
Every tool schema in an agent’s active registry consumes prompt cache space and token budget on every turn. TokenEconomizer monitors tool utilization over time. If a tool remains uncalled across p prompts (p = 10) spanning at least m sessions (m = 2), or over s sessions of any length (s = 5), the controller removes it from subsequent sessions.
Crucially, pruning operates through native host configuration files rather than custom forks. For Claude Code, pruned tools are written to .claude/settings.local.json; for Codex, packages are configured in .codex/config.toml; and for GitHub Copilot CLI, entries target .github/copilot/settings.local.json. Essential tools for file editing and command execution remain permanently safeguarded. To preserve prompt cache locality, deny lists are written only after the session has sat idle for q minutes (q = 60). Any pruned tool can be restored instantly with a single command.
Mechanism 3: Failure-Loop Intervention (The Loop Guard)
When coding agents become trapped in repetitive cycles, standard harnesses allow them to churn until they hit token ceilings or turn caps. TokenEconomizer introduces a loop guard that first establishes an empirical envelope: it observes e successful turns (e = 5) to estimate standard task duration in the repository.
When an ongoing turn exhibits churning edits or repeating error messages, an independent judge model evaluates the transcript. If the judge reaches a confidence threshold of c >= 0.7, the guard injects a concise context redirect advising the agent to change tactics. To prevent derailment, redirects are capped at r = 1 per turn. If the turn remains stalled after the redirect, the guard notifies the human operator with instructions on how to interrupt the process, avoiding arbitrary automated process termination. This pattern reinforces the principles discussed in Automater’s guide on CI agent fix loop guards.
Benchmark Results: Consistent Savings Without Success Degradation
The authors evaluated TokenEconomizer across 277 SWE-Bench bug-fixing tasks in six open-source repositories using five distinct agent and model pairings. Evaluated agents included Claude Code, Codex, and GitHub Copilot CLI paired with frontier reasoning engines.
| Metric / Setting | Baseline Agents | With TokenEconomizer | Net Impact |
|---|---|---|---|
| Mean Token Cost Reduction | Baseline (0%) | 18.4% to 30.9% lower | Significant cost savings |
| Repository Cell Consistency | Variable spend | Cheaper in all 30/30 cells | 100% repository coverage |
| Pass@1 Success Rate | Baseline | Stable or improved (up in 4/5) | Zero performance penalty |
| Web-Bench Sequential Tasks | Baseline | 16.0% to 21.2% lower | Domain transfer |
| Security Auditing Tasks | Baseline | 6.4% to 19.6% lower | Domain transfer |
| Breakeven Point | Task 1 | Reached by Task 6 | Cumulative net positive |
All reported cost figures include the internal inference calls made by TokenEconomizer’s fact proposer and judge models. While early repository tasks incur a slight learning overhead, cumulative token expenditure fell below unassisted baselines by the sixth task in every evaluated repository. By comparison, attempting to reduce costs simply by dialing down model reasoning effort caused substantial degradation in task success rates, demonstrating why teams should evaluate cost per completed task vs token price.
Local Architecture and Tool Support
TokenEconomizer is written with Python standard-library dependencies, requiring Python 3.10 or newer. It requires no separate API keys or external SaaS infrastructure. All learned facts, tool statistics, and loop envelopes remain strictly local on the developer’s machine under ~/.tokeneconomizer.
Installation integrates directly with host package and plugin ecosystems:
# Clone the repository
git clone https://github.com/microsoft/TokenEconomizer.git
# Install for Claude Code
claude plugin marketplace add "$PWD/TokenEconomizer"
claude plugin install tokeneconomizer@tokeneconomizer
# Check status in any repository
/tokeneconomizer:status
Beyond Claude Code, Codex, and GitHub Copilot CLI, the project includes experimental support for Pi, opencode, Cursor Agent, Grok Build, Factory Droid, Hermes Agent, and the Antigravity CLI. Operators can inspect active learned facts via /tokeneconomizer:microskills, examine excluded tools with /tokeneconomizer:toolprune, or restore pruned tools immediately using /tokeneconomizer:toolprune restore.
Operational Takeaways for Engineering Fleets
As autonomous agents transition from single-prompt experiments into persistent engineering collaborators, treating every session as an isolated blank slate is economically unsustainable. TokenEconomizer demonstrates that external, experience-driven controllers can deliver 18.4% to 30.9% token reductions without requiring model fine-tuning or proprietary agent modifications. For engineering teams managing substantial agent fleets, deploying local cross-session caching and adaptive tool filtering offers an immediate, low-risk path toward scaling autonomous workflows responsibly.