LeanCTX 开箱即用,零配置。以下所有设置都是可选的--默认值已针对典型的 AI 辅助开发工作流进行优化。
配置文件
LeanCTX 支持位于 ~/.lean-ctx/config.toml 的可选 TOML 配置文件。
创建默认配置
lean-ctx config init 查看当前配置
lean-ctx config 设置单个值
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 配置选项
| 键 | 类型 | 默认值 | 描述 |
|---|---|---|---|
ultra_compact | bool | false | 启用超紧凑输出模式 |
compression_level | string | "standard" | 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. |
tee_mode | string | "failures" | 何时将完整输出保存到 ~/.lean-ctx/tee/。值:always、failures、never。向后兼容:tee_on_error: true 映射为 failures。 |
passthrough_urls | list | [] | curl 直通的 URL 片段。以这些 URL 为目标的 curl 命令跳过 JSON 压缩并返回完整响应。 |
checkpoint_interval | u32 | 15 | 每 N 次工具调用自动创建检查点(0 = 禁用)。可被 LEAN_CTX_CHECKPOINT_INTERVAL 环境变量覆盖。 |
excluded_commands | list | [] | 跳过压缩的命令 |
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. |
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 | u32 | 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 | false | 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/**"] |
[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. |
[compaction] | table | - | Configure session resilience after IDE compaction events. Controls how lean-ctx recovers context when the IDE truncates conversation history. See Compaction Resilience. |
[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. |
环境变量
| 变量 | 值 | 描述 |
|---|---|---|
LEAN_CTX_CRP_MODE | tdd (default), compact, off | 控制 Token Dense Dialect 压缩级别 |
LEAN_CTX_OUTPUT_DENSITY | normal (default), terse, ultra | 无需修改配置即可覆盖输出密度。适用于所有 MCP 工具输出。 |
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) | 每 N 次工具调用自动创建检查点(0 = 禁用) |
LEAN_CTX_CACHE_TTL | number (default: 300) | 空闲多少秒后自动清除会话缓存(0 = 禁用) |
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_ACTIVE | set / unset | 递归保护。设置后,lean-ctx 直接传递命令而不压缩。由 shell hook 自动设置。 |
LEAN_CTX_DISABLED | set / unset | 主开关。绕过 shell hook 和 MCP 服务器中的所有压缩。一切原样传递。 |
LEAN_CTX_RAW | set / unset | 跳过当前命令的压缩。与 --raw 标志或 raw=true MCP 参数效果相同。 |
LEAN_CTX_AUTONOMY | true / false | 启用/禁用自主智能功能(自动预加载、自动去重、相关提示)。默认:true。 |
LEAN_CTX_HOOK_EXCLUDE | comma-separated globs | 以逗号分隔的 glob 模式,用于 hook 重定向排除。优先于 config.toml 中的 redirect_exclude。示例:.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_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_LEVEL | 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: 500) | Minimum token count before a tool result is archived. Override for archive.threshold_tokens. |
LEAN_CTX_ARCHIVE_TTL | number (default: 120) | Auto-cleanup interval in minutes for archived results. Override for archive.ttl_minutes. |
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 | 覆盖向量嵌入模型的存储/下载目录(默认:~/.lean-ctx/models)。适用于容器和 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. |
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 重定向排除
lean-ctx hook redirect 命令拦截原生 Read/Grep/ListFiles 工具调用并将其重定向到 lean-ctx MCP 工具。要将特定路径排除在重定向之外(例如用于 Edit 工作流或框架协议文件),请使用 redirect_exclude 配置选项或 LEAN_CTX_HOOK_EXCLUDE 环境变量。
# ~/.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 three integration modes:
cli-redirect (CLI-first), mcp (explicit ctx_* tools), and hybrid (both).
auto selects the best default per tool.
# Auto (recommended)
lean-ctx init --agent cursor
# Force MCP tools
lean-ctx init --agent cursor --mode mcp
# Force CLI-first
lean-ctx init --agent cursor --mode cli-redirect
# Best of both worlds
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 规则
为获得最大 token 节省,在项目中添加 .cursor/rules/lean-ctx.mdc。这指示 LLM 优先使用 LeanCTX 工具并使用紧凑输出模式。仓库中 examples/lean-ctx.mdc 包含一个示例。
Cursor 规则包含 CRP v2(紧凑响应协议),可通过结构化任务解析和单假设推理减少输出 token(50–80%)和思考 token(30–60%)。这些范围是每响应估算值,取决于模型和任务复杂度。
自主配置
自主智能层自动运行优化流水线。在 ~/.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 = 300 # Minimum seconds between consolidations 通过环境变量覆盖:LEAN_CTX_AUTONOMY=false 禁用所有。
All Autonomy Fields
| Key | Type | Default | Description |
|---|---|---|---|
autonomy.enabled | bool | true | Master switch. When false, all autonomy features are disabled regardless of individual settings. |
autonomy.auto_preload | bool | true | After ctx_read, automatically pre-cache files referenced via imports/includes. |
autonomy.auto_dedup | bool | true | Automatically deduplicate the file cache when it exceeds dedup_threshold. |
autonomy.auto_related | bool | true | Suggest related files based on the import graph when reading a file. |
autonomy.auto_consolidate | bool | true | Periodically consolidate knowledge base entries to merge duplicates and prune stale entries. |
autonomy.silent_preload | bool | true | Pre-cache files silently without adding output to tool responses. Disable to see which files were preloaded. |
autonomy.dedup_threshold | u32 | 8 | Number of cached files that triggers automatic deduplication. |
autonomy.consolidate_every_calls | u32 | 25 | Run knowledge consolidation every N tool calls. Higher values reduce overhead but delay cleanup. |
autonomy.consolidate_cooldown_secs | u32 | 300 | Minimum seconds between two consecutive consolidation runs, preventing rapid repeated consolidation. |
用户 TOML 过滤器
在 ~/.lean-ctx/filters/*.toml 中定义自定义压缩规则。用户过滤器在内置模式之前应用。
# ~/.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 = "" 通过 CLI 管理:lean-ctx filter list、lean-ctx filter validate、lean-ctx filter init(创建示例)。
Security Layer v3.1.3
Security
lean-ctx enforces these security boundaries automatically. No configuration needed - they are always active:
| Feature | Behavior |
|---|---|
| PathJail | All 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 Cap | Shell command output is capped at 200KB with clear truncation markers ([truncated]). Prevents memory exhaustion from runaway commands. |
| File Size Cap | File 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 Variable | Default | Protects |
|---|---|---|
LCTX_MAX_READ_BYTES | 5 MB | File reads (ctx_read, ctx_edit) |
LCTX_MAX_SHELL_BYTES | 1 MB | Shell 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.
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:
LEAN_CTX_DATA_DIRenvironment variable (if set)~/.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.
| Parameter | Value |
|---|---|
| TTL | 5 minutes (300s) |
| Max entries | 200 |
| Change detection | BLAKE3 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 Reciprocal Rank Fusion (RRF) for eviction. Each entry is ranked independently by recency, frequency, and size - then fused into a single score. Entries with the lowest RRF score 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:
| Level | Trigger | Behavior |
|---|---|---|
| Normal | ≤ 2 identical calls | No intervention |
| Reduced | 3–6 identical calls | Warning appended to tool response |
| Blocked | 7+ identical calls | 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 = 6 # block after this many
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.
| Shell | Detection Method |
|---|---|
| Bash / Zsh | [ ! -t 1 ] |
| Fish | not 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
- A tool response exceeds the configured token threshold
- LeanCTX stores the full output to
~/.lean-ctx/archive/(content-addressed by SHA-256) - The compressed response includes an
[ARCHIVE: <id>]reference - 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: false)
threshold_chars = 4096 # Minimum characters before archiving (default: 4096)
threshold_tokens = 500 # Minimum tokens before archiving kicks in (default: 500)
max_age_hours = 48 # Auto-cleanup after N hours (default: 48)
max_disk_mb = 500 # Maximum total disk usage for archive (default: 500)
ttl_minutes = 120 # Per-entry auto-cleanup interval (default: 120)
exclude_tools = ["ctx_tree"] # Tools whose output is never archived
always_archive = ["ctx_shell", "ctx_search"] # Tools always archived above threshold
mask_secrets = true # Redact secrets before archiving (default: true) | Key | Type | Default | Description |
|---|---|---|---|
archive.enabled | bool | false | Enable the archive system. When disabled, compression is permanent (no retrieval). |
archive.threshold_chars | u32 | 4096 | Minimum character count in a tool response before archiving is considered. Works alongside threshold_tokens - both must be exceeded. |
archive.threshold_tokens | u32 | 500 | Minimum token count before archiving kicks in. Small outputs don't benefit from archiving. |
archive.max_age_hours | u32 | 48 | Global maximum age for archived entries. Entries older than this are purged regardless of access patterns. |
archive.max_disk_mb | u32 | 500 | Maximum total disk space (in MB) the archive directory may consume. Oldest entries are evicted when the limit is reached. |
archive.ttl_minutes | u32 | 120 | Per-entry auto-cleanup interval. Entries not accessed within this window are cleaned up. |
archive.exclude_tools | list | [] | Tool names whose output should never be archived, even if they exceed thresholds. Example: ["ctx_tree", "ctx_overview"] |
archive.always_archive | list | [] | Tool names whose output is always archived when above threshold, bypassing any heuristics that might skip archiving. Example: ["ctx_shell", "ctx_search"] |
archive.mask_secrets | bool | true | Redact detected secrets (API keys, tokens, passwords) before writing to the archive. Uses the same detection patterns as the security layer. |
Environment Variables
| Variable | Values | Description |
|---|---|---|
LEAN_CTX_ARCHIVE | true / false | Override archive.enabled at runtime. |
LEAN_CTX_ARCHIVE_THRESHOLD | number | Override archive.threshold_tokens. |
LEAN_CTX_ARCHIVE_TTL | number (minutes) | Override archive.ttl_minutes. |
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.
Compaction Resilience v3.3.3
IDEs like Cursor and Windsurf periodically compact (truncate) conversation history to stay within
context limits. When this happens, the agent loses all prior context - cached files, knowledge,
session state. The [compaction] block controls how lean-ctx automatically recovers.
How It Works
- lean-ctx detects a compaction event (conversation history is shorter than expected)
- A resume summary is generated from the session diary, knowledge base, and cached files
- The summary is injected into the next tool response so the agent can continue seamlessly
Configuration
# ~/.lean-ctx/config.toml
[compaction]
auto_detect = true # Automatically detect compaction events (default: true)
resume_max_tokens = 500 # Maximum tokens for the resume summary (default: 500)
include_archives = true # Include archive references in resume (default: true)
include_knowledge = true # Include knowledge base entries in resume (default: true) | Key | Type | Default | Description |
|---|---|---|---|
compaction.auto_detect | bool | true | Automatically detect when the IDE has compacted conversation history and trigger a context recovery. Disable if you handle compaction manually. |
compaction.resume_max_tokens | u32 | 500 | Maximum token budget for the resume summary injected after compaction. Higher values recover more context but consume more of the fresh context window. |
compaction.include_archives | bool | true | Include references to archived tool results in the resume summary. The agent can then call ctx_expand to retrieve full content. |
compaction.include_knowledge | bool | true | Include relevant knowledge base entries (discoveries, decisions, blockers) in the resume summary. |
Example Recovery Flow
# After compaction, the next tool call automatically includes:
# --- COMPACTION RECOVERY ---
# Session: 45 calls, 3 files cached
# Active task: Refactoring auth module
# Key decisions: JWT over sessions (see knowledge #12)
# Archives: [ARC:a1b2c3] auth.rs full output
# --- 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
| Level | Value | Behavior |
|---|---|---|
| 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 = "standard" # off | 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
| Variable | Values | Description |
|---|---|---|
LEAN_CTX_COMPRESSION_LEVEL | off / lite / standard / max | Override compression_level at runtime. |
LEAN_CTX_TERSE_AGENT | off / lite / full / ultra / true / false | Legacy. 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
| Feature | Normal Mode | Minimal Mode |
|---|---|---|
| Auto-checkpoint injection | Injected every N calls with session cache | Skipped 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 instructions | Active session stats block | Removed |
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
| Value | Injection Location | Use Case |
|---|---|---|
"global" | ~/.cursor/rules/lean-ctx.mdc | Share lean-ctx rules across all projects. Project directories stay clean. |
"project" | .cursor/rules/lean-ctx.mdc | Per-project rules only. Useful when global rules conflict with other tools. |
"both" | Both locations | Maximum 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 | Key | Type | Default | Description |
|---|---|---|---|
buddy_enabled | bool | false | Enable 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]]
name = "test"
command = "cargo test --workspace --quiet"
[[custom_aliases]]
name = "lint"
command = "npm run lint -- --fix"
[[custom_aliases]]
name = "deploy-staging"
command = "kubectl apply -f k8s/staging/ --dry-run=client" | Field | Type | Description |
|---|---|---|
name | string | The alias name. Used as ctx_shell("@test") or similar shorthand. |
command | string | The 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
退出码
在 shell hook 模式(-c)下,LeanCTX 转发被包装命令的退出码。这确保脚本和 CI 流水线正常工作。如果命令失败(非零退出),完整的原始输出会保存到 ~/.lean-ctx/tee/ 以供调试(当 tee_mode 设置为 failures 或 always 时)。
Token 计数
lean-ctx 使用 tiktoken-rs 的 o200k_base 编码(与 GPT-4o、Claude 和其他现代模型相同)进行精确 token 计数。无估算,无启发式方法--你在 lean-ctx gain 或仪表盘中看到的每个节省数字都反映真实的 token 计数。