Documentação

Configuração

Opções de configuração, variáveis de ambiente e configuração do editor.

O LeanCTX funciona pronto para uso, sem configuração. Todas as opções abaixo são opcionais - os valores padrão são ajustados para fluxos de trabalho típicos de desenvolvimento assistido por IA.


Arquivo de Configuração

O LeanCTX suporta um arquivo de configuração TOML opcional em ~/.lean-ctx/config.toml.

Criar a configuração padrão

lean-ctx config init

Ver configuração atual

lean-ctx config

Definir valores individuais

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

Opções de Configuração

ChaveTipoPadrãoDescrição
ultra_compact bool false Ativar modo de saída ultra-compacto
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" Quando salvar a saída completa em ~/.lean-ctx/tee/. Valores: always, failures, never. Compatível com versões anteriores: tee_on_error: true equivale a failures.
passthrough_urls list [] Fragmentos de URL para passthrough do curl. Comandos curl direcionados a essas URLs ignoram a compressão JSON e retornam respostas completas.
checkpoint_interval u32 15 Auto-checkpoint a cada N chamadas de ferramenta (0 = desativado). Sobrescrito pela variável de ambiente LEAN_CTX_CHECKPOINT_INTERVAL.
excluded_commands list [] Comandos para os quais a compressão deve ser ignorada
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.

Variáveis de Ambiente

VariávelValoresDescrição
LEAN_CTX_CRP_MODE tdd (default), compact, off Controla o nível de compressão do Token Dense Dialect
LEAN_CTX_OUTPUT_DENSITY normal (default), terse, ultra Sobrescreve a densidade de saída sem editar a configuração. Aplica-se a todas as saídas de ferramentas 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) Auto-checkpoint a cada N chamadas de ferramenta (0 = desativado)
LEAN_CTX_CACHE_TTL number (default: 300) Segundos de inatividade antes da limpeza automática do cache da sessão (0 = desativado)
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 Proteção contra recursão. Quando definida, o lean-ctx passa os comandos sem compressão. Definida automaticamente pelo hook do shell.
LEAN_CTX_DISABLED set / unset Interruptor principal. Ignora TODA compressão tanto no hook do shell quanto no servidor MCP. Tudo passa sem modificação.
LEAN_CTX_RAW set / unset Ignora a compressão para o comando atual. Mesmo efeito que a flag --raw ou o parâmetro MCP raw=true.
LEAN_CTX_AUTONOMY true / false Ativa/desativa recursos de inteligência autônoma (auto-preload, auto-dedup, dicas de relacionamento). Padrão: true.
LEAN_CTX_HOOK_EXCLUDE comma-separated globs Padrões glob separados por vírgula para exclusão de redirecionamento do hook. Tem precedência sobre redirect_exclude no config.toml. Exemplo: .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 Sobrescreve onde os modelos de embeddings são armazenados/baixados (padrão: ~/.lean-ctx/models). Útil para contêineres e 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.

Exclusão de redirecionamento do hook

O comando lean-ctx hook redirect intercepta chamadas nativas das ferramentas Read/Grep/ListFiles e as redireciona para as ferramentas lean-ctx MCP. Para excluir caminhos específicos deste redirecionamento (por exemplo, para fluxos de trabalho de Edit ou arquivos de protocolo do framework), use a opção de configuração redirect_exclude ou a variável de ambiente 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.


Regra do Cursor

Para economia máxima de tokens, adicione um .cursor/rules/lean-ctx.mdc ao seu projeto. Isso instrui o LLM a preferir ferramentas do LeanCTX e usar padrões de saída compactos. Um exemplo está incluído em examples/lean-ctx.mdc no repositório.

A regra do Cursor inclui o CRP v2 (Protocolo de Resposta Compacta) que pode reduzir tanto tokens de saída (50–80%) quanto tokens de raciocínio (30–60%) por meio de análise estruturada de tarefas e raciocínio de hipótese única. Esses intervalos são estimativas por resposta e dependem do modelo e da complexidade da tarefa.


Configuração de Autonomia

A camada de inteligência autônoma executa pipelines de otimização automaticamente. Configure em ~/.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

Sobrescreva com variável de ambiente: LEAN_CTX_AUTONOMY=false desativa tudo.

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_secsu32300Minimum seconds between two consecutive consolidation runs, preventing rapid repeated consolidation.

Filtros TOML do Usuário

Defina regras de compressão personalizadas em ~/.lean-ctx/filters/*.toml. Filtros do usuário são aplicados antes dos padrões integrados.

# ~/.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 = ""

Gerencie via CLI: lean-ctx filter list, lean-ctx filter validate, lean-ctx filter init (cria exemplo).


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.


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 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:

LevelTriggerBehavior
Normal≤ 2 identical callsNo intervention
Reduced3–6 identical callsWarning appended to tool response
Blocked7+ identical callsTool 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.

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: 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)
KeyTypeDefaultDescription
archive.enabledboolfalseEnable the archive system. When disabled, compression is permanent (no retrieval).
archive.threshold_charsu324096Minimum character count in a tool response before archiving is considered. Works alongside threshold_tokens - both must be exceeded.
archive.threshold_tokensu32500Minimum token count before archiving kicks in. Small outputs don't benefit from archiving.
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.
archive.ttl_minutesu32120Per-entry auto-cleanup interval. Entries not accessed within this window are cleaned up.
archive.exclude_toolslist[]Tool names whose output should never be archived, even if they exceed thresholds. Example: ["ctx_tree", "ctx_overview"]
archive.always_archivelist[]Tool names whose output is always archived when above threshold, bypassing any heuristics that might skip archiving. Example: ["ctx_shell", "ctx_search"]
archive.mask_secretsbooltrueRedact detected secrets (API keys, tokens, passwords) before writing to the archive. Uses the same detection patterns as the security layer.

Environment Variables

VariableValuesDescription
LEAN_CTX_ARCHIVEtrue / falseOverride archive.enabled at runtime.
LEAN_CTX_ARCHIVE_THRESHOLDnumberOverride archive.threshold_tokens.
LEAN_CTX_ARCHIVE_TTLnumber (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

  1. lean-ctx detects a compaction event (conversation history is shorter than expected)
  2. A resume summary is generated from the session diary, knowledge base, and cached files
  3. 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)
KeyTypeDefaultDescription
compaction.auto_detectbooltrueAutomatically detect when the IDE has compacted conversation history and trigger a context recovery. Disable if you handle compaction manually.
compaction.resume_max_tokensu32500Maximum token budget for the resume summary injected after compaction. Higher values recover more context but consume more of the fresh context window.
compaction.include_archivesbooltrueInclude references to archived tool results in the resume summary. The agent can then call ctx_expand to retrieve full content.
compaction.include_knowledgebooltrueInclude 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

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 = "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

VariableValuesDescription
LEAN_CTX_COMPRESSION_LEVELoff / 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_enabledboolfalseEnable 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"
FieldTypeDescription
namestringThe 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

Códigos de Saída

No modo hook do shell (-c), o LeanCTX encaminha o código de saída do comando executado. Isso garante que scripts e pipelines de CI funcionem corretamente. Se o comando falhar (código de saída diferente de zero), a saída original completa é salva em ~/.lean-ctx/tee/ para depuração (quando tee_mode está definido como failures ou always).


Contagem de Tokens

O lean-ctx usa tiktoken-rs com a codificação o200k_base (a mesma do GPT-4o, Claude e outros modelos modernos) para contagem exata de tokens. Sem estimativas, sem heurísticas - cada número de economia que você vê em lean-ctx gain ou no dashboard reflete contagens reais de tokens.