lean-ctx

A set of rules for reducing the amount of project text loaded into an agent's working context, using cached reads and compact results. It replaces standard file, shell, search, and directory commands with its own context-focused tools.

In plain words
What is it for?
Use it when reading files, searching code, viewing directory maps, or running common project commands such as Git, npm, Cargo, Docker, and TypeScript checks. Different read modes provide full content, dependency maps, signatures, diffs, or more aggressively compressed output.
Why use it?
It helps avoid repeatedly loading large files or noisy command output, which can make an agent's context harder to manage.

Cursor rule

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add rules/yvgude/lean-ctx/lean-ctx
Clone the repo
git clone --depth 1 https://github.com/yvgude/lean-ctx
Per session 969 This file is loaded in full into every session.
When invoked 969 The same file — it is already loaded in full.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00969 $0.00969
Opus 5 $0.00485 $0.00485
Sonnet 5 $0.00194 $0.00194
Haiku 4.5 $0.00097 $0.00097

Measured yesterday against content hash d0ca03df266e, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

lean-ctx scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured yesterday.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

rust/examples/lean-ctx.mdc · 100 lines

How it starts

The opening of the file, as written. The whole thing — 100 lines — stays where its author put it; the contents beside it link to each section on GitHub.

lean-ctx MCP Tools

Always use lean-ctx MCP tools. Never use the standard equivalents.

Tool Mapping

Instead of Use Why
Read ctx_read Session caching, 6 compression modes, re-reads cost ~13 tokens
Shell ctx_shell Pattern-based compression for git, npm, cargo, docker, tsc
Grep ctx_search Compact context, token-efficient results
ls, find ctx_tree Compact directory maps with file counts

ctx_read Modes

  • full — default, cached read (use for files you will edit)
  • map — dependency graph + exports + API signatures (use for context-only files)
  • signatures — API surface only (tree-sitter AST for 26 languages)
  • diff — changed lines only (use after edits)
  • aggressive — syntax stripped
  • entropy — Shannon + Jaccard filtering

Re-reads cost ~13 tokens. File refs F1, F2.. persist across the session.

Set fresh=true on ctx_read to bypass cache. Use when: spawned as a subagent, after context compaction, or if you see a [cached] response but do not have the file content in your context.

Default Strategy

  • Reading for context → map
  • Reading to understand API → signatures
  • Reading to edit → full
  • Re-reading → just call full again (cache returns ~13 tokens)

v2.3.0 Scientific Features (automatic)

  • Auto-Delta — re-reading a changed file sends only the diff (98.9% savings)
  • Bayesian Mode Selection — learns optimal mode per file type over time
  • BPE Entropy — entropy filtering uses LLM tokenizer for precision
  • Cross-File Dedupctx_dedup with action=apply replaces shared blocks with references
  • Boltzmann Cache — evicts least-valuable entries when token budget exceeded
  • LITM Profiles — context positioned optimally per LLM model (Claude/GPT/Gemini)

Session Management

  • ctx_compress at >10 turns — creates memory checkpoint
  • ctx_metrics — hard numbers on what was saved
  • ctx_benchmark — find optimal mode for a file
  • ctx_session load — on new chat, restore previous session
  • ctx_dedup — analyze or apply cross-file deduplication

Read the full file on GitHub · 100 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. yesterday First seen · 100 lines · 969 tokens per session scan A d0ca03df266e

Subscribe to this mod's changes

lean-ctx is a cursor rule published in the GitHub repository yvgude/lean-ctx (3,681 stars, last pushed yesterday), licensed Apache-2.0. It adds 969 tokens to every session, about $0.0048 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.