Cognitive Context Layer

The system that decides
what your AI sees.

The cognitive context layer between your AI and your codeOne local binary between your AI and your code. LeanCTX perceives, compresses, remembers, routes, and governs the complete lifecycle of AI context, from file reads to verified outputs.

Cognitive Context Layer

What is a Cognitive Context Layer?

A Cognitive Context Layer is the infrastructure between your AI tools and your codebase. It controls what files are read, how output is compressed, what knowledge persists across sessions, and whether results meet quality standards before delivery.

AI Agent
LeanCTX Cognitive Context Layer
I/O Intelligence Memory Verify
Your Code & Tools
System Blueprint

The construction drawing

The process topology of the shipped binary: seven entry points, one runtime, one set of local stores. Every box below maps to a real module, port or file on disk.

Data Flow

What happens to a single read

Sheet 2 traces one request through the runtime, stage by stage, including the cache short-circuit that makes repeated reads nearly free. The shell path runs in parallel with the same accounting.

Specifications

Engineering data sheet

The reference tables behind the drawings: every surface with its transport and lifecycle, the on-disk layout, the adaptive-learning layers, and the security boundaries the runtime enforces.

AProcess model

All surfaces are the same binary in different roles. Nothing requires a cloud connection; everything binds local-first.

REF SURFACE TRANSPORT ENDPOINT LIFECYCLE COMMAND
01 MCP server (stdio) JSON-RPC over stdin/stdout spawned per editor session child process of the editor lean-ctx
02 MCP server (HTTP) MCP Streamable HTTP localhost, configurable --host/--port foreground or service lean-ctx serve
03 IPC daemon Unix Domain Socket OS data dir, e.g. ~/Library/Application Support/lean-ctx/daemon.sock launchd / systemd autostart lean-ctx serve --daemon
04 Shell hook process exec, compressed stdout wraps IDE bash calls + interactive shells per command lean-ctx -c "<cmd>"
05 API proxy HTTP (LLM API pass-through) localhost:4444 (default) on demand lean-ctx proxy start
06 Web dashboard HTTP + bearer token localhost:3333 (default, --port) on demand lean-ctx dashboard
07 Terminal UI TTY (in-place redraw) live event stream / 1 s refresh interactive lean-ctx watch · gain --live

BStorage layout — ~/.lean-ctx

Persistent state is plain files in your home directory: inspectable, exportable, deletable. No hidden databases outside this folder.

ARTIFACT FORM PURPOSE
config.toml TOML Single config file — integration mode, compression, providers, opt-outs
cache/ content-addressed Session file cache; unchanged re-reads collapse to ~13-token stubs
bm25 index inverted index Lexical search over code chunks + provider documents
context_graph/ property graph Imports, calls, types across files and repos — powers map mode + deep queries
knowledge SQLite Persistent facts, decisions, rooms — recalled across sessions (CCP)
savings ledger append-only JSONL Every compression event; Ed25519-signable for audit
litm_calibration.json JSON Learned context-position hit rates (lost-in-the-middle calibration)
events.jsonl event stream Live feed consumed by watch, dashboard and efficacy reports

CAdaptive-learning layers

Seven online-learning mechanisms tune compression to your real usage, locally, from quality signals like bounces and edit failures. Deep dive: Adaptive Learning →

  • L1
    Adaptive thresholds Online-learned compression aggressiveness from quality signals (bounces, edit failures, clean runs)
  • L2
    LITM calibration Empirical placement of critical context at positions the model actually attends to
  • L3
    Stigmergic scent field Multi-agent coordination via decaying markers: claimed, done, stuck, hot, avoid
  • L4
    Delta playbook Incremental checkpoint snapshots that survive context compaction
  • L5
    Query-conditioned IB Information-Bottleneck compression fused with query relevance
  • L6
    Theta-gamma chunking Wakeup facts grouped in attention-friendly bursts
  • L7
    Semantic dedup Likelihood-scored redundancy filtering across the session

DSecurity boundaries

Hard guarantees enforced in the runtime. Security model →

  • PathJail Every file access is canonicalised and confined to the workspace root
  • Shell allowlist Deny-by-default command policy for agent-issued shell executions
  • Local-first All processing on-device; dashboard binds to localhost and requires a bearer token
  • Signed evidence Savings ledger entries are Ed25519-signable and batch-verifiable
Integration Modes

One binary. Three ways in.

LeanCTX automatically selects the optimal integration mode for each agent: CLI-Redirect drives the LeanCTX CLI through editor rules with zero MCP overhead, Hybrid combines MCP cached reads with shell compression hooks, and Full MCP provides maximum tool access for protocol-only editors.

CLI-Redirect
For rules-driven and terminal-first agents, plus CI
Editor rules route every read, search and shell call through the CLI. No MCP server, no schema overhead.
lean-ctx -c / read / grep
Hybrid
Default for Cursor, Claude Code, Codex, Windsurf, and 20+ agents
MCP for cached reads (13 tokens), CLI for shell commands and searches, best of both worlds.
MCP cache + CLI shell/search
Full MCP
For JetBrains, VS Code, Neovim, Emacs, Zed
All 76 tools via MCP protocol with lazy tool set, ideal for agents that require MCP.
76 tools via MCP + lazy tool set

Either way, LeanCTX picks the right mode for your editor, automatically. See all 30+ supported tools

Background Daemon

Always on. Always yours.

A small background service keeps your session warm, so cache hits are instant and memory is always there. It starts automatically during setup, restarts itself when you update, and cleans up after itself, nothing to manage.

lean-ctx serve --status
$ lean-ctx serve --status
Daemon running (PID 4139)
Endpoint: ~/Library/Application Support/lean-ctx/daemon.sock (ready)
PID file: ~/Library/Application Support/lean-ctx/daemon.pid
# autostart on login:
$ lean-ctx daemon enable # launchd / systemd
Verification

Every output carries proof

LeanCTX generates proof artifacts for every session: which files were read, what was compressed, which checks passed, and how tokens were spent. This makes AI work auditable, replayable, and trustworthy.

The one-paragraph definition

LeanCTX (short for Lean Context) is the open-source context intelligence layer for AI agents. One local Rust binary decides what agents read (10 read modes, 60–90% fewer tokens, ~13-token cached re-reads), remembers what they learn (persistent sessions, knowledge graph), guards what they touch (PathJail, secret redaction, budgets, injection detection) and proves what they save (Ed25519-signed ledger, reproducible benchmark). Compression is one of five subsystems, and every original stays locally retrievable. Works with 30+ AI coding tools via MCP and shell hooks; embeds in any agent via a versioned /v1 API with Python, TypeScript and Rust SDKs. Local use is free forever, enforced by CI.

Read the full story

Take back control of your context.

One local binary between your AI and your code. LeanCTX perceives, compresses, remembers, routes, and governs the complete lifecycle of AI context, from file reads to verified outputs.