Documentation

Configuration

Configuration options, environment variables, and editor setup.

LeanCTX works out of the box with zero configuration. All settings below are optional - defaults are tuned for typical AI-assisted development workflows.


Config File

LeanCTX supports an optional TOML configuration file at ~/.lean-ctx/config.toml.

Create the default config

lean-ctx config init

View current configuration

lean-ctx config

Set individual values

lean-ctx config set ultra_compact true
lean-ctx config set checkpoint_interval 15
lean-ctx config set tee_mode always
lean-ctx config set passthrough_urls localhost:3000,api.internal

Validate Your Config

# Check for typos, unknown keys, and wrong field names
lean-ctx config validate

# View the full config schema (all keys, types, defaults)
lean-ctx config schema

Configuration Priority

When the same setting is defined in multiple places, lean-ctx uses a clear priority order. Higher-priority sources override lower ones:

PrioritySourceExample
1 (highest)Environment variableLEAN_CTX_COMPRESSION=max
2Project-local config.lean-ctx.toml
3 (lowest)Global config~/.lean-ctx/config.toml

Within a single config file, TOML is a key-value format — there is no line-order priority. If you define the same key twice, the TOML parser will use the last occurrence, but this is considered a syntax error. Use lean-ctx config validate to catch such issues.


Common Confusion: "max" Parameters

lean-ctx has several parameters with "max" in their name that control completely different things. This table clarifies which parameter controls what:

ParameterWhat it controlsDefaultSection
graph_index_max_files Graph file scan limit — how many files the JSON graph index scans for dependency analysis. This is what lean-ctx gain shows as "Graph: N files".5000 root
memory.knowledge.max_facts Knowledge facts — how many learned facts (from conversations) are stored per project. Has nothing to do with the graph.200 [memory.knowledge]
memory.embeddings.max_facts Embedding index — how many facts are indexed in the vector store for semantic search. Separate from knowledge facts.2000 [memory.embeddings]
archive.max_disk_mb Archive storage — maximum disk space for zero-loss compression archives. Unrelated to memory or graph.500 [archive]
archive.max_age_hours Archive retention — how long archived tool results are kept before cleanup.48 [archive]
memory.lifecycle.stale_days Knowledge staleness — facts not accessed for this many days become eligible for pruning during consolidation. Does not delete instantly — facts are pruned only when auto_consolidate runs.30 [memory.lifecycle]
Example: If lean-ctx gain shows "Graph: 2000 files (capped)" and you want more coverage, increase graph_index_max_files — not max_facts. max_facts controls knowledge memory, which is a completely separate system.

Config Options

KeyTypeDefaultDescription
ultra_compact bool false Enable ultra-compact output mode
compression_level string "off" Unified compression level controlling the 4-layer terse engine. "off" = no compression, "lite" = abbreviations only, "standard" = balanced compression, "max" = maximum token savings. Replaces the legacy output_density and terse_agent settings. See Compression Level.
minimal_overhead bool false Reduce per-call token overhead to the absolute minimum. Disables auto-checkpoints, meta-strings (savings notes, stale notes, shell hints), and session/knowledge blocks in MCP instructions. Recommended for Codex, CI pipelines, and cost-sensitive environments. Also available via LEAN_CTX_MINIMAL=1. See Minimal Overhead.
output_density string "normal" Legacy — superseded by compression_level. Values: "normal" / "terse" / "ultra". Also available via LEAN_CTX_OUTPUT_DENSITY. See Output Density.
tee_mode string "failures" When to save full output to ~/.lean-ctx/tee/. Values: always, failures, never. Backward-compatible: tee_on_error: true maps to failures.
passthrough_urls list [] URL fragments for curl passthrough. Curl commands targeting these URLs skip JSON compression and return full responses.
checkpoint_interval u32 15 Auto-checkpoint every N tool calls (0 = disabled). Overridden by LEAN_CTX_CHECKPOINT_INTERVAL env var.
excluded_commands list [] Commands to skip compression for
custom_aliases list [] Define custom shell command aliases for ctx_shell. Each entry maps a short name to a full command. See Custom Aliases for examples.
path_jail bool true Enable/disable PathJail sandbox. Set to false to allow file operations outside the project root. Useful in Docker containers or monorepos with external mounts. Also available via LEAN_CTX_NO_JAIL=1. See Security: PathJail. v3.6.7
redirect_exclude list [] Glob patterns for files that should bypass the hook redirect and go straight to the native Read tool. Useful for config files or files you don't want compressed. Example: [".wolf/**", ".claude/**", "*.json", "CLAUDE.md"]. Also configurable via LEAN_CTX_HOOK_EXCLUDE.
content_defined_chunking bool false Enable Rabin-Karp content-defined chunking for ctx_read. Creates stable chunk boundaries based on content hashes, improving LLM prompt cache hit rates across edits. Opt-in: enable when working with large files that change frequently.
disabled_tools list [] Exclude tools from the MCP tool list to reduce token overhead. Example: ["ctx_benchmark", "ctx_metrics", "ctx_wrapped"]. Also available via LEAN_CTX_DISABLED_TOOLS env var (comma-separated).
terse_agent string "off" Legacy — superseded by compression_level. Still accepted for backward compatibility ("off"/"lite"/"full"/"ultra" and true/false). If both are set, compression_level takes precedence.
rules_scope string "both" Controls where lean-ctx injects its cursor rules. "global" = only ~/.cursor/rules/, "project" = only .cursor/rules/ in the project, "both" = both locations. See Rules Scope.
slow_command_threshold_ms u64 5000 Commands running longer than this threshold (in milliseconds) are flagged in ctx_shell output with a timing warning. Set to 0 to disable slow command detection entirely.
buddy_enabled bool true Enable buddy mode for collaborative multi-agent workflows. See Buddy Mode.
extra_ignore_patterns list [] Additional glob patterns to exclude from ctx_tree, ctx_search, and file discovery. These are merged with the built-in ignore list (node_modules, .git, etc.). Example: ["*.generated.ts", "vendor/**", "dist/**"]
theme string "default" Visual theme for CLI and dashboard output.
memory_profile string "balanced" Controls memory subsystem behavior. "low" reduces memory usage at the cost of recall. "balanced" (default) optimizes for typical workflows. "performance" retains more context for maximum recall. Also available via LEAN_CTX_MEMORY_PROFILE.
bm25_max_cache_mb u64 128 Maximum size in MB for the BM25 search index cache. Higher values improve search performance for large codebases.
allow_paths list [] Additional paths outside the project root that are allowed through the PathJail sandbox. Use for monorepo shared directories or external dependency sources.
shell_hook_disabled bool false Disable the shell hook entirely. When true, lean-ctx will not intercept shell commands for compression.
update_check_disabled bool false Disable automatic version update checks. When true, lean-ctx will not check for new releases on startup.
graph_index_max_files u64 5000 Maximum files scanned by the JSON graph index fallback. Increase for large monorepos. When the cap is reached, lean-ctx gain shows "(capped)" and a warning is emitted.
sandbox_level u8 0 Sandbox level for code execution. 0 = subprocess isolation only, 1 = OS-level sandboxing (Seatbelt on macOS, Landlock on Linux). Also available via LEAN_CTX_SANDBOX_LEVEL.
shell_allowlist list [] When non-empty, only listed commands are permitted by ctx_shell. All other commands are rejected. Also available via LEAN_CTX_SHELL_ALLOWLIST (comma-separated).
allow_auto_reroot bool false Allow automatic project root re-rooting. When false, absolute paths outside the PathJail are rejected. Also available via LEAN_CTX_ALLOW_REROOT.
agent_token_budget usize 0 Default per-agent token budget. 0 = unlimited. Override per-agent via ctx_session.
reference_results bool false When true, large tool outputs (>4000 chars) are stored as references and a URI is returned instead of the full content. Also available via LEAN_CTX_REFERENCE_RESULTS.
[archive] table - Configure zero-loss compression. Full tool results are saved to disk; the agent retrieves them on demand via ctx_expand. See Tool Result Archive.
[cloud] table - Cloud integration settings. See Cloud.
[proxy] table - Upstream API proxy overrides. See Proxy.
[memory] table - Memory subsystem configuration with sub-tables for knowledge, episodic, procedural, lifecycle, gotcha, and embeddings. See Memory.
[secret_detection] table - Regex-based secret detection in tool outputs. See Secret Detection.
[boundary_policy] table - Cross-project access control and audit policy. See Boundary Policy.
bypass_hints string "gentle" Controls bypass detection hints when agents use native Read/Grep instead of lean-ctx tools. Values: "gentle" (reminder after cooldown), "firm" (immediate reminder), "off" (disabled). Also available via LEAN_CTX_BYPASS_HINTS.
[loop_detection] table - Configure loop detection thresholds. See Loop Detection section below.
memory_cleanup string "aggressive" Controls how aggressively lean-ctx frees memory when idle. "aggressive" (default) clears caches after 5 minutes of idle time — ideal for single-IDE use. "shared" retains caches for 30 minutes — ideal when multiple IDEs or models share the same lean-ctx instance. Also available via LEAN_CTX_MEMORY_CLEANUP env var.
shell_activation string "always" Controls when lean-ctx shell aliases auto-activate. "always" (default): active in every interactive shell. "agents-only": only active when AI agent env vars are detected (LEAN_CTX_AGENT, CLAUDECODE, CODEX_CLI_SESSION, GEMINI_SESSION). "off": never auto-activate, user must call lean-ctx-on manually. Also available via LEAN_CTX_SHELL_ACTIVATION env var. See Shell Activation.
max_ram_percent u8 5 Maximum percentage of system RAM that lean-ctx may use before the RAM Guardian triggers adaptive cache eviction. Range: 1–50. Also available via LEAN_CTX_MAX_RAM_PERCENT env var.
savings_footer string "auto" Controls visibility of token savings footers (e.g. [42 tok saved (30%)]). "auto" (default): shown in CLI, suppressed in MCP/agent context. "always": always shown. "never": never shown. Also available via LEAN_CTX_SAVINGS_FOOTER env var.

Environment Variables

VariableValuesDescription
LEAN_CTX_CRP_MODE tdd (default), compact, off Controls Token Dense Dialect compression level
LEAN_CTX_OUTPUT_DENSITY normal (default), terse, ultra Override the compression level without editing config. Maps to CompressionLevel (Off/Lite/Standard/Max). Applies to all MCP tool outputs (and most tool footers).
LEAN_CTX_MINIMAL 1 / unset Enable minimal overhead mode. Disables auto-checkpoints, meta-strings, and session blocks in instructions. Equivalent to minimal_overhead = true in config.
LEAN_CTX_NO_CHECKPOINT 1 / unset Disable auto-checkpoint injection without affecting other meta-strings. More targeted than LEAN_CTX_MINIMAL which disables everything.
LEAN_CTX_CHECKPOINT_INTERVAL number (default: 10) Auto-checkpoint every N tool calls (0 = disabled)
LEAN_CTX_CACHE_TTL number (default: 300) Seconds of inactivity before session cache auto-clears (0 = disabled)
LEAN_CTX_MEMORY_CLEANUP aggressive (default) / shared Controls idle memory cleanup aggressiveness. aggressive: clear caches after 5 min idle (single-IDE). shared: retain caches 30 min (multi-IDE/multi-model). Overrides memory_cleanup in config.toml. If LEAN_CTX_CACHE_TTL is also set, it takes precedence for the cache TTL value.
LEAN_CTX_SHELL_ACTIVATION always (default) / agents-only / off Override shell_activation config setting. Controls when lean-ctx shell aliases auto-activate. always: active in every interactive shell. agents-only: only when AI agent env vars detected. off: manual activation via lean-ctx-on. See Shell Activation.
LEAN_CTX_MAX_RAM_PERCENT number (default: 5) Maximum percentage of system RAM lean-ctx may use before the RAM Guardian triggers adaptive cache eviction. Range: 1–50. Overrides max_ram_percent in config.toml.
LEAN_CTX_SAVINGS_FOOTER auto (default) / always / never Controls visibility of token savings footers. auto: shown in CLI, suppressed in MCP/agent context. always: always shown. never: never shown. Overrides savings_footer in config.toml.
LEAN_CTX_SANDBOX_LEVEL 0 (default) / 1 Override sandbox_level config. 0 = subprocess only, 1 = OS-level sandboxing (Seatbelt on macOS, Landlock on Linux).
LEAN_CTX_SHELL_ALLOWLIST comma-separated commands Override shell_allowlist config. When set, only listed commands are permitted by ctx_shell. Example: git,cargo,npm,ls
LEAN_CTX_ALLOW_REROOT true / false Override allow_auto_reroot config. When false (default), absolute paths outside the PathJail are rejected.
LEAN_CTX_REFERENCE_RESULTS true / false Override reference_results config. When true, large tool outputs (>4000 chars) are stored as references with a URI returned instead.
LEAN_CTX_ACTIVE set / unset Recursion guard. When set, lean-ctx passes commands through without compression. Set automatically by the shell hook.
LEAN_CTX_DISABLED set / unset Master kill-switch. Bypasses ALL compression in both shell hook and MCP server. Everything passes through unmodified.
LEAN_CTX_RAW set / unset Skip compression for the current command. Same effect as --raw flag or raw=true MCP parameter.
LEAN_CTX_AUTONOMY true / false Enable/disable autonomous intelligence features (auto-preload, auto-dedup, related hints). Default: true.
LEAN_CTX_HOOK_EXCLUDE comma-separated globs Comma-separated glob patterns for hook redirect exclusion. Takes precedence over redirect_exclude in config.toml. Example: .wolf/**,.claude/**,*.json
LEAN_CTX_DISABLED_TOOLS comma-separated tool names Exclude specific tools from the MCP tool list. Overrides disabled_tools in config.toml when set. Example: ctx_benchmark,ctx_metrics,ctx_wrapped
LEAN_CTX_BYPASS_HINTS gentle (default) / firm / off Controls bypass detection hints. When agents use native Read/Grep instead of lean-ctx tools, a reminder is appended to the next response. gentle: reminder with cooldown (every 5 calls). firm: immediate reminder on every detection. off: disabled. Overrides bypass_hints in config.toml.
LEAN_CTX_HARDEN 1 / unset Activate strict enforcement mode. When set, hook redirects deny native tool usage entirely and return an error message directing the agent to use lean-ctx tools. Set automatically by lean-ctx harden.
LEAN_CTX_SHELL path to shell binary Override which shell lean-ctx uses for ctx_shell / lean-ctx -c. Useful on Windows when PowerShell detection fails. Example: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
LEAN_CTX_COMPRESSION off / lite / standard / max Override compression_level config setting. Controls the 4-layer terse engine for unified input/output compression. See Compression Level.
LEAN_CTX_TERSE_AGENT true / false Legacy. Override terse_agent config. Mapped to compression_level internally. See Compression Level.
LEAN_CTX_ARCHIVE true / false Override archive.enabled config. Enable zero-loss compression via the Tool Result Archive.
LEAN_CTX_ARCHIVE_THRESHOLD number (default: 4096) Minimum character count before a tool result is archived. Override for archive.threshold_chars.
LEAN_CTX_HEADLESS set / unset Start MCP server in headless mode - skip all auto-setup (rules injection, hook installation, CLAUDE.md modification, agent registry). See Headless MCP Mode.
LEAN_CTX_MODELS_DIR path Override where embedding models are stored/downloaded (default: ~/.lean-ctx/models). Useful for containers and CI.
LEAN_CTX_DATA_DIR path (default: ~/.lean-ctx) Override the base directory for all lean-ctx persistent data (stats, knowledge, agents, cache, sessions). All modules resolve paths through this single env var. Useful for custom data locations or containerized environments.
LEAN_CTX_NO_JAIL 1 / true Disable PathJail sandbox for the current session. Useful in Docker/Podman containers or monorepos with external mounts. See PathJail Sandbox. v3.6.7
LCTX_MAX_READ_BYTES number (default: 5242880) Maximum file size in bytes that ctx_read / ctx_edit will process. Files exceeding this limit are rejected with a clear error. Prevents accidental ingestion of very large files. Default: 5 MB.
LCTX_MAX_SHELL_BYTES number (default: 1048576) Maximum shell output capture size in bytes for ctx_shell. Output exceeding this limit is truncated with a [truncated] marker. Default: 1 MB.

Hook Redirect Exclusion

The lean-ctx hook redirect command intercepts native Read/Grep/ListFiles tool calls and redirects them to lean-ctx MCP tools. To exclude specific paths from this redirect (e.g. for Edit workflows or framework protocol files), use the redirect_exclude config option or the LEAN_CTX_HOOK_EXCLUDE environment variable.

# ~/.lean-ctx/config.toml
redirect_exclude = [".wolf/**", ".claude/**", "*.json", "CLAUDE.md"]
# Or via environment variable (takes precedence)
export LEAN_CTX_HOOK_EXCLUDE=".wolf/**,.claude/**,*.json"

Agent Integration Modes

lean-ctx init --agent <tool> supports two integration modes: hybrid (MCP reads + shell hooks, recommended) and mcp (explicit ctx_* tools only). auto selects the best default per tool (Hybrid for agents with shell access, MCP for others).

# Auto (recommended — selects Hybrid for most agents)
lean-ctx init --agent cursor

# Force MCP tools only
lean-ctx init --agent cursor --mode mcp

# Force Hybrid (MCP reads + shell compression)
lean-ctx init --agent cursor --mode hybrid

Binary File Passthrough (v2.21.0)

The hook redirect automatically detects binary files (images, PDFs, archives, fonts, videos, compiled files) by extension and passes them through to the native Read tool. This ensures AI agents can still view screenshots, images, and other non-text files without interference.


Cursor Rule

For maximum token savings, add a .cursor/rules/lean-ctx.mdc to your project. This instructs the LLM to prefer LeanCTX tools and use compact output patterns. An example is included in examples/lean-ctx.mdc in the repository.

The Cursor rule includes CRP v2 (Compact Response Protocol) which can reduce both output tokens (50–80%) and thinking tokens (30–60%) through structured task parsing and one-hypothesis reasoning. These ranges are per-response estimates and depend on the model and task complexity.


Autonomy Configuration

The autonomous intelligence layer runs optimization pipelines automatically. Configure in ~/.lean-ctx/config.toml:

[autonomy]
enabled = true                    # Master switch for all autonomy features
auto_preload = true               # Pre-cache imported files after ctx_read
auto_dedup = true                 # Auto-deduplicate at 8+ cached files
auto_related = true               # Suggest related files via import graph
auto_consolidate = true           # Auto-consolidate knowledge periodically
silent_preload = true             # Cache files without output
dedup_threshold = 8               # Files before auto-dedup triggers
consolidate_every_calls = 25      # Run consolidation every N tool calls
consolidate_cooldown_secs = 120   # Minimum seconds between consolidations

Override with environment: LEAN_CTX_AUTONOMY=false disables all.

All Autonomy Fields

KeyTypeDefaultDescription
autonomy.enabledbooltrueMaster switch. When false, all autonomy features are disabled regardless of individual settings.
autonomy.auto_preloadbooltrueAfter ctx_read, automatically pre-cache files referenced via imports/includes.
autonomy.auto_dedupbooltrueAutomatically deduplicate the file cache when it exceeds dedup_threshold.
autonomy.auto_relatedbooltrueSuggest related files based on the import graph when reading a file.
autonomy.auto_consolidatebooltruePeriodically consolidate knowledge base entries to merge duplicates and prune stale entries.
autonomy.silent_preloadbooltruePre-cache files silently without adding output to tool responses. Disable to see which files were preloaded.
autonomy.dedup_thresholdu328Number of cached files that triggers automatic deduplication.
autonomy.consolidate_every_callsu3225Run knowledge consolidation every N tool calls. Higher values reduce overhead but delay cleanup.
autonomy.consolidate_cooldown_secsu32120Minimum seconds between two consecutive consolidation runs, preventing rapid repeated consolidation.

User TOML Filters

Define custom compression rules in ~/.lean-ctx/filters/*.toml. User filters are applied before builtin patterns.

# ~/.lean-ctx/filters/my-api.toml
name = "My API filter"
commands = ["curl"]

[[rules]]
pattern = "X-Request-Id: [a-f0-9-]+"
replacement = ""

[[rules]]
pattern = "^\\s+$"
replacement = ""

Manage via CLI: lean-ctx filter list, lean-ctx filter validate, lean-ctx filter init (creates example).


Security Layer v3.1.3

Security

lean-ctx enforces these security boundaries automatically. No configuration needed - they are always active:

FeatureBehavior
PathJailAll file operations are sandboxed to the project root. Symlink traversal, ../ path escapes, and absolute paths outside the project are blocked. Fixes CVE-class symlink leak vulnerabilities.
Shell Output CapShell command output is capped at 200KB with clear truncation markers ([truncated]). Prevents memory exhaustion from runaway commands.
File Size CapFile reads are capped at 10MB. Prevents accidental ingestion of binary files or generated assets.

PathJail (Project-Root Sandbox)

Every file operation is validated through a single resolve_path choke point. Paths must resolve inside the project root - symlink attacks, ../ traversal, and absolute paths outside the project are rejected. The sandbox is enforced for ctx_read, ctx_edit, ctx_search, ctx_tree, and all other file-touching tools.

Size Caps

Env VariableDefaultProtects
LCTX_MAX_READ_BYTES5 MBFile reads (ctx_read, ctx_edit)
LCTX_MAX_SHELL_BYTES1 MBShell output capture (ctx_shell)

Prompt Injection Hardening

All metadata in tool responses is sanitized via neutralize_metadata - stripping control characters, encoded sequences, and injection patterns. Sensitive output blocks (knowledge, memory, gotchas) are wrapped in CSPRNG-fenced boundaries to prevent prompt injection through LLM-generated content stored in memory.

TOCTOU Mitigation

ctx_edit uses the same file handle for read-verify-write, preventing time-of-check-to-time-of-use race conditions where a file could be swapped between the validation and write phases.


Secret Detection v3.6.0

The [secret_detection] section configures regex-based secret detection in tool outputs. When enabled, lean-ctx scans all tool responses for patterns matching API keys, tokens, passwords, and other sensitive values.

Configuration

# ~/.lean-ctx/config.toml
[secret_detection]
enabled = true                # Enable regex-based secret detection (default: true)
redact = false                # Automatically redact detected secrets (default: false)
custom_patterns = []          # Additional regex patterns for detection (default: [])
KeyTypeDefaultDescription
secret_detection.enabledbooltrueEnable regex-based secret detection. When enabled, tool outputs are scanned for patterns matching known secret formats (API keys, tokens, passwords, private keys).
secret_detection.redactboolfalseAutomatically redact detected secrets in tool output. When true, matched secrets are replaced with [REDACTED]. When false, secrets are flagged with a warning but passed through.
secret_detection.custom_patternslist[]Additional regex patterns for secret detection, merged with the built-in patterns. Each entry is a regex string. Example: ["MYAPP_[A-Z0-9]32", "custom-secret-[a-f0-9]+"]

Boundary Policy v3.6.0

The [boundary_policy] section controls cross-project access behavior. In monorepo or multi-project setups, this determines whether agents can search or import files across project boundaries, and whether such access is logged for audit.

Configuration

# ~/.lean-ctx/config.toml
[boundary_policy]
allow_cross_project = false   # Allow cross-project search/import (default: false)
audit_cross_project = false   # Log cross-project access events (default: false)
KeyTypeDefaultDescription
boundary_policy.allow_cross_projectboolfalseAllow cross-project search and import. When false, file operations are restricted to the current project root. Enable for monorepo setups where agents need access across project boundaries.
boundary_policy.audit_cross_projectboolfalseLog cross-project access events. When true, every cross-project file access is recorded in the audit trail for compliance and security review.

Data Directory v3.1.3

All persistent data (stats, knowledge, agent state, sessions, cache) is stored under a single base directory, resolved in this order:

  1. LEAN_CTX_DATA_DIR environment variable (if set)
  2. ~/.lean-ctx (default)
# Override for custom locations or containers
export LEAN_CTX_DATA_DIR=/data/lean-ctx

# Verify current data directory
ls "$(lean-ctx config | grep data_dir)"

CLI File Cache v2.21.1

Since v2.21.1, lean-ctx read <file> in CLI mode caches file content to $LEAN_CTX_DATA_DIR/cli-cache/cache.json (default: ~/.lean-ctx/cli-cache/cache.json). Subsequent reads of unchanged files return a compact ~13-token cache-hit response instead of the full file content. This enables caching for CLI-mode integrations (e.g. pi-lean-ctx) that don't use the MCP server.

ParameterValue
TTL5 minutes (300s)
Max entries200
Change detectionBLAKE3 hash
Cache location~/.lean-ctx/cli-cache/cache.json

Management Commands

lean-ctx cache              # Overview with hit rate
lean-ctx cache stats        # Detailed statistics
lean-ctx cache clear        # Remove all entries
lean-ctx cache invalidate <path>  # Remove specific file

lean-ctx read file.rs --fresh     # Bypass cache for one read

Cache Eviction Policy

The session cache uses a multi-signal eviction system: Reciprocal Rank Fusion (RRF) fuses recency, frequency, and size into a base score. On top, Hebbian co-access patterns track which files are used together, and a Boltzmann temperature mechanism adjusts eviction aggressiveness based on memory pressure. Entries with the lowest combined energy are evicted first. No manual tuning required.


Progressive Throttling / Loop Detection

lean-ctx automatically detects when an AI agent loops - calling the same or similar tools repeatedly without progress. This prevents token waste in large and monorepo projects.

Per-Fingerprint Throttling

Tool calls are tracked using a fingerprint-based sliding window (default 5 minutes). When repeated identical calls are detected, throttling escalates:

LevelTriggerBehavior
Normal≤ 2 identical callsNo intervention
Reduced3+ identical calls (configurable via reduced_threshold)Warning appended to tool response
BlockedDisabled by default (blocked_threshold = 0). Set to a positive value to enable.Tool call blocked with actionable guidance

Cross-Tool Search Tracking v3.1.2

Agents often alternate between ctx_search, ctx_shell (grep/rg/find), and ctx_semantic_search to search for the same thing. lean-ctx now tracks ALL search-related calls as a group. If more than 10 total search calls happen within the window (regardless of which tool), the agent is blocked with guidance to use ctx_tree and narrow searches with the path parameter.

Pattern Similarity Detection v3.1.2

Searching for "compress", then "compression", then "compress_output" is now detected as the same semantic loop. lean-ctx extracts the alpha-root of each search pattern and groups similar queries together.

Configuration

All thresholds are configurable via ~/.lean-ctx/config.toml:

[loop_detection]
normal_threshold = 2      # warn after this many identical calls
reduced_threshold = 4     # reduce output after this many
blocked_threshold = 0     # block after this many (0 = disabled)
window_secs = 300         # sliding window in seconds
search_group_limit = 10   # total search calls before block

Pipe Guard v2.21.6

Since v2.21.6, the shell hook automatically detects when stdout is piped (not a terminal) and bypasses all compression. This prevents corruption when piping command output - for example, curl -fsSL https://example.com/install.sh | sh now works correctly even with the lean-ctx shell hook active.

ShellDetection Method
Bash / Zsh[ ! -t 1 ]
Fishnot isatty stdout
PowerShell[Console]::IsOutputRedirected

Tool Result Archive v3.3.3

When compression removes details you might need later, the Tool Result Archive saves the full uncompressed output to disk and returns a reference ID. The agent can retrieve the full content at any time using ctx_expand.

This is the core of zero-loss compression: the context window stays small, but no information is permanently lost.

How It Works

  1. A tool response exceeds the configured token threshold
  2. LeanCTX stores the full output to ~/.lean-ctx/archive/ (content-addressed by SHA-256)
  3. The compressed response includes an [ARCHIVE: <id>] reference
  4. The agent calls ctx_expand <id> to retrieve the full content on demand

Configuration

# ~/.lean-ctx/config.toml
[archive]
enabled = true                # Enable tool result archiving (default: true)
threshold_chars = 4096        # Minimum characters before archiving (default: 4096)
max_age_hours = 48            # Auto-cleanup after N hours (default: 48)
max_disk_mb = 500             # Maximum total disk usage for archive (default: 500)
KeyTypeDefaultDescription
archive.enabledbooltrueEnable the archive system. When disabled, compression is permanent (no retrieval).
archive.threshold_charsu324096Minimum character count in a tool response before archiving is considered.
archive.max_age_hoursu3248Global maximum age for archived entries. Entries older than this are purged regardless of access patterns.
archive.max_disk_mbu32500Maximum total disk space (in MB) the archive directory may consume. Oldest entries are evicted when the limit is reached.

Environment Variables

VariableValuesDescription
LEAN_CTX_ARCHIVEtrue / falseOverride archive.enabled at runtime.
LEAN_CTX_ARCHIVE_THRESHOLDnumberOverride archive.threshold_chars.

Storage

Archived results are content-addressed (SHA-256 hash of the full output). Duplicate content is automatically deduplicated. Race conditions from parallel tool calls are handled via PID-unique temporary files with atomic rename.



Compression Level v4.0.0

compression_level is the unified control for the 4-layer terse engine, replacing the legacy output_density and terse_agent settings. It governs both input compression (tool responses) and output optimization (agent responses) through a single knob.

Levels

LevelValueBehavior
Off"off"No compression. Full tool output, no output optimization.
Lite"lite"Abbreviations and function references. Moderate input compression (~20-40% savings).
Standard"standard"Balanced compression with CEP v1 protocol, delta-only output, and structured notation. The recommended default.
Max"max"Maximum compression. Single-line output, aggressive truncation, symbols only. Best for CI/automated pipelines.

Configuration

# ~/.lean-ctx/config.toml
compression_level = "off"    # off (default) | lite | standard | max

# Legacy settings still accepted for backward compatibility:
# terse_agent = "full"            # maps to compression_level = "standard"
# output_density = "terse"        # maps to compression_level = "lite"

Environment Variables

VariableValuesDescription
LEAN_CTX_COMPRESSIONoff / lite / standard / maxOverride compression_level at runtime.
LEAN_CTX_TERSE_AGENToff / lite / full / ultra / true / falseLegacy. Still accepted. Mapped to compression_level internally.

Interaction with CRP Mode

compression_level and LEAN_CTX_CRP_MODE are complementary but independent. CRP mode controls the format of lean-ctx's own output (tdd/compact/off), while compression_level controls both input compression and the instructions given to the agent about how it should respond. Both can be active simultaneously for maximum token savings.

What Changes (Standard / Max)

When compression is active at "standard" or above, the 4-layer terse engine applies:

  • Layer 1: Dictionary — Common token substitutions and abbreviations
  • Layer 2: Residual — Whitespace normalization, boilerplate removal
  • Layer 3: Scoring — Information-theoretic ranking of content blocks
  • Layer 4: Pipeline — CEP v1 protocol with delta-only output, structured notation (+/-/~), and token budgets

This can reduce total token usage by 30-50% per conversation turn. Formally verified by 82 Lean4 theorems including TerseQuality and TerseEngine proofs.

Legacy: Terse Agent Mode

The terse_agent setting is still accepted for backward compatibility. Values are mapped automatically: "off" → Off, "lite" → Lite, "full" → Standard, "ultra" → Max. Boolean true maps to Standard.


Legacy: Output Density v3.1.0 — superseded by compression_level

output_density is superseded by compression_level, which unifies both input and output compression into a single setting. The legacy output_density values are still accepted and mapped automatically: "normal" → Off, "terse" → Lite, "ultra" → Max.

Legacy Configuration

# ~/.lean-ctx/config.toml
# Legacy (still works):
output_density = "terse"

# Recommended replacement:
compression_level = "lite"

Minimal Overhead v3.4.1

minimal_overhead reduces lean-ctx's per-call token footprint to the absolute minimum. It's designed for environments where every token counts - Codex, CI pipelines, or any cost-sensitive workflow.

What it disables

FeatureNormal ModeMinimal Mode
Auto-checkpoint injectionInjected every N calls with session cacheSkipped entirely
Savings notes[saved 142 tokens vs native Grep]Suppressed
Stale notes[cache stale, full→signatures]Suppressed
Shell efficiency hints[hint: ctx_search is more token-efficient]Suppressed
Archive hints[result archived as A1, use ctx_expand]Suppressed
Session block in instructionsActive session stats blockRemoved

Configuration

# ~/.lean-ctx/config.toml
minimal_overhead = true

# Or via environment variable
# export LEAN_CTX_MINIMAL=1

# To only disable checkpoints (keep other meta-strings):
# export LEAN_CTX_NO_CHECKPOINT=1

When to use

  • Codex / Claude Code CLI - these environments benefit most from reduced overhead since every token in the conversation counts against the context window
  • CI/CD pipelines - automated tasks where meta-strings add noise without value
  • High-frequency tool calls - sessions with many short tool calls where per-call overhead accumulates
  • Cost optimization - when you want to minimize API costs without changing compression settings

Measured impact

Combined with lazy tools (default), minimal overhead reduces per-session token overhead from ~6,600 tokens (full tools + verbose) to ~2,400 tokens (lazy + minimal) - a 64% reduction.


Rules Scope v3.3.3

Controls where lean-ctx injects its cursor rules file (lean-ctx.mdc). By default, rules are injected in both the global and project locations to ensure coverage across all Cursor workspaces.

Values

ValueInjection LocationUse Case
"global"~/.cursor/rules/lean-ctx.mdcShare lean-ctx rules across all projects. Project directories stay clean.
"project".cursor/rules/lean-ctx.mdcPer-project rules only. Useful when global rules conflict with other tools.
"both"Both locationsMaximum coverage - ensures lean-ctx is active regardless of how Cursor resolves rules.

Configuration

# ~/.lean-ctx/config.toml
rules_scope = "global"    # global | project | both

# Set via CLI
lean-ctx config set rules_scope project

Buddy Mode

Buddy mode enables collaborative multi-agent workflows. When enabled, lean-ctx coordinates context sharing between multiple agent sessions working on the same project.

Configuration

# ~/.lean-ctx/config.toml
buddy_enabled = true
KeyTypeDefaultDescription
buddy_enabledbooltrueEnable buddy mode for multi-agent collaboration. Agents can share cached files, knowledge entries, and session context via ctx_agent.

Custom Aliases

Define custom shell command aliases that expand inside ctx_shell. Useful for project-specific shortcuts that agents can use without knowing the full command.

Configuration

# ~/.lean-ctx/config.toml
[[custom_aliases]]
alias = "test"
command = "cargo test --workspace --quiet"

[[custom_aliases]]
alias = "lint"
command = "npm run lint -- --fix"

[[custom_aliases]]
alias = "deploy-staging"
command = "kubectl apply -f k8s/staging/ --dry-run=client"
FieldTypeDescription
aliasstringThe alias name. Used as ctx_shell("@test") or similar shorthand.
commandstringThe full command that the alias expands to.

Headless MCP Mode v3.3.3

By default, when lean-ctx starts as an MCP server it automatically configures the environment: injecting rules, installing hooks, updating CLAUDE.md, and registering itself. If you want full control over your configuration - for example when using a custom launcher - you can disable all auto-setup behavior.

Usage

# Set before launching the MCP server
export LEAN_CTX_HEADLESS=1

# lean-ctx will start as a pure MCP server:
# - No rules injection
# - No hook installation
# - No CLAUDE.md modification
# - No agent registry updates
# - No background version check

When to Use

  • Custom launchers - You inject lean-ctx instructions via --append-system-prompt
  • Toggle setups - You want to switch lean-ctx on/off per session
  • CI/CD pipelines - No interactive environment, no need for hooks
  • Sandboxed environments - You don't want lean-ctx modifying files outside the project

Project Identity File (.lean-ctx-id) v3.5.16

lean-ctx identifies projects by hashing their root path. In Docker or CI environments where multiple projects share the same mount path (e.g. /workspace), this can cause data collisions — graph index, semantic cache, bandit state, and embedding index from one project bleeding into another.

To prevent this, create a .lean-ctx-id file in your project root with a unique project name. This file takes highest priority for project identification — above git remote URL, package.json name, and directory name.

# Create the identity file
echo "my-unique-project-name" > .lean-ctx-id

Priority Order

PrioritySourceExample
1 (highest).lean-ctx-idmy-api-service
2Git remote URLgithub.com/org/repo
3package.json / Cargo.toml namemy-api
4 (lowest)Directory path/workspace

Identity-Aware Caches

The following data stores use a composite project hash (path + identity) to ensure isolation:

  • Graph index — project dependency graph (SQLite Property Graph)
  • Semantic cache — embedding-based similarity lookups
  • Bandit state — multi-armed bandit model for read mode selection
  • Embedding index — vector store for semantic search

When a .lean-ctx-id file is added or changed, existing data is automatically migrated from old hash directories. No manual action required.

Docker Usage

# Include in your Dockerfile or mount as volume
COPY .lean-ctx-id /workspace/.lean-ctx-id

# Or create during build
RUN echo "my-project" > /workspace/.lean-ctx-id

Shell Activation v3.5.16

shell_activation controls when lean-ctx shell aliases (like _lc) auto-activate in interactive shells. By default, lean-ctx activates in every shell. In team environments or CI, you may want to limit activation to AI agent sessions only.

Modes

ModeValueBehavior
Always"always"Active in every interactive shell. Default behavior, same as previous versions.
Agents Only"agents-only"Only activates when AI agent environment variables are detected: LEAN_CTX_AGENT, CLAUDECODE, CODEX_CLI_SESSION, GEMINI_SESSION. Since v3.5.19, these same variables also enable tracking in non-interactive shells (e.g. Docker bash -c, Codex CLI), bypassing the TTY guard that normally disables the hook when stdout is not a terminal.
Off"off"Never auto-activate. User must call lean-ctx-on manually to enable shell aliases.

Configuration

# ~/.lean-ctx/config.toml
shell_activation = "agents-only"    # always | agents-only | off

# Or via environment variable
# export LEAN_CTX_SHELL_ACTIVATION=agents-only

When to Use

  • always — Solo developer, want compression in every terminal
  • agents-only — Team environments where non-AI shells should remain unmodified. Also enables non-interactive (non-TTY) tracking for Docker/Codex containers (v3.5.19+)
  • off — Full manual control, only enable when explicitly needed

Cloud

The [cloud] section controls cloud integration features for lean-ctx.

Configuration

# ~/.lean-ctx/config.toml
[cloud]
contribute_enabled = false    # Opt-in to anonymous usage telemetry (default: false)
KeyTypeDefaultDescription
cloud.contribute_enabledboolfalseOpt-in to anonymous usage telemetry. When enabled, lean-ctx sends aggregated compression statistics to help improve the product. No code, file paths, or personal data is transmitted.

Proxy

The [proxy] section lets you override upstream API endpoints for LLM providers. Useful for corporate proxies, self-hosted inference, or regional routing.

Configuration

# ~/.lean-ctx/config.toml
[proxy]
anthropic_upstream = null     # Custom Anthropic API endpoint (default: null)
openai_upstream = null        # Custom OpenAI API endpoint (default: null)
gemini_upstream = null        # Custom Gemini API endpoint (default: null)
KeyTypeDefaultDescription
proxy.anthropic_upstreamstring?nullCustom upstream URL for Anthropic API requests. Set to your corporate proxy or self-hosted endpoint.
proxy.openai_upstreamstring?nullCustom upstream URL for OpenAI API requests.
proxy.gemini_upstreamstring?nullCustom upstream URL for Gemini API requests.

Memory

The [memory] section configures lean-ctx's persistent memory subsystem. Memory is organized into specialized stores — each with its own retention policy, capacity limits, and retrieval rules. Use memory_profile for a preset, or fine-tune individual sub-tables.

How Memory Lifecycle Works

Memory entries (facts, patterns, gotchas) have a confidence score that changes over time:

  1. New facts start with high confidence (based on source quality)
  2. Daily decay (decay_rate) gradually reduces confidence of unused facts
  3. Access resets — when a fact is recalled by the agent, its confidence is restored
  4. Low-confidence facts (below low_confidence_threshold) are flagged for review
  5. Stale facts (not accessed for stale_days) become eligible for pruning
  6. Pruning happens during auto_consolidate runs — not instantly
Safe to tune: Reducing stale_days from 30 to 7 means facts unused for a week are pruned sooner. This is safe — frequently accessed facts are never stale regardless of this setting. Increasing decay_rate from 0.01 to 0.05 makes facts lose confidence 5× faster, which is useful in fast-changing projects where old context quickly becomes irrelevant.

[memory.knowledge]

Fact store for project-level knowledge extracted from conversations and code analysis.

[memory.knowledge]
max_facts = 200               # Maximum stored facts per project (default: 200)
max_patterns = 50             # Maximum code patterns (default: 50)
max_history = 100             # Maximum history entries (default: 100)
contradiction_threshold = 0.5 # Confidence threshold for contradiction detection (default: 0.5)
recall_facts_limit = 10       # Facts recalled per retrieval (default: 10)
rooms_limit = 25              # Maximum rooms returned (default: 25)
timeline_limit = 25           # Maximum timeline entries (default: 25)
relations_limit = 40          # Maximum relations returned (default: 40)
KeyTypeDefaultDescription
memory.knowledge.max_factsusize200Maximum number of facts stored in the knowledge base per project. Oldest entries are evicted when the limit is reached.
memory.knowledge.max_patternsusize50Maximum number of code patterns retained.
memory.knowledge.max_historyusize100Maximum history entries retained in the knowledge timeline.
memory.knowledge.contradiction_thresholdf320.5Confidence threshold for automatic contradiction detection between facts.
memory.knowledge.recall_facts_limitusize10Number of facts retrieved per knowledge recall query.
memory.knowledge.rooms_limitusize25Maximum number of rooms returned per query.
memory.knowledge.timeline_limitusize25Maximum number of timeline entries returned per query.
memory.knowledge.relations_limitusize40Maximum number of entity relations returned per query.

[memory.episodic]

Session-level memory capturing what happened during agent interactions.

[memory.episodic]
max_episodes = 500            # Maximum stored episodes (default: 500)
max_actions_per_episode = 50  # Actions per episode (default: 50)
summary_max_chars = 200       # Episode summary length (default: 200)
KeyTypeDefaultDescription
memory.episodic.max_episodesusize500Maximum number of episodes retained across sessions.
memory.episodic.max_actions_per_episodeusize50Maximum actions recorded per individual episode.
memory.episodic.summary_max_charsusize200Maximum character length for automatically generated episode summaries.

[memory.procedural]

Learned procedures extracted from repeated agent workflows.

[memory.procedural]
max_procedures = 100          # Maximum stored procedures (default: 100)
min_repetitions = 3           # Repetitions before a pattern becomes a procedure (default: 3)
min_sequence_len = 2          # Minimum sequence length for detection (default: 2)
max_window_size = 10          # Maximum window size for pattern analysis (default: 10)
KeyTypeDefaultDescription
memory.procedural.max_proceduresusize100Maximum number of learned procedures stored.
memory.procedural.min_repetitionsusize3Minimum times a workflow pattern must repeat before it is promoted to a procedure.
memory.procedural.min_sequence_lenusize2Minimum length of a tool-call sequence to be considered for procedure extraction.
memory.procedural.max_window_sizeusize10Maximum sliding window size when scanning for repeated patterns.

[memory.lifecycle]

Controls how memory entries age, decay, and are pruned over time.

[memory.lifecycle]
decay_rate = 0.01             # Daily decay factor for memory scores (default: 0.01)
low_confidence_threshold = 0.3 # Below this, facts are low-confidence (default: 0.3)
stale_days = 30               # Days before an entry is considered stale (default: 30)
similarity_threshold = 0.85   # Cosine similarity for deduplication (default: 0.85)
KeyTypeDefaultDescription
memory.lifecycle.decay_ratef320.01Daily decay factor applied to memory entry scores. Higher values age entries faster.
memory.lifecycle.low_confidence_thresholdf320.3Facts with confidence below this threshold are flagged as low-confidence and may be pruned earlier.
memory.lifecycle.stale_daysi6430Number of days after which an unaccessed entry is considered stale and eligible for pruning.
memory.lifecycle.similarity_thresholdf320.85Cosine similarity threshold for deduplication. Entries above this threshold are merged.

[memory.gotcha]

Stores project-specific gotchas — pitfalls, caveats, and non-obvious behaviors discovered during sessions.

[memory.gotcha]
max_gotchas_per_project = 100       # Maximum gotchas per project (default: 100)
retrieval_budget_per_room = 10      # Gotchas surfaced per session (default: 10)
default_decay_rate = 0.03           # Default decay rate for gotcha importance (default: 0.03)
KeyTypeDefaultDescription
memory.gotcha.max_gotchas_per_projectusize100Maximum number of gotchas stored per project.
memory.gotcha.retrieval_budget_per_roomusize10Maximum number of gotchas surfaced in a single session context.
memory.gotcha.default_decay_ratef320.03Default daily decay rate applied to gotcha importance scores. Category-specific overrides can be set via category_decay_overrides.

[memory.embeddings]

Configuration for the vector embedding index used by semantic memory operations.

[memory.embeddings]
max_facts = 2000              # Maximum facts in the embedding index (default: 2000)
KeyTypeDefaultDescription
memory.embeddings.max_factsusize2000Maximum number of facts indexed in the vector embedding store.

Exit Codes

In shell hook mode (-c), LeanCTX forwards the exit code of the wrapped command. This ensures scripts and CI pipelines work correctly. If the command fails (non-zero exit), the full original output is saved to ~/.lean-ctx/tee/ for debugging (when tee_mode is set to failures or always).


Token Counting

lean-ctx uses tiktoken-rs with the o200k_base encoding (same as GPT-4o, Claude, and other modern models) for exact token counting. No estimation, no heuristics - every savings number you see in lean-ctx gain or the dashboard reflects real token counts.