# code-review-graph

> A local knowledge graph that maps codebases using tree-sitter, reducing Claude Code token usage by up to 49x on daily coding tasks.

12 March 2026 · [Read on Medium](https://tirthkanani18.medium.com/i-built-a-knowledge-graph-that-cuts-claude-codes-token-usage-by-49x-ca73ef078981)

32,000+ GitHub stars · up to 49× fewer tokens

[Source code](https://github.com/tirth8205/code-review-graph)

When Claude Code reviews a pull request, it reads your entire codebase to understand context. Every function, every import, every file gets serialised into tokens. For anything beyond a small project, this is wasteful. Most of the code has nothing to do with the change being reviewed. I built code-review-graph to fix that.

The tool creates a persistent, local knowledge graph of your codebase. It uses tree-sitter for static analysis, parsing source files into syntax trees without needing compilation. From those trees it extracts every function definition, class, import statement, and call site, then stores the dependency relationships between them as a directed graph.

When a diff arrives, the system identifies which symbols changed and walks the graph outward, typically two or three hops. That walk produces a focused set of code that is genuinely relevant to the change. Only that set gets fed to Claude as context. Everything else is left out.

The graph sits in memory using adjacency lists and rarely needs a full rebuild. File saves trigger incremental updates. On real pull requests, this reduced token usage by 6.8x for code reviews and up to 49x for routine daily tasks. The savings are largest on big repositories, which is precisely where costs hurt most.

Integration works through the Model Context Protocol. code-review-graph registers as an MCP server, and Claude Code queries it whenever it needs codebase context. The graph decides what is relevant. The model does the reasoning. Each handles what it is good at.

A question I kept returning to was how many hops to traverse. Too few and you miss a function three calls deep where the bug actually lives. Too many and you are back to feeding in half the codebase. Two hops with a configurable override covers the vast majority of real changes.

In September 2026 I demoed it live on stage at Claude Founder House in London. The example was a simple question: how does FastAPI build a route handler? Grep matches routing.py and hands the model all 4,912 lines. The graph knows where that function starts and ends, so it returns eight. Across 18 questions on six real repositories, an agent using the graph needed a median of 8.9 times fewer tokens to reach the same answer as a baseline agent.

The project is open source under an MIT licence and has picked up more than 30,000 stars on GitHub. That response suggests the problem resonates. If you use any LLM-based coding tool, the core idea applies: less context, more relevant context, better results.

---

By [Tirth Kanani](https://tirthkanani.com/index.md). Human version: https://tirthkanani.com/writing/code-review-graph
