Skip to content
Cload Cloud
Developer Tools

lean-ctx

MCP server and context runtime for AI coding agents: session caching, AST-aware compression, and 90+ shell patterns to reduce token usage. Supports Claude Code,

What lean-ctx Does

lean-ctx is an MCP (Model Context Protocol) server and runtime environment designed specifically for AI coding agents working with Claude Code. It intelligently manages context windows by implementing session caching, AST-aware code compression, and 90+ pre-built shell patterns that drastically reduce token consumption. This is essential for developers and AI agents that need to maintain long coding sessions without hitting token limits or incurring excessive API costs.

The tool targets product designers and power users who leverage AI agents for code generation, refactoring, and debugging tasks. By compressing code representations while preserving semantic meaning, lean-ctx enables AI agents to operate on larger codebases and maintain richer context about project structure without the overhead of full source code transmission. It’s particularly valuable when working with Claude Code, where token efficiency directly impacts productivity and cost.

How to Install

Installation Steps

Prerequisites

  • Node.js 16+ or Python 3.8+
  • Git installed on your system
  • Claude Code integration enabled in your development environment
npm install -g lean-ctx

Installation from Source

  1. Clone the repository:
git clone https://github.com/yvgude/lean-ctx.git
cd lean-ctx
  1. Install dependencies:
npm install
# or
pip install -r requirements.txt
  1. Build the project:
npm run build
  1. Install globally (optional):
npm link

Configuration

  1. Create a configuration file in your project root (.lean-ctx.json):
{
  "sessionCache": true,
  "compressionLevel": "aggressive",
  "astAnalysis": true,
  "shellPatterns": "default",
  "tokenLimit": 100000
}
  1. Add lean-ctx to your Claude Code configuration in your IDE settings

  2. Restart your Claude Code integration to activate the MCP server

Verification

Verify installation by running:

lean-ctx --version
lean-ctx --test

Use Cases

  • Long-running coding sessions: Maintain context across multi-file refactoring projects that would normally exceed token limits, allowing AI agents to understand the full scope of changes needed across an entire codebase without losing context about previously analyzed files.
  • Cost optimization for enterprise teams: Reduce API costs for teams running multiple AI coding agents by 40-60% through intelligent context compression, making AI-assisted development economically viable for large-scale projects and continuous integration pipelines.
  • Real-time debugging large codebases: Compress repository structure and error traces while preserving AST relationships, enabling Claude Code to pinpoint bugs in complex systems faster without sending redundant source code representations.
  • Rapid prototyping with full context: Maintain rich context about project architecture, dependencies, and patterns across multiple sessions, allowing designers and developers to iterate quickly without re-explaining the codebase to the AI agent each session.
  • Shell script automation: Leverage 90+ pre-built shell patterns to automate common development workflows (builds, tests, deployments) while reducing token usage, making scripting suggestions from Claude Code more efficient and cost-effective.

How It Works

lean-ctx operates as an MCP server that sits between Claude Code and your development environment, intercepting code context requests and applying three core optimization strategies. When an AI agent requests file context, lean-ctx first checks its session cache to see if the file has been analyzed before. If it exists in cache with the same file hash, it returns the compressed representation directly, eliminating redundant processing. This session-level caching is crucial for multi-file operations where the same modules are referenced repeatedly.

The second optimization layer uses Abstract Syntax Tree (AST) analysis to compress code intelligently. Rather than sending raw source code, lean-ctx parses Python, JavaScript, TypeScript, and other supported languages into their AST representations, then performs semantic-aware compression. It preserves critical information like function signatures, class hierarchies, and dependency relationships while removing non-essential elements like comments, whitespace, and intermediate variable assignments. This maintains the AI agent’s understanding of code behavior while reducing token count by 50-70% for typical projects.

The third component provides 90+ pre-built shell pattern templates for common development tasks (running tests, building Docker images, deploying to cloud platforms, etc.). Instead of having the AI agent generate shell commands from scratch (which requires verbose context about the project setup), it can reference these patterns by name. For example, rather than explaining how to build a Node.js project, the agent can simply invoke the ‘node-build’ pattern, which lean-ctx expands with environment-specific parameters. This pattern-based approach reduces tokens needed to execute complex workflows by 30-40% while improving consistency and reliability.

Pros and Cons

Pros:

  • Reduces token usage by 40-60% through intelligent AST-aware compression and session caching
  • Transparent integration with Claude Code—no code changes required, works as MCP middleware
  • 90+ pre-built shell patterns for common development tasks reduce scripting overhead significantly
  • Local processing with encrypted caching means no code leaves your machine
  • Supports 8+ programming languages with full AST parsing, degrades gracefully for unsupported languages
  • Substantial cost savings for enterprise teams running AI coding agents at scale
  • Customizable patterns and compression levels allow tuning to specific project needs

Cons:

  • Initial setup and configuration required; not plug-and-play for all IDE environments
  • Performance overhead on first analysis pass while AST parsing occurs (negligible on subsequent cache hits)
  • Compression may sometimes lose non-semantic information that a human would find valuable (verbose error messages, detailed comments)
  • Requires Node.js or Python runtime—adds dependency for some lightweight development environments
  • Custom shell patterns need maintenance and updates as project build systems evolve
  • Limited effectiveness on very small projects or single-file codebases where compression overhead isn’t justified
  • Claude Code: The AI coding assistant integrated with lean-ctx; lean-ctx optimizes context delivery for Claude Code agents
  • Tree-sitter: A parser library for multiple languages that lean-ctx uses for AST generation and syntax-aware code analysis
  • Github Copilot: Alternative AI coding assistant; lean-ctx’s compression techniques apply similarly to other code AI tools
  • Local LLMs with llama.cpp: For teams running local language models, lean-ctx’s compression strategies reduce resource requirements
  • Conventional Commits: Git workflow tool that pairs well with lean-ctx’s shell patterns for automated commit and versioning workflows

Alternatives

  • Custom context management scripts: Teams can write bespoke Python or Node.js scripts to compress their specific codebases, but this requires manual maintenance and loses the pre-built patterns and AST-aware optimizations lean-ctx provides.
  • Manual codebase chunking: Breaking large projects into smaller submissions to Claude Code, but this fragments context and requires developers to manually track which files have been previously analyzed, losing session-level efficiency gains.
  • Prompt engineering alone: Instructing Claude Code to be concise or summarize code, but this is less effective than structural compression and doesn’t reduce actual token consumption, only works within a single conversation turn.
Glossary

Key terms

AST (Abstract Syntax Tree)
A tree representation of the syntactic structure of source code. lean-ctx uses ASTs to understand code semantics and remove redundant elements while preserving meaning, enabling intelligent compression that retains functionality information.
MCP Server (Model Context Protocol)
A protocol-based server that mediates between AI models and development tools. lean-ctx implements MCP to intercept context requests from Claude Code and apply optimization before context reaches the model.
Session Caching
Storage of previously analyzed and compressed code representations during a development session. lean-ctx reuses these cached compressions for subsequent requests about the same files, avoiding redundant analysis and token consumption.
Token
The unit of text that Claude API charges and consumes. Roughly 4 characters equals 1 token. lean-ctx reduces total tokens sent to Claude by compressing code and caching results, lowering both cost and response latency.
Shell Patterns
Pre-defined, reusable templates for common command-line operations (building, testing, deploying). Rather than sending verbose instructions, lean-ctx lets Claude Code reference patterns by name, significantly reducing context overhead.
FAQ

Frequently Asked Questions

How much can lean-ctx reduce my token usage?

Token reduction varies based on codebase characteristics, but typical improvements are 40-60% through AST compression, session caching, and shell pattern reuse. Projects with large utility files or repetitive patterns see higher reductions. You can test your specific codebase with 'lean-ctx --analyze' to get an estimate.

Is lean-ctx compatible with my existing Claude Code setup?

Yes, lean-ctx is designed as an MCP server that works with Claude Code without requiring code changes. It acts transparently between Claude and your IDE. Compatibility is tested with VS Code, JetBrains IDEs, and other Claude Code-integrated environments. Check the GitHub repository for your specific IDE version.

What programming languages does lean-ctx support for AST analysis?

lean-ctx provides full AST support for JavaScript, TypeScript, Python, Java, C++, C#, Go, and Rust. It also supports Go and Bash for shell operations. For languages without full AST parsing, it falls back to intelligent regex-based compression while still maintaining semantic integrity.

How does session caching work, and is my code secure?

Session caching stores compressed code representations locally in encrypted SQLite databases, identified by file hash rather than absolute paths. The cache is specific to your machine and project. No code is sent to external servers—everything processes locally. You can disable caching entirely in configuration if preferred.

Can I customize the 90 shell patterns or add my own?

Yes. lean-ctx supports custom pattern definitions in `.lean-ctx-patterns.json`. You can override default patterns and define new ones specific to your project's build system, deployment process, or development workflows. Patterns use template variables like `${PROJECT_ROOT}` and `${NODE_VERSION}` for dynamic substitution.

What happens if lean-ctx encounters code it can't compress?

lean-ctx gracefully degrades. If AST parsing fails for a file, it falls back to regex-based compression with minimal semantic analysis. If compression still exceeds configured thresholds, it truncates non-critical sections (tests, comments) or returns a stub representation. The agent continues working with reduced context but full functionality.

How do I monitor compression effectiveness and cache hits?

Run 'lean-ctx --stats' to see session metrics including cache hit rate, compression ratios by file type, and estimated token savings. Integration with Claude Code also provides real-time metrics in your IDE's output panel. Use '--verbose' flag for detailed per-file breakdown.

Does lean-ctx work with monorepos and multi-workspace projects?

Yes, lean-ctx is optimized for monorepos. It maintains separate caches per workspace root and understands workspace-level dependency relationships. For large monorepos, configure 'workspaceFocus' to only compress the packages the agent is actively working on, further reducing overhead.

More in Developer Tools

All →
Developer Tools

Webapp Testing

Tests local web applications using Playwright for verifying frontend functionality, debugging UI behavior, and capturing screenshots.

ComposioHQ