How microsoft/tokeneconomizer works
TokenEconomizer is a repository-local plugin for Claude Code, Codex, Copilot CLI, and Pi. Agent hooks observe sessions; finished-session traces update repository state and inform later sessions through micro-skills, tool pruning, and failure-loop intervention. The README also documents cache-aware cost accounting and benchmark experiments. Sampled excerpts show the live controller and folding/state mechanisms; several host and benchmark links below rely on README claims or SOURCE INDEX references rather than complete source excerpts.
Main entry points: controller.py Live controller · fold.py Session folding · context.py Repository context · swebench.py Benchmark execution
Components 23
Agent integration
- Host hooks
tokeneconomizer/plugin/hook.py - Pi extension
plugin/pi/tokeneconomizer.ts - Host adapters
tokeneconomizer/plugin/hosts.py
Live runtime
- Live controller
tokeneconomizer/plugin/controller.py - Repository context
tokeneconomizer/plugin/context.py - Repository identity
tokeneconomizer/plugin/repos.py - State paths
tokeneconomizer/plugin/paths.py - Local state store
tokeneconomizer/plugin/store.py - Plugin commands
tokeneconomizer/plugin/cli.py
Session learning
- Session trace readers
tokeneconomizer/trace/calls.py - Environment observations
tokeneconomizer/microskills/observe.py - Micro-skills
tokeneconomizer/microskills/lifecycle.py - Session folding
tokeneconomizer/plugin/fold.py - Tool pruning
tokeneconomizer/toolprune/stage.py - Agent settings
tokeneconomizer/plugin/denylist.py - Loop intervention
tokeneconomizer/plugin/controller.py - Loop signals
tokeneconomizer/loopguard/state.py
Cost and evaluation
- Cache-aware pricing
tokeneconomizer/pricing.py - Benchmark workloads
experiments/te_bench/workloads.py - Benchmark execution
experiments/te_harness/swebench.py - Codex spiral recovery
experiments/te_bench/codex_spiral.py
Other components
- Developer
- Coding agents
Connections 26
- Developer → Coding agents: runs
- Coding agents → Host hooks: fires hooks
- Coding agents → Pi extension: loads extension
- Host hooks → Live controller: dispatches events
- Pi extension → Live controller: integrates hooks
- Live controller → Repository context: builds context
tokeneconomizer/plugin/context.py - Repository context → Repository identity: resolves repository
tokeneconomizer/plugin/context.py - Repository context → State paths: selects paths
tokeneconomizer/plugin/context.py - Repository context → Local state store: opens state
tokeneconomizer/plugin/context.py - Live controller → Host adapters: adapts host
tokeneconomizer/plugin/controller.py - Live controller → Session folding: folds sessions
tokeneconomizer/plugin/controller.py - Live controller → Loop intervention: checks turns
tokeneconomizer/plugin/controller.py - Live controller → Plugin commands: dispatches commands
tokeneconomizer/plugin/cli.py - Session folding → Session trace readers: reads traces
tokeneconomizer/plugin/fold.py - Session folding → Micro-skills: learns facts
tokeneconomizer/plugin/fold.py - Session folding → Tool pruning: tracks usage
tokeneconomizer/plugin/fold.py - Session folding → Local state store: updates state
- Micro-skills → Environment observations: classifies evidence
tokeneconomizer/microskills/envfacts.py - Tool pruning → Agent settings: applies pruning
tokeneconomizer/plugin/denylist.py - Agent settings → Coding agents: changes settings
tokeneconomizer/plugin/denylist.py - Loop intervention → Loop signals: folds tool calls
- Loop intervention → Coding agents: redirects once
- Coding agents → Session trace readers: produces session logs
- Benchmark workloads → Benchmark execution: supplies workloads
- Benchmark execution → Cache-aware pricing: measures cost
experiments/te_harness/swebench.py - Benchmark execution → Codex spiral recovery: runs Codex recovery
experiments/te_harness/swebench.py