Skip to content
/

lean-ctx

lean-ctx · yvgude/lean-ctx · ★ 2.2k · last commit 2026-05-26

Primitive shape 63 total
Skills 1 MCP tools 62
00

Summary

lean-ctx — Summary

lean-ctx (Lean Cortex) is a single Rust binary with 62 MCP tools that compresses AI agent context at three layers: file reading (10 compression modes, tree-sitter AST for 18 languages, cached re-reads at ~13 tokens), shell output (60+ pattern modules for git/npm/cargo/docker/kubectl), and session memory (CCP — context-carry protocol across chats). Its own benchmark shows 83.6% session savings without CCP and 84.7% with CCP; map mode achieves 96% compression on Rust files. README claims "up to 99%" and "60-99% compression on shell output."

It ships lean-ctx setup (one-command install of shell hook + editor wiring + rules + skills), a browser-based Context Manager dashboard, a lean-ctx gain --live real-time savings counter, a SKILL.md that mandates replacing native Read/Grep/Bash with ctx_read/ctx_shell/ctx_search, multi-agent tools (ctx_agent, ctx_handoff), LSP refactoring (ctx_refactor), and cryptographic context proofs (ctx_proof, ctx_verify).

Compared to seeds, lean-ctx is closest to ccmemory (MCP memory layer) but is broader: it compresses every tool interaction (not just session memory), ships 62 tools (vs ccmemory's ~10), targets 23+ AI clients (not just Claude Code), and adds real-time observability. The SKILL.md's "CRITICAL: NEVER use native Read, Grep, or Shell directly" is the most aggressive tool-replacement mandate in this batch.

01

Overview

lean-ctx — Overview

Origin

Yves Gugger (yvgude). Apache-2.0 and MIT dual license. Rust. Version 3.3.6. Available on crates.io, npm (lean-ctx-bin), Homebrew, AUR, and Pi.dev. Active (last commit 2026-05-26). 20 contributors.

Philosophy

From README:

"LeanCTX stands for Lean Cortex: a lightweight cognitive layer that helps AI agents perceive, compress, remember, route, and reuse context across workflows."

"It governs every token between your code and the AI — so you make better decisions, not just cheaper ones."

From LEAN-CTX.md (injected as agent rules):

"CRITICAL: ALWAYS use lean-ctx tools instead of native equivalents. This is NOT optional." "NEVER use native Read, Grep, or Shell directly."

Three Layers

  1. Compression — file reads + shell output compressed by mode and pattern
  2. Memory — CCP (context-carry protocol) persists tasks/facts/decisions across chat sessions
  3. Governance & Observability — real-time dashboard, budgets, SLOs, context proofs

Benchmark Proof

From BENCHMARKS.md (self-measured via lean-ctx benchmark report .):

Mode Token Savings Quality
map 96.0% 92.0%
signatures 95.7% 88.2%
cache_hit 99.6% N/A

Session simulation (30-min coding): 83.6% savings without CCP, 84.7% with CCP.

Verbatim from README: "Repeated file reads: ~2000 tokens each → Cached re-reads: ~13 tokens", "Raw git status: ~800 tokens → Compressed: ~120 tokens"

02

Architecture

lean-ctx — Architecture

Distribution

  • Universal install: curl -fsSL https://leanctx.com/install.sh | sh
  • Homebrew: brew tap yvgude/lean-ctx && brew install lean-ctx
  • npm: npm install -g lean-ctx-bin
  • Cargo: cargo install lean-ctx
  • Pi: pi install npm:pi-lean-ctx

Directory Structure

rust/                    # Rust source (binary)
skills/lean-ctx/         # SKILL.md + install scripts
  └── SKILL.md           # Agent behavioral mandate
.claude-plugin/
  └── manifest.json      # Plugin manifest v3.3.6
.claude/
  ├── lean-ctx/          # Claude-specific rules
  └── rules/             # Additional rule files
bin/                     # Helper scripts
benchmark/               # Benchmark tooling
cookbook/                # Usage recipes

CLI Binary

lean-ctx — own Rust runtime, not a wrapper.

Key subcommands:

  • lean-ctx setup — install shell hooks + editor wiring
  • lean-ctx -c <cmd> — run command with compression
  • lean-ctx read <file> -m <mode> — compressed file read
  • lean-ctx gain --live — real-time token savings
  • lean-ctx gain --wrapped — weekly/monthly savings summary
  • lean-ctx serve — HTTP MCP endpoint
  • lean-ctx pack [--pr] — context package builder
  • lean-ctx doctor — diagnostic tool
  • lean-ctx benchmark report <path> — compression benchmark

State Files

  • ~/.config/lean-ctx/config.toml — global config
  • .lean-ctx.toml — per-project overrides (auto-merged)
  • .lean-ctx-id — unique project ID (for Docker/multi-workspace)
  • CCP storage: unknown path (local session memory)

Required Runtime

None (statically linked Rust binary).

Target AI Tools

Cursor, Claude Code, GitHub Copilot, Windsurf, Codex, Gemini, and 23+ other agents. Two integration modes:

  • Hybrid: MCP + shell hooks (agents with shell access)
  • MCP only: protocol-only agents (JetBrains, VS Code, Zed)
03

Components

lean-ctx — Components

MCP Tools (62)

Categories from README:

Core Read/Search (10 modes) ctx_read(path, mode) — 10 modes: auto, full, map, signatures, diff, aggressive, entropy, task, reference, lines:N-M

Shell/Command Tools ctx_shell(command) — pattern compression for 60+ shell tools ctx_search(pattern, path) — token-efficient grep ctx_tree(path, depth) — compact directory maps ctx_edit(path, old, new) — search-and-replace without Read

Memory (CCP) ctx_compress — compress current context when large ctx_overview(task) — session overview at start

Search & Intelligence Hybrid BM25 + embeddings + graph proximity (RRF-ranked)

LSP Refactoring ctx_refactor — language-server powered rename, go-to-def, find-references

Multi-Agent ctx_agent — start/manage agent run ctx_handoff — transfer context to another agent

Cryptographic Proofs ctx_proof — create context proof ctx_verify — verify context integrity

Observability ctx_expand — FTS5 cross-archive search Context Manager dashboard tools

Other notable ctx_overview, archive search, PR context packs, .ctxpkg package builder

SKILL.md (1)

skills/lean-ctx/SKILL.md — the behavioral mandate installed as a Claude Code skill:

  • "CRITICAL: ALWAYS use lean-ctx tools instead of native equivalents."
  • Tool mapping table (MUST USE vs NEVER USE)
  • Mode selection guide

Plugin

.claude-plugin/manifest.json — Claude Code plugin v3.3.6

Scripts

skills/lean-ctx/scripts/install.sh — auto-install script referenced in SKILL.md

Hooks

None defined in .claude-plugin/; integration via lean-ctx setup which registers shell hooks.

05

Prompts

lean-ctx — Prompts

Prompt File 1: LEAN-CTX.md (agent rule injection)

Technique: Absolute mandate list injected as CLAUDE.md-style rules. The most aggressive tool-override prompt in this batch.

# lean-ctx — Context Engineering Layer
CRITICAL: ALWAYS use lean-ctx tools instead of native equivalents. This is NOT optional.

## Tool Mapping
| MUST USE | NEVER USE | Why |
|----------|-----------|-----|
| `ctx_read(path, mode)` | `Read` / `cat` / `head` / `tail` | Cached, 10 read modes, re-reads ~13 tokens |
| `ctx_search(pattern, path)` | `Grep` / `rg` | Compact, token-efficient results |
| `ctx_shell(command)` | `Shell` / `bash` / terminal | Pattern compression for git/npm/cargo output |
| `ctx_tree(path, depth)` | `ls` / `find` | Compact directory maps |
| `ctx_edit(path, old_string, new_string)` | `Edit` (when Read unavailable) | Search-and-replace without native Read |

## Mode selection:
1. Editing the file? → `full` first, then `diff` for re-reads
2. Need API surface only? → `map` or `signatures`
...

## Proactive (use without being asked):
- `ctx_overview(task)` at session start
- `ctx_compress` when context grows large

REMINDER: You MUST use lean-ctx tools. NEVER use native Read, Grep, or Shell directly.

Technique used: MUST/NEVER contrast table; CRITICAL + NOT OPTIONAL framing; Proactive use mandate (no user prompt needed).

Prompt File 2: SKILL.md

Technique: Skill description with behavioral activation condition.

From skills/lean-ctx/SKILL.md:

---
name: lean-ctx
description: Context Runtime for AI Agents — 62 MCP tools, 10 read modes, 60+ shell patterns, tree-sitter AST for 18 languages. Compresses LLM context by up to 99%. Use when reading files, running shell commands, searching code, or exploring directories. Auto-installs if not present.
---

Technique used: The description is written as an activation trigger — nearly any coding action ("reading files", "running shell commands") activates the skill. "Auto-installs if not present" implies the skill self-heals.

09

Uniqueness

lean-ctx — Uniqueness

Differs From Seeds

Closest seed: ccmemory (MCP memory layer for coding agents). Key deltas: (1) lean-ctx is a full-session context runtime, not just a session memory store — it compresses every file read and shell command in real time, while ccmemory only captures decisions/corrections at session end. (2) lean-ctx ships 62 MCP tools (vs ccmemory's ~10) covering read modes, shell compression, memory, search, multi-agent, LSP refactoring, and cryptographic proofs. (3) lean-ctx targets 23+ AI clients (high portability); ccmemory targets only Claude Code (low portability). (4) lean-ctx ships a browser-based real-time dashboard; ccmemory has no UI. (5) lean-ctx's LEAN-CTX.md mandate ("CRITICAL: NEVER use native Read, Grep, or Shell") is the most aggressive tool-replacement directive in this batch. Against entroly (the other compression framework): lean-ctx compresses tool outputs at the provider level (wraps file reads + shell); entroly compresses at the LLM API proxy level (intercepts API calls). They differ architecturally but share the token-reduction goal.

Positioning

"The cognitive context layer that governs every token between code and AI — compression + memory + governance + observability, all in one zero-dependency Rust binary."

Unique Features in Batch

  1. Reproducible benchmarks: lean-ctx benchmark report . generates committed benchmark data with language-by-language savings and quality scores
  2. Cryptographic context proofs: ctx_proof/ctx_verify for audit trails — unique in this batch
  3. Browser dashboard: real-time token tracking and cost visibility — unique in this batch
  4. 10 read modes: granularity from lines:N-M to signatures to aggressive — unique level of per-file control
  5. Shell pattern compression: 60+ named patterns for specific CLI tools — not present in any other framework in this batch

Observable Failure Modes

  1. LEAN-CTX.md mandate rigidity — "NEVER use native Read" can cause issues when ctx_read fails for a binary file or unsupported format; the SKILL.md fallback instruction ("fall back only if unavailable") may not be clear enough.
  2. 62-tool context overhead — injecting 62 tool descriptions into Claude's context window uses ~5K tokens before any work; lean-ctx's own instruction to use itself consumes the savings it provides at scale.
  3. Quality metric ambiguity — BENCHMARKS.md shows aggressive mode: "4.1% savings / 100% quality" — contradicts the README's "aggressive = maximum compression" claim.
  4. CCP internals opaque — the exact storage format for CCP is not documented publicly; upgrading lean-ctx may break CCP continuity.
  5. Property Graph not externally queryable — the knowledge graph powers search ranking but doesn't expose raw graph queries to agents.
04

Workflow

lean-ctx — Workflow

Phases

Phase What Happens Artifact
Install `curl ... shlean-ctx setup`
Session Start SKILL.md or LEAN-CTX.md tells agent to call ctx_overview(task) Session overview in context
File Read Agent calls ctx_read(file, mode) instead of native Read Compressed file content (~5-99% savings)
Shell Agent calls ctx_shell("git status") instead of Bash Compressed output (~60-99% savings)
Search Agent calls ctx_search(pattern) instead of Grep Token-efficient results
Compress When context grows large, ctx_compress Reduced context
End of Session CCP saves task/facts/decisions for next session Cross-session memory
Observability lean-ctx gain --live shows real-time savings Token + USD savings dashboard

Mode Selection Logic (from SKILL.md)

  1. Editing the file? → full first, then diff for re-reads
  2. Need API surface only? → map or signatures
  3. Large file, context only? → entropy or aggressive
  4. Specific lines? → lines:N-M
  5. Active task set? → task (IB-filtered)
  6. Unsure? → auto

Anti-pattern from LEAN-CTX.md: "NEVER use full for files you won't edit."

Approval Gates

None.

Spec Format

None.

PR Context Packs

lean-ctx pack --pr builds a PR-ready context pack (changed files, related tests, impact, artifacts). Portable .ctxpkg files with SHA-256 integrity.

06

Memory Context

lean-ctx — Memory & Context

Memory Model

hybrid — file-based session memory (CCP) + in-process caching for file reads + optional knowledge graph with vector embeddings for semantic search.

Persistence Scope

project for CCP (task/facts/decisions per chat session); global for the cached file read store (cross-session re-reads cost ~13 tokens).

Three Memory Components

1. File Read Cache

  • Cached file reads persist across tool calls
  • Re-reads of the same file: ~13 tokens (vs ~2000 raw)
  • Token savings: 99.6% on cache hit (from BENCHMARKS.md)

2. CCP (Context-Carry Protocol)

  • Persists task/facts/decisions across chat sessions
  • Structured recovery queries survive context compaction
  • Session memory: "what I was doing, where I stopped, what decisions I made"
  • Enables cross-chat long-running task continuity

3. Knowledge Graph

  • Temporal facts with validity windows
  • Episodic + procedural memory
  • Property Graph: multi-edge code graph (imports, calls, exports, type_ref)
  • Powers impact analysis and search ranking

Context Compaction Handling

yes — explicit: CCP uses "structured recovery queries survive compaction" by design. lean-ctx setup installs shell hooks that activate across all sessions including after compaction.

Token Reduction Claims (verbatim from README)

  • "Repeated file reads: ~2000 tokens each → Cached re-reads: ~13 tokens"
  • "Raw git status: ~800 tokens → Compressed: ~120 tokens"
  • "60-99% compression on shell output"
  • "up to 99%"

From BENCHMARKS.md (self-measured, lean-ctx benchmark report .):

  • map mode: 96.0% savings, 3.5ms latency, 92.0% quality
  • signatures mode: 95.7% savings
  • cache_hit: 99.6% savings, 0μs latency
  • Session simulation: 83.6% savings (no CCP), 84.7% (with CCP)

Search Mechanism

hybrid — BM25 + vector embeddings + graph proximity, combined via RRF (Reciprocal Rank Fusion). Archive full-text search via FTS5.

Context Proofs

ctx_proof / ctx_verify — cryptographic proofs with 4-layer verification engine. A context bundle can be hashed and verified, providing an audit trail of what was in context when a decision was made.

State Files

  • ~/.config/lean-ctx/config.toml — config
  • .lean-ctx.toml — per-project overrides
  • CCP storage: implementation-internal (Rust binary manages)
  • Knowledge graph: implementation-internal (SQLite or similar)
07

Orchestration

lean-ctx — Orchestration

Multi-Agent

yesctx_agent + ctx_handoff with context transfer bundles, diary system, and synchronized shared state. PR: "Multi-Agent: agent handoff with context transfer bundles, diary system, synchronized shared state."

Orchestration Pattern

parallel-fan-out (via ctx_agent delegation) + sequential (single-agent with CCP). The handoff pattern enables a pipeline: agent A works → packages context bundle → hands off to agent B.

Isolation Mechanism

none — all agents share the same lean-ctx context store. Isolation is logical (per-project config, .lean-ctx.toml).

Execution Mode

event-driven — shell hooks fire on every command; MCP tools on agent demand; lean-ctx gain --live streams real-time data.

Multi-Model

No explicit model routing, but targets 23+ AI clients across model families.

Context Compaction Handling

yes — CCP designed for compaction resilience; structured recovery queries survive.

Crash Recovery

yes — CCP can resume tasks across session crashes.

Auto Validators

None — lean-ctx is a context compression layer, not a code quality gate.

Prompt Chaining

yes — CCP is a form of prompt chaining: one session's decisions and facts become the context prompt for the next session.

LSP Refactoring

ctx_refactor uses language server protocol to perform reliable renames and navigation: rust-analyzer, typescript-language-server, pylsp, gopls supported. This is orchestration of external language servers from within an AI agent session.

08

Ui Cli Surface

lean-ctx — UI & CLI Surface

CLI Binary

  • Name: lean-ctx
  • Type: Own runtime (Rust binary)
  • Key subcommands: setup, serve, -c <cmd>, read, gain, gain --live, gain --wrapped, pack, pack --pr, doctor, benchmark report, doctor --json, watch, update

Local UI (Browser Dashboard)

  • Type: Web dashboard (Context Manager)
  • Launch: lean-ctx serve opens browser automatically when running
  • Features: real-time token tracking, compression stats, utilization gauge, budgets/SLOs, per-agent context breakdown, live savings counter (lean-ctx gain --live)
  • Port: unknown (implementation-internal; auto-opened)

Terminal Monitoring

  • lean-ctx gain --live — real-time streaming savings in terminal
  • lean-ctx watch — TUI monitoring of context activity
  • lean-ctx wrapped --week/--month — periodic savings summary

Observability Tools

  • lean-ctx doctor — diagnostic tool
  • lean-ctx doctor --json — shareable diagnostic output
  • lean-ctx benchmark report <path> — reproducible compression measurement by language
  • Context Manager dashboard — browser-based real-time visibility

IDE Integration

Via MCP protocol (any MCP client). Additional shell hook layer for agents with shell access. SKILL.md installable via Claude Code plugin marketplace.

Transport

stdio (default MCP) + Streamable HTTP (lean-ctx serve/v1/tools/call). HTTP mode used by Cookbook recipes and SDK.

Related frameworks

same archetype · same primary tool · same memory type

Context Mode ★ 16k

Keeps raw tool output data out of the context window via sandbox execution and SQLite+FTS5 session indexing, reducing context…

Nemp Memory ★ 101

Persists AI agent context across sessions as 100%-local plain JSON files with zero dependencies, zero cloud, and agent identity…

CogniLayer v4 ★ 28

Provides AI coding agents with typed semantic memory, tree-sitter code intelligence, and a multi-agent coordination protocol to…

cursor-coding-agent-os (Mugiwara555343) ★ 3

Lean/Verbose dual-mode Agent OS fork for solo developers on token budgets.

rtk (Real Token Killer) ★ 55k

Intercepts Claude Code's Bash tool calls at the PreToolUse hook and compresses verbose CLI output (git status, test runners,…

Code-Mode Library ★ 1.5k

Replaces traditional tool-calling with TypeScript code execution in a sandbox, collapsing N sequential tool calls into 1 code…