lean-ctx

lean-ctx is a cursor rule for Cursor from yvgude/lean-ctx. It costs 969 tokens per session, scanned A, original, Apache-2.0.

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 for Cursor

Written for Cursor: a Cursor rule (.mdc). Also seen: mentions subagents.

Good fit 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.

Compare 6 cursor rules from other repositories ↓
Install with agentmods
npx agentmods add rules/yvgude/lean-ctx/lean-ctx
About the project

LeanCTX is a local Rust program that controls the repository context an AI coding agent receives, including selecting, compressing, remembering, and tracking information. Developers use it alongside coding agents to reduce repeated context and manage what those agents read and execute, while the catalogue entries provide related rules, skills, and instructions.

yvgude/lean-ctx · 3,879 stars · on GitHub · leanctx.com

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.

Clone the repo
git clone --depth 1 https://github.com/yvgude/lean-ctx

Made for: Cursor.

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for lean-ctx

README.md
[![agentmods](https://agentmods.dev/badge/rules/yvgude/lean-ctx/lean-ctx/github.svg)](https://agentmods.dev/rules/yvgude/lean-ctx/lean-ctx)
Your own site
<a href="https://agentmods.dev/rules/yvgude/lean-ctx/lean-ctx"><img src="https://agentmods.dev/badge/rules/yvgude/lean-ctx/lean-ctx/github.svg" alt="Measured on agentmods" height="20"></a>

Or the 80×15 button, for a site that already has a row of RSS and ATOM ones. Only the verdict fits; the numbers stay here.

agentmods 80×15 button for lean-ctx

Your own site · 80×15
<a href="https://agentmods.dev/rules/yvgude/lean-ctx/lean-ctx"><img src="https://agentmods.dev/badge/rules/yvgude/lean-ctx/lean-ctx.svg" alt="Reviewed on agentmods" width="80" height="20"></a>
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. A grade says what 26 rules found in the file — not that it is safe.
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.1 $0.00969 $0.00969
Opus 5.5 $0.00388 $0.00388
Sonnet 5.5 $0.00194 $0.00194
Haiku 4.5 $0.00097 $0.00097

Measured 12d ago against content hash ec44e4751380, method: parsed. Prices are Anthropic first-party input rates as of 2026-10-07, 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 12d ago.

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, read modes, compact references for eligible re-reads
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

Eligible unchanged re-reads return compact references. 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 → call full again (eligible cache hits return a compact reference)

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 Dedup — ctx_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. 12d ago Changed ec44e4751380
  2. 1mo ago 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,879 stars, last pushed yesterday), licensed Apache-2.0. It adds 969 tokens to every session, about $0.0039 per session on Opus 5.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.