leankg-graph-first

leankg-graph-first is a cursor rule for coding agents from FreePeak/LeanKG. It costs 522 tokens per session, scanned A, original, Apache-2.0.

A rule for using LeanKG, a code knowledge graph, before ordinary text search when its local service is available.

In plain words
What is it for?
Use it when navigating unfamiliar code, finding related functions, tracing connections, or deciding when to fall back to grep and file reading.
Why use it?
It helps the agent find code by meaning and relationships instead of searching files blindly.

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/freepeak/leankg/leankg-graph-first
Clone the repo
git clone --depth 1 https://github.com/FreePeak/LeanKG

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 leankg-graph-first

README.md
[![agentmods](https://agentmods.dev/badge/rules/freepeak/leankg/leankg-graph-first.svg)](https://agentmods.dev/rules/freepeak/leankg/leankg-graph-first)
Your own site
<a href="https://agentmods.dev/rules/freepeak/leankg/leankg-graph-first"><img src="https://agentmods.dev/badge/rules/freepeak/leankg/leankg-graph-first.svg" alt="Measured on agentmods" height="20"></a>
Per session 522 This file is loaded in full into every session.
When invoked 522 The same file — it is already loaded in full.
Security scan A 1 finding. 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.00522 $0.00522
Opus 5 $0.00261 $0.00261
Sonnet 5 $0.00104 $0.00104
Haiku 4.5 $0.00052 $0.00052

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

Security

Grade A, and why

leankg-graph-first scanned grade A with 1 finding 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 5d 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.

Makes network callslowCapability

Not a fault in itself. Listed so you know the mod talks to something, and to what.

curl -sf --max-time 2 http://localhost:9699/health
instructions/cursor-rules/leankg-graph-first.mdc · 49 lines

What it actually says

LeanKG Graph-First (HTTP-gated)

Gate (once per session)

curl -sf --max-time 2 http://localhost:9699/health
Health Mode
OK LeanKG MCP with container project= (e.g. /workspace for Docker)
Fail / timeout Grep / Glob / Read only — do not call LeanKG MCP

Prefer-order (discover before connection verbs)

For fuzzy / natural-language / “where is X?” questions, discover first — do not open with query_graph.

1. get_overview_context(project=…)   # session start
2. mcp_status(project=…)
3. concept_search → semantic_search → search_code / find_function
4. Connection verbs (only with known seeds / endpoints / symbols):
   path → shortest_path | explain → explain_node | query → query_graph
5. get_context / get_impact_radius / get_dependencies / get_dependents
6. Fallback to Grep/Glob/Read only when LeanKG is down or returns empty
Question type First tools
Fuzzy / meaning / domain NL concept_searchsemantic_searchsearch_code
Exact symbol / file name find_function / search_code / query_file
How A↔B? (known endpoints) shortest_path
What is this known symbol? explain_node
Expand subgraph after seeds query_graph (after semantic/concept hits)

BAN: Do not call query_graph as the first tool for NL discovery when embeddings/concepts may answer. Use semantic_search (and concept_search) first; then query_graph to expand the frontier.

Rules

  • Do not pass host Mac paths as project= against Docker MCP on :9699
  • Do not grep for code navigation when LeanKG is healthy and returns hits
  • Prefer get_context over reading entire source files when exploring indexed code
  • Hard-removed: wake_up, search_by_environment, mcp_hello, mcp_impact, get_doc_for_file, find_clones
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. 5d ago First seen · 49 lines · 522 tokens per session scan A c89afb2ace9b

Subscribe to this mod's changes

leankg-graph-first is a cursor rule published in the GitHub repository FreePeak/LeanKG (215 stars, last pushed 11d ago), licensed Apache-2.0. It adds 522 tokens to every session, about $0.0026 per session on Opus 5. A static security scan graded it A with 1 finding (makes network calls). No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.