Labsco
FreePeak logo

Lean KG

โ˜… 199

from FreePeak

LeanKG: Stop Burning Tokens. Start Coding Lean.

๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅโœ“ VerifiedFreeAdvanced setup

LeanKG

LeanKG

Lightweight Knowledge Graph for AI-Assisted Development

LeanKG is a local-first knowledge graph that gives AI coding tools accurate codebase context. It indexes your code, builds dependency graphs, and exposes an MCP server so tools like Cursor, OpenCode, and Claude Code can query the knowledge graph directly. No cloud services, no external databases.

Visualize your knowledge graph with force-directed layout, WebGL rendering, and community clustering.

LeanKG Graph Visualization LeanKG Obsidian

See docs/web-ui.md for more features.


Live Demo

Try LeanKG without installing: https://leankg.onrender.com

leankg web --port 9000

How LeanKG Helps

graph LR
    subgraph "Without LeanKG"
        A1[AI Tool] -->|Full codebase context| B1[15,000-45,000 tokens]
        B1 --> A1
    end

    subgraph "With LeanKG"
        A2[AI Tool] -->|Targeted subgraph| C[LeanKG Graph]
        C -->|Context reduction| A2
    end

Without LeanKG: AI processes full context from files found via grep/search. With LeanKG: AI queries knowledge graph for targeted context. Token reduction varies by task complexity (see benchmark results).


Highlights

  • Auto-Init -- Install script configures MCP, rules, skills, and hooks automatically
  • Auto-Trigger -- Session hooks inject LeanKG context into every AI tool session
  • Token Optimized -- Targeted subgraph retrieval vs full file scanning
  • Impact Radius -- Compute blast radius before making changes
  • Pre-Commit Risk Analysis -- detect_changes classifies risk as critical/high/medium/low
  • Dependency Graph -- Build call graphs with IMPORTS, CALLS, TESTED_BY edges
  • MCP Server -- Expose graph via MCP protocol for AI tool integration (40 tools)
  • Orchestration -- Smart context routing with caching via natural language intent
  • Community Detection -- Auto-detect functional clusters in your codebase
  • Multi-Language -- Index Go, TypeScript, Python, Rust, Java, Kotlin, Ruby, PHP, Perl, R, Elixir, Bash with tree-sitter
  • Android -- Extract XML layouts, resources, manifest relationships, and navigation graphs
  • Service Topology -- Microservice call graph visualization
  • Annotation Search -- Search code by @Entity, @HiltViewModel, and other annotations
  • Graph Export -- Export as JSON, DOT, or Mermaid formats
  • REST API -- Full REST API with auth and API key management
  • RTK Compression -- Run shell commands with token-saving compression

See docs/architecture.md for system design and data model details.


Supported AI Tools

ToolAuto-SetupSession HookPluginFull Lifecycle Hooks
CursorYessession-start--
Claude CodeYessession-startYesSetup, SessionStart, UserPromptSubmit, PreToolUse, PostToolUse, Stop
OpenCodeYes-Yes-
DockerYes---
Kilo CodeYes---
Gemini CLIYes---
Google AntigravityYes---
CodexYes---

Note: Cursor requires per-project installation. The AI features work on a per-workspace basis, so LeanKG should be installed in each project directory where you want AI context injection.

See docs/agentic-instructions.md for detailed setup and auto-trigger behavior.


Context Metrics

Track token savings to understand LeanKG's efficiency.

leankg metrics --json              # View with JSON output
leankg metrics --since 7d           # Filter by time
leankg metrics --tool search_code   # Filter by tool

See docs/metrics.md for schema and examples.


Update

# Check current version
leankg version

# Update LeanKG binary (kills processes, removes old binary, installs hooks)
leankg update

# Or via install script
curl -fsSL https://raw.githubusercontent.com/FreePeak/LeanKG/main/scripts/install.sh | bash -s -- update

# Obsidian vault sync
leankg obsidian init                      # Initialize Obsidian vault
leankg obsidian push                      # Push LeanKG data to Obsidian notes
leankg obsidian pull                      # Pull annotation edits from Obsidian

Documentation

DocDescription
docs/cli-reference.mdAll CLI commands
docs/mcp-tools.mdMCP tools reference
docs/agentic-instructions.mdAI tool setup & auto-trigger
docs/architecture.mdSystem design, data model
docs/web-ui.mdWeb UI features
docs/metrics.mdMetrics schema & examples
docs/benchmark.mdPerformance benchmarks
docs/roadmap.mdFeature planning
docs/tech-stack.mdTech stack & structure
docs/android-extraction.mdAndroid XML & resource extraction

Performance Benchmarks

Load Test Results (100K nodes)

OperationThroughput
Insert elements~57,618 elements/sec
Insert relationships~67,067 relationships/sec
Retrieve all elements~418,718 elements/sec
Cache speedup (cold to warm)345-461x

Run load tests:

cargo test --release load_test -- --nocapture

A/B Benchmark Results

See tests/benchmark/results/clean-benchmark-2026-04-21.md for detailed A/B testing results comparing LeanKG vs baseline code search.