README.md

July 6, 2026 · View on GitHub

LightMem2 logo

A modular framework for long-running agent memory and context management

framework hosts component pnpm license

LightMem2 overview

LightMem2 is a lightweight runtime framework for long-running LLM agents. It reduces context growth and serving cost in real shared-session workloads.

95.7% fewer input tokens   |   87.0% lower cost
vs. Vanilla OpenClaw on Claw-Eval continuous mode

67.4% fewer input tokens   |   61.5% lower cost
vs. Vanilla OpenClaw on PinchBench continuous mode


🧩 Components

LightMem2 is intended to host multiple long-running-agent components over time.

ComponentWhat It DoesHow It WorksEffect
TokenPilotKeeps long-running agent sessions smaller, cheaper, and easier to sustainStabilizes the reusable prompt prefix, trims oversized tool output before it poisons later turns, and limits how much old context is carried forward as sessions growBetter cache reuse, lower token usage, lower cost, and less context bloat in shared sessions

📑 Table of Contents

📢 News

🔧 Installation

1. Prepare the Repository Once

Clone the repository and build the shared packages:

git clone https://github.com/zjunlp/LightMem2.git
cd LightMem2
corepack enable
pnpm install
pnpm build
pnpm lightmem2:build
pnpm lightmem2:install

2. Pick Your Host

Open the host you want and run the default install commands.

OpenClaw

Default install:

pnpm component:install:tokenpilot:openclaw

This installs the current TokenPilot OpenClaw adapter, updates ~/.openclaw/openclaw.json, enables the plugin, switches plugins.slots.contextEngine to layered-context, applies the default normal mode, and tries to restart the OpenClaw gateway automatically.

If your OpenClaw home or config path is not under the default ~/.openclaw, set:

export LIGHTMEM2_OPENCLAW_HOME="/path/to/openclaw-home"
export OPENCLAW_CONFIG_PATH="/path/to/openclaw.json"

Then run the same install command again:

pnpm component:install:tokenpilot:openclaw
Codex CLI

Default install:

npm --prefix components/tokenpilot/adapters/codex run build
npm --prefix components/tokenpilot/adapters/codex run install:codex

This keeps your current active Codex provider name, reroutes that provider through the local TokenPilot proxy, writes ~/.codex/tokenpilot.json, registers hooks in ~/.codex/hooks.json, registers the shared tokenpilot_memory_fault_recover MCP server, and creates the standalone lightmem2 CLI entrypoint at ~/.local/bin/lightmem2.

If your Codex config files are not under the default ~/.codex, set:

export CODEX_CONFIG_PATH="/path/to/config.toml"
export CODEX_HOOKS_CONFIG_PATH="/path/to/hooks.json"
export TOKENPILOT_CODEX_CONFIG="/path/to/tokenpilot.json"

Then run the same install flow:

npm --prefix components/tokenpilot/adapters/codex run build
npm --prefix components/tokenpilot/adapters/codex run install:codex

If lightmem2 is not found after install, make sure ~/.local/bin is on your PATH.

Claude Code

Default install:

npm --prefix components/tokenpilot/adapters/claude-code run build
npm --prefix components/tokenpilot/adapters/claude-code run install:claude-code

This updates ~/.claude/settings.json for local gateway routing, writes ~/.claude/tokenpilot.json, registers the shared tokenpilot_memory_fault_recover MCP server in ~/.claude/.claude.json, installs a SessionStart hook that auto-starts the local gateway on first use, and preserves existing Claude files as .tokenpilot.bak backups before rewriting.

If your Claude Code files are not under the default ~/.claude, set:

export CLAUDE_CODE_SETTINGS_PATH="/path/to/settings.json"
export CLAUDE_CODE_MCP_CONFIG_PATH="/path/to/.claude.json"
export TOKENPILOT_CLAUDE_CODE_CONFIG="/path/to/tokenpilot.json"

Then run the same install flow:

npm --prefix components/tokenpilot/adapters/claude-code run build
npm --prefix components/tokenpilot/adapters/claude-code run install:claude-code

If lightmem2 is not found after install, make sure ~/.local/bin is on your PATH.

⚡ Quick Start

Pick your host and open the matching one-pass setup below.

OpenClaw
  1. Start or restart OpenClaw.
  2. Open a session with a lightmem2/<model> model such as lightmem2/gpt-5.4-mini.
  3. Run:
/lightmem2 status

You should see a status block similar to:

  • plugin entry enabled
  • config enabled
  • mode normal
  • context engine slot layered-context
  • stabilizer enabled
  • reduction enabled

For a fuller runtime summary, run:

/lightmem2 report
/lightmem2 doctor
/lightmem2 visual
/lightmem2 mode normal

/lightmem2 doctor is the quickest integration self-check for the current OpenClaw adapter surface. /lightmem2 visual opens the local visual inspector for stability, reduction, and eviction snapshots. /lightmem2 mode <conservative|normal|aggressive> switches preset runtime behavior.

You can also use the standalone CLI outside OpenClaw:

lightmem2 openclaw status
lightmem2 openclaw report
lightmem2 openclaw doctor
lightmem2 openclaw visual
lightmem2 openclaw mode normal
Codex CLI

The current Codex path uses the standalone CLI plus Codex hooks.

  1. Run the Codex install flow shown above.
  2. Start Codex normally.
  3. If Codex asks you to review or trust the installed TokenPilot hooks, approve them.
  4. Open a new Codex session so SessionStart can start the local proxy.
  5. In another terminal, verify the adapter:
lightmem2 codex status
lightmem2 codex doctor
lightmem2 codex report
lightmem2 codex mode normal
lightmem2 codex reduction status
lightmem2 codex stabilizer target user

Expected first-run shape:

  • lightmem2 codex doctor reports proxy healthy: yes
  • lightmem2 codex status shows stabilizer and reduction enabled
  • after a few turns, lightmem2 codex report no longer says No TokenPilot session stats yet.

Install success does not always mean the proxy is already running before the first trusted session. If doctor still reports proxy healthy: no after trusting hooks and opening a new Codex session, use the manual fallback:

tokenpilot-codex status
tokenpilot-codex start
Claude Code

The current Claude Code path also uses the standalone CLI, but routes requests through a local Anthropic-compatible gateway and a shared MCP recovery server.

  1. Run the Claude Code install flow shown above.
  2. Start Claude Code normally.
  3. Open a new Claude Code session so SessionStart can auto-start the local gateway.
  4. In another terminal, verify the adapter:
lightmem2 claude-code status
lightmem2 claude-code doctor
lightmem2 claude-code report
lightmem2 claude-code mode normal
lightmem2 claude-code reduction status
lightmem2 claude-code stabilizer target developer

Expected first-run shape:

  • lightmem2 claude-code doctor reports proxy healthy: yes
  • lightmem2 claude-code status shows stabilizer and reduction enabled
  • after a few turns, lightmem2 claude-code report no longer says No TokenPilot session stats yet.

Like Codex, install success does not guarantee that the gateway is already healthy before the first real session triggers SessionStart.

🖼️ Visual Results

The screenshots below come from the built-in visual inspector opened with:

lightmem2 visual
TokenPilot runtime effects

Stable-prefix view:

TokenPilot stabilizer view

Reduction view:

TokenPilot reduction view

Eviction view:

TokenPilot eviction view

🏗️ Architecture

The current public repository is organized around the released component and its current host adapters.

At a high level:

  • components/<name>/packages
    • shared logic that should remain reusable across hosts
  • components/<name>/adapters
    • host-specific integration code, install surfaces, runtime hooks, and command wiring
LightMem2/
├── components/
│   └── tokenpilot/
│       ├── adapters/
│       │   ├── openclaw/         # production host adapter for OpenClaw
│       │   ├── codex/            # Codex CLI adapter with hooks + local proxy
│       │   └── claude-code/      # Claude Code adapter with gateway routing + MCP recovery
│       ├── products/
│       │   ├── cli/              # shared lightmem2 CLI surface
│       │   └── mcp/              # shared memory_fault_recover MCP server
│       └── packages/
│           ├── host-adapter/     # Shared host-adapter contracts and path-resolution interfaces
│           ├── runtime-core/     # Host-agnostic runtime engine and shared execution logic
│           ├── kernel/           # Shared types, interfaces, events, and runtime contracts
│           └── layers/           # Stateful and policy-oriented logic
│               ├── history/      # Canonical state, raw semantic turns, task registry
│               ├── decision/     # Policy analysis, reduction/eviction decisions, estimator
│               └── memory/       # Experimental memory layer; distillation and retrieval are still in progress
├── docs/                         # Public-facing notes and smoke helpers for the current runtime path
├── experiments/                  # Benchmark adapters and evaluation scripts for the current runtime path
└── README.md

🧪 Experiments

Use these following docs for benchmark-specific assets, environment setup, and runner commands. Experiment entrypoints:

💡 Commands

Use the basic commands first, then the session-aware and advanced ones when you need them.

Shared standalone CLI patterns:

lightmem2 report
lightmem2 visual
lightmem2 use openclaw
lightmem2 use codex session <session-id>
lightmem2 context
lightmem2 <host> session <session-id> report
  • lightmem2 report shows the latest available report across hosts
  • lightmem2 visual opens the shared browser visual and lets you switch hosts and sessions
  • lightmem2 use <host> sets the default host for hostless CLI commands
  • lightmem2 use <host> session <session-id> pins the default session for later report and visual
  • lightmem2 context shows the current default host, pinned session, and remembered config target
  • lightmem2 <host> session <session-id> report reads one specific session directly

Pick your host for the command surface below.

OpenClaw

Inside an OpenClaw session:

/lightmem2 status
/lightmem2 report
/lightmem2 doctor
/lightmem2 visual
/lightmem2 mode normal
/lightmem2 stabilizer target developer
/lightmem2 reduction mode balanced
/lightmem2 eviction on
/lightmem2 help

Outside OpenClaw, the standalone CLI supports the same host directly:

lightmem2 openclaw status
lightmem2 openclaw report
lightmem2 openclaw doctor
lightmem2 openclaw visual
lightmem2 openclaw mode normal
lightmem2 openclaw session <session-id> report

Useful OpenClaw-only controls:

  • mode aggressive enables the most aggressive runtime policy preset
  • eviction ... controls lifecycle-aware context eviction
  • settings details on expands status output with more runtime detail
  • stabilizer ... and reduction ... let you tune prefix stabilization and observation reduction directly
Codex CLI

Use the standalone CLI:

lightmem2 codex status
lightmem2 codex report
lightmem2 codex doctor
lightmem2 codex visual
lightmem2 codex session <session-id> report
lightmem2 codex reduction status
lightmem2 codex stabilizer target developer
lightmem2 codex mode normal
lightmem2 codex reduction mode balanced
lightmem2 codex help

Useful Codex controls:

  • stabilizer on|off toggles stable-prefix rewriting
  • stabilizer target <developer|user> chooses where dynamic context is attached
  • reduction on|off toggles observation reduction
  • reduction mode <light|balanced> switches between lighter and stronger trimming
  • reduction pass toolPayloadTrim off disables one specific reduction pass
Claude Code

Use the standalone CLI:

lightmem2 claude-code status
lightmem2 claude-code report
lightmem2 claude-code doctor
lightmem2 claude-code visual
lightmem2 claude-code session <session-id> report
lightmem2 claude-code reduction status
lightmem2 claude-code stabilizer target developer
lightmem2 claude-code mode normal
lightmem2 claude-code reduction mode balanced
lightmem2 claude-code help

Useful Claude Code controls:

  • stabilizer on|off toggles stable-prefix rewriting
  • stabilizer target <developer|user> chooses where dynamic context is attached
  • reduction on|off toggles observation reduction
  • reduction mode <light|balanced> switches between lighter and stronger trimming
  • reduction pass toolPayloadTrim off disables one specific reduction pass

📁 Experimental Results

The tables below summarize the current LightMem2 runtime path, implemented today through the TokenPilot component, on PinchBench and Claw-Eval.

Isolated mode evaluates each task in a fresh session, focusing on single-task behavior without cross-task history carryover. Continuous mode evaluates longer-running shared-session workflows, where context accumulation and cache reuse matter much more.

For exact reproduction commands and benchmark-specific setup, start from: experiments/README.md

PinchBench

PinchBench logs and output bundles: PinchBench Result

Isolated Mode

MethodOverallProdResWriteCodeAnalCSVLogMeetMemSkillIntegCache Read (M)Cache Miss (M)Output (M)Cost ($)
Vanilla80.587.268.784.186.075.183.094.781.486.570.355.36.1848.7530.2858.31
LLMLingua-276.989.364.082.186.980.879.684.466.385.079.672.114.2413.9750.3845.78
SelectiveContext76.588.564.573.083.782.681.192.863.386.982.877.211.2734.6420.3245.79
LCM77.890.164.979.685.481.381.087.167.585.081.780.616.0183.0640.3565.10
Pichay78.985.458.971.879.088.379.883.684.091.369.863.36.7173.3330.2384.07
Summary79.580.766.383.577.982.187.577.281.392.567.254.412.3033.0090.2964.51
MemoBrain78.186.862.188.985.782.688.385.463.692.576.169.710.2002.1070.2333.36
AgentSwing78.489.871.980.279.583.580.883.777.992.565.735.04.5347.1290.2416.77
Keep-Last-N80.486.070.082.480.177.678.391.584.392.570.187.812.8132.6570.2914.26
MemOS79.484.254.483.182.378.281.197.277.692.585.980.229.0184.5730.4927.81
LightMem281.089.071.280.072.688.985.395.279.495.095.258.08.8931.9330.2443.22

Continuous Mode

MethodOverallProdResWriteCodeAnalCSVLogMeetMemSkillIntegCache Read (M)Cache Miss (M)Output (M)Cost ($)
Vanilla79.283.558.486.880.078.587.894.677.695.055.883.625.0155.9430.2027.24
LLMLingua-273.885.858.480.374.379.682.884.263.490.079.183.620.5742.1830.1944.06
SelectiveContext74.085.464.283.175.478.877.391.262.289.571.080.325.4752.6080.1964.75
LCM77.088.163.290.175.778.585.488.965.182.880.878.218.7082.4170.2224.21
Pichay76.588.066.776.281.077.683.584.267.6100.063.875.311.6986.8740.2607.20
Summary78.489.164.473.882.969.681.693.680.395.061.775.320.6876.2490.1967.12
MemoBrain78.087.765.085.584.975.981.089.072.390.386.684.712.9172.2830.2323.73
AgentSwing78.586.367.389.079.182.487.468.172.493.861.783.812.6805.4760.3146.47
Keep-Last-N79.186.367.087.887.077.085.477.375.995.056.875.118.1174.4810.2095.66
MemOS80.987.559.085.487.182.081.095.078.192.587.484.130.8598.9390.30810.41
LightMem281.376.776.990.684.186.085.689.173.695.077.280.18.5511.5490.2192.79

PinchBench abbreviations: Prod=Productivity, Res=Research, Write=Writing, Code=Coding, Anal=Analysis, CSV=CSV Analysis, Log=Log Analysis, Meet=Meeting Analysis, Mem=Memory, Skill=Skills, Integ=Integrations.

Claw-Eval

Claw-Eval logs and output bundles: Claw-Eval Result

Isolated Mode

MethodOverallWkflOpsFinOffCommProdOprnSafeTermMMOthCache Read (M)Cache Miss (M)Output (M)Cost ($)
Vanilla64.565.470.845.744.473.270.977.774.056.841.069.29.4294.6370.2165.16
LLMLingua-261.958.767.557.643.362.970.162.461.049.644.075.28.1694.0430.1824.44
SelectiveContext60.759.168.246.336.961.575.559.267.253.144.074.78.2713.8620.1814.31
LCM61.259.067.351.147.765.976.658.458.651.441.572.29.7763.5430.1724.17
Pichay59.357.362.138.239.468.565.091.664.125.655.076.54.6483.9440.1864.14
Summary62.070.071.032.220.680.068.582.849.220.041.071.42.9352.8710.1743.16
MemoBrain58.064.560.526.137.656.159.971.063.420.041.075.318.1825.1180.3326.69
AgentSwing60.964.266.544.145.767.852.885.857.225.653.668.84.5803.5850.1943.91
Keep-Last-N61.867.173.844.721.654.563.686.238.439.455.069.14.2291.8450.1862.54
MemOS61.664.774.240.925.271.232.073.680.220.056.274.612.5822.7090.3634.61
LightMem263.168.175.447.022.371.865.072.047.837.045.669.94.4361.1540.2392.27

Continuous Mode

MethodOverallWkflOpsFinOffCommProdOprnSafeTermMMOthCache Read (M)Cache Miss (M)Output (M)Cost ($)
Vanilla63.470.880.326.727.862.273.478.463.620.041.069.6709.84521.9812.62281.52
LLMLingua-259.058.771.334.830.661.965.377.664.620.041.072.4575.65437.1972.63082.91
SelectiveContext56.558.171.621.821.254.774.057.766.420.041.072.3437.11448.6782.75481.69
LCM61.466.869.038.329.563.374.966.667.320.041.072.7383.00728.7142.69162.37
Pichay61.069.563.840.324.063.167.094.152.521.641.071.097.43163.5101.04659.65
Summary61.663.674.535.320.655.570.187.166.169.042.666.959.77210.1431.00116.59
MemoBrain57.965.955.024.936.747.873.564.260.620.038.481.647.49713.9901.13419.16
AgentSwing62.267.666.548.636.870.063.890.731.722.441.072.853.77610.0270.90715.63
Keep-Last-N60.765.374.035.520.854.173.691.935.759.542.464.744.8129.1060.78013.70
MemOS57.755.965.056.322.244.864.668.889.020.039.671.549.74225.4320.29324.12
LightMem260.858.861.852.532.164.257.389.265.876.845.270.921.4309.9280.33810.58

Claw-Eval abbreviations: Wkfl=Workflow, Ops=Ops, Fin=Finance, Off=Office QA, Comm=Communication, Prod=Productivity, Oprn=Operations, Safe=Safety, Term=Terminal, MM=Multimodal, Oth=Others.

📄 Citation

Please cite our paper if you use LightMem2 in your work.

@article{xu2026tokenpilot,
  title={TokenPilot: Cache-Efficient Context Management for LLM Agents},
  author={Xu, Buqiang and Xue, Zirui and Chen, Dianmou and Fu, Chenyang and Wu, Chiyu and Huang, Caiying and Jiang, Chen and Fang, Jizhan and Deng, Xinle and Chen, Yijun and others},
  journal={arXiv preprint arXiv:2606.17016},
  year={2026}
}

@inproceedings{fang2025lightmem,
  title={LightMem: Lightweight and Efficient Memory-Augmented Generation},
  author={Jizhan Fang and Xinle Deng and Haoming Xu and Ziyan Jiang and Yuqi Tang and Ziwen Xu and Shumin Deng and Yunzhi Yao and Mengru Wang and Shuofei Qiao and Huajun Chen and Ningyu Zhang},
  booktitle={The Fourteenth International Conference on Learning Representations},
  year={2026},
  url={https://openreview.net/forum?id=dyJ0GWpjJB}
}

🎉Contributors

We thank all the contributors to this project, more contributors are welcome!

  • LLMLingua-2 — Token-level prompt compression
  • SelectiveContext — Self-information-based context reduction
  • Pichay — Demand paging for LLM context windows
  • MemoBrain — Executive memory for long-horizon reasoning agents
  • AgentSwing — Adaptive parallel context management routing for web agents
  • MemOS — Memory operating system for LLM agents
  • LightMem — Lightweight memory-augmented generation
  • Headroom — Compresses everything when AI agent reads

🙌 We thank all the contributors to this project, and welcome further contributions from the community.