Control what your AI can see. LeanCTX (Lean Context) is the context intelligence layer for AI agents — one local Rust binary that decides what they read, rememb
Control what your AI can see. LeanCTX (Lean Context) is the context intelligence layer for AI agents — one local Rust binary that decides what they read, rememb
Control what your AI can see — and what it costs. LeanCTX is an AI Value Gate for coding agents: it understands tasks, routes and compresses context, remembers what it learns, and measures cost against accepted outcomes.
Token savings are the receipt. Intelligence is the product. Works with Cursor, Claude Code, Copilot, Windsurf, Codex, Gemini and 30+ other agents — no config needed.
See it in action:
|
All GIFs are generated from reproducible VHS tapes in demo/.
lean-ctx setup command, no config changes neededSaves you tokens? Give it a star — it helps others discover LeanCTX.
Models are converging on commodity. The durable edge isn't which model you call — it's your context: what your agents read, what they remember, and what you can prove. And the layer that optimizes and owns that context can't come from the vendor that bills per token or keeps your memory in a black box — it has to sit on your side.
That's the shift behind "agent entities" that live in your chat and remember your company (Claude in Slack, ClickUp Brain): a context login, not a model login — you end up renting your own company knowledge back. LeanCTX is the opposite layer. It keeps the moat yours: local-first, portable (.ctxpkg), and model-agnostic — swap OpenAI, Anthropic or Gemini without losing context or cache. Own your context; don't rent it back.
LeanCTX treats context and AI spend as managed resources, not afterthoughts. One binary covers the capabilities that decide how well an AI agent performs:
Your AI agent reads files and runs commands. LeanCTX compresses both automatically.
Workload-specific token reduction on eligible context, with recovery paths and a local Shadow Mode baseline for measurement
File reads: 10 read modes (full, map, signatures, diff, lines:N-M, density:X, …) — cached re-reads cost ~13 tokens
Target density (density:0.4): SDE-style budget compression — keeps the highest-entropy lines until ~40% of the original tokens remain, deterministic
JIT disclosure: signatures carries line spans and points at lines:N-M for targeted expansion — outline first, bodies on demand
Shell output: 95+ shell-output patterns compress git, npm, cargo, docker, kubectl, terraform and more (270 passthrough rules)
Tree-sitter AST: structural understanding for 27 languages — not just text compression
Reversible by design (CCR): compression never discards content — pruned or truncated payloads move to a content-addressed store with a deterministic handle, so the model can pull the original bytes back on demand via ctx_expand, ctx_retrieve, an in-band marker, or GET /v1/references/{id}. Five recovery paths →
Not every task or file needs the same depth. LeanCTX classifies the task, then sends the signal rather than the noise.
ModePredictor: learns the optimal read mode per file type from past sessionsIntentEngine: classifies query complexity so simple lookups stay cheapRelevant code, sessions, and connected sources become focused context instead of a larger prompt.
.ctxpkg package and move it across machines or models, instead of locking it in a vendor's black boxPerformance is the cost of a useful result, not just speed. LeanCTX records costs and outcomes locally; CPAO (Cost per Accepted Outcome) is the north-star metric for comparing useful AI work.
ctx_proof, ctx_verify): 4-layer verification engine with CI drift gatesShadow Mode compares LeanCTX treatment with a configured baseline without changing the active workflow. Its reports show cost, tokens, CPAO, and whether quality held, then recommend savings only when the comparison supports them.
LeanCTX automatically tracks local cost and outcome signals; it does not add those reports to agent context. CPAO — cost per accepted outcome — is the north-star metric, while Shadow Mode provides a baseline comparison for savings.
lean-ctx savings --period week # costs, token savings, and CPAO
lean-ctx value-report --format markdown --last 20 # recent outcome quality
lean-ctx shadow --latest # latest baseline comparison# Enable shadow mode for savings comparison
echo '[shadow]\nenabled = true' >> ~/.config/lean-ctx/config.toml
# After using LeanCTX for a while:
lean-ctx savings
lean-ctx shadow --latestctx_url_read): pull a public web page, PDF, or YouTube transcript into context as compressed, citation-backed text — facts/quotes return claims with a confidence score + source URL, relevance-ranked research-compression distils to a token budget, SSRF-guarded (http/https only)ctx_refactor): language-server-powered rename, references, go-to-definition via rust-analyzer, typescript-language-server, pylsp, goplsctx_agent, ctx_handoff): agent handoff with context transfer bundles, diary system, synchronized shared statectx_expand search_all): FTS5-powered cross-archive search over all previously archived tool outputslean-ctx pack --pr builds a PR-ready context pack (changed files, related tests, impact, artifacts)lean-ctx pack create bundles Knowledge + Graph + Session into portable .ctxpkg files with SHA-256 integritylean-ctx snapshot create|list|show|verify|restore|publish|import — git-anchored, ed25519-signed snapshots of the layer state (lineage, ledger Φ, ROI, session) on an append-only timeline; replay them in the dashboard, restore to resume a session (and --git to check out the commit), or publish/import a signed snapshot to share it (concept →)lean-ctx gain --live for real-time savings, lean-ctx wrapped for weekly/monthly summaries (gain --svg/--share for a shareable card or self-hostable page), lean-ctx watch for TUI monitoringlean-ctx savings is an auditable, per-event ledger (tokenizer transparency, bounce-netting, tamper-evident SHA-256 chain) — local-only, on by defaultlean-ctx serve for Streamable HTTP MCP + /v1/tools/call (used by the Cookbook and external clients)You don't have to choose between LeanCTX and the other context tools you already
like. An addon is a signed package carrying either a sandboxed WASM module
that runs inside the context pipeline, or an [mcp] declaration that wires an
external MCP server into the gateway — after which LeanCTX treats what it returns
like your own reads instead of just proxying it.
lean-ctx addon release ./my-addon # build a signed .ctxpkg — no artifact host, no CI
lean-ctx addon add ./my-addon-1.0.0.ctxpkg # verify, disclose, ask, install
lean-ctx addon list # what's installed, what loads, what's wiredadd re-checks the signature on your
machine rather than trusting its source, checks every module against its
pinned SHA-256, prints the publisher key and the exact command any declared
server would run, and only then prompts.uv tool install, no
npx. Fetching a declared tool stays your step, where your own package
manager's trust model applies. The manifest says how to run it.ctx_expand handle, index into BM25 / graph / knowledge. A typed integration routes specific tools straight into ctx_expand, ctx_callgraph and ctx_knowledge.There is deliberately no marketplace and no addon search: LeanCTX does not
host, curate or rank addons. A package is a file you install, or one you fetch
from a registry you name. See the
addon guide for the full walkthrough.
LeanCTX is growing from a single context layer into a full cognitive context layer for whole teams: version-controlled context strategy, one unified graph, and a governance layer across many agents.
ctxpkg.com registry for hosted, versioned context history and a side-by-side model-view | git-diff replay. The temporal axis through everything LeanCTX does — it decides, remembers, guards, proves, and replays. (concept →)No open issues yet, or sync has not completed.