>_ DevTrendsen

Language

Home

Languages

Sections

Frontend Backend Mobile DevOps AI / ML GameDev Blockchain Embedded Security
Python

Knowledge Graphs Instead of Gigabytes of Embeddings for Code Analysis in Monorepos

A familiar situation: you feed a large monorepo to an AI assistant, and it starts mixing up function calls from neighboring services, losing connections between modules, and inventing non-existent arguments. Regular vector search is pretty good at finding similar text fragments. However, vector representations struggle with understanding the rigid structure of code, where a single renamed variable or implicit import changes the logic of the entire application.

Developer Vitaliy created the code-graph-rag project to solve this problem systematically. Instead of relying solely on vector embeddings, the tool builds a knowledge graph of the codebase.

demo

How It Works Under the Hood

The system is built on three core components: the Tree-sitter parser, the Memgraph graph database, and the Qdrant vector database.

Here's how it works:

  1. Tree-sitter parses source code files into abstract syntax trees (AST).
  2. The analyzer extracts nodes: functions, classes, methods, modules, and the relationships between them.
  3. Data is written to Memgraph under a unified, language-independent schema.
  4. On top of this structure, a CLI utility cgr translates natural language questions into precise Cypher queries to the graph database.

As a result, when you ask "where is function X used and where does its data flow to next?", the system doesn't try to guess the answer based on vector proximity in space. It executes a targeted query against the graph and returns the actual call path.

Исходный код -> Tree-sitter -> AST-анализ -> Граф знаний Memgraph
                                                  |
Запрос пользователя -> AI-модель (Cypher) -> Запрос Cypher -> Результаты

What the Tool Can Do

The project supports 13 languages, including Python, TypeScript, Go, Rust, Java, C++, C#, and PHP. Scala support is still in development, and Ruby uses ast-grep pattern matching for parsing.

Here are the main tasks cgr solves:

  • Navigation and Q&A about code. You can query function sources not only by name, but also by meaning or role in the architecture.
  • Dead code detection. The utility traverses the relationship graph from application entry points and finds functions or modules that are unreachable through any call chain.
  • Structural search and replace. Instead of classic regular expressions, it uses ast-grep. The tool searches for patterns in the syntax tree and carefully rewrites code while preserving structure.
  • Data flow tracking. The FLOWS_TO feature traces how a variable value passes through a chain of assignments, function calls, and ends up in I/O operations. Currently, tracing works for C#, Java, C, and Go.

An extra bonus is Model Context Protocol (MCP) support. This means code-graph-rag can be run as an MCP server and connected directly to Claude Code or any other MCP-enabled client. AI agents will be able to query the exact project structure on their own and suggest changes as ready-made diffs.

Quick Start and System Requirements

The tool is distributed as a Python package code-graph-rag. For full functionality, you'll need to install Docker (required for Memgraph and Qdrant containers), as well as the cmake and ripgrep system utilities.

The easiest way to install the CLI utility is via uv or pipx:

uv tool install "code-graph-rag[treesitter-full,semantic]"

After installation, you need to set up the local environment and start indexing the repository:

# Запуск контейнеров базы данных
cgr daemon up

# Индексация кодовой базы в новый граф
cgr start --repo-path /path/to/repo --update-graph --clean

# Интерактивный режим работы
cgr start --repo-path /path/to/repo

If you plan to use the utility as part of CI/CD or connect it to an agent via MCP, the documentation has a detailed section on configuration setup and graph export.

Is It Worth Adopting?

The project looks promising for teams working with multilingual monorepos. Regular AI assistants often fall short when the codebase exceeds a hundred thousand lines and service-to-service connections rely on function calls across different folders. The graph approach solves this problem radically, mixing strict code topology with the flexibility of language models.

Of course, you should consider the learning curve. You'll need to run a Docker container with Memgraph, allocate resources for graph construction during the initial scan, and configure LLM API keys. But if you're tired of neural network hallucinations during refactoring of a complex project, spending half an hour getting familiar with code-graph-rag is definitely worth it.

Related projects