Why Knowledge Graphs Fail for Code Analysis (And What Actually Works)
I spent a week trying to use knowledge graphs to understand a legacy codebase. I thought it would help my AI coding assistant work better. I was wrong.
The knowledge graph produced pretty diagrams. But when I asked my AI questions about the code, it still gave me wrong answers. The graph didn’t capture what actually matters in code.
Here’s what I learned about why knowledge graphs fail for code, and what tools you should use instead.
The Problem: KG Output Doesn’t Match Code Reality
Knowledge graphs work great for Obsidian vaults. They show how your notes connect to each other. You can see which ideas link together.
But code is different. Code isn’t just nodes and edges. Code has:
- Syntax - the structure of functions, classes, and variables
- Semantics - what the code actually means and does
- Types - what data flows where
Knowledge graphs ignore all of this. They treat code like text files with some relationships. That’s not enough for understanding code.
I tried graphify on my project. It has 36.4k stars on GitHub. The description says:
“Knowledge graph for your codebase. Codex onboards to a new repo in a fraction of the usual time when this is plugged in.”
I was excited. But the output was just a bunch of connected boxes. It showed me that user.py connects to auth.py. But I already knew that from the imports. The graph didn’t tell me:
- What functions
auth.pyexports - What types
user.pyexpects - Where the bugs might be hiding
The Solution: Use AST and LSP Instead
For code analysis, you need tools that understand code. Not tools that treat code like a wiki.
AST (Abstract Syntax Tree)
AST breaks down your code into its actual structure. It knows:
- This is a function named
get_user - It takes two parameters:
idandinclude_deleted - It returns a
Userobject - It has three if statements inside
# Install tree-sitterpip install tree-sitter
# Parse your codebasetree-sitter parse ./my-repoThe output shows you the real structure of your code. Your AI assistant can use this to understand what functions exist and how they’re called.
LSP (Language Server Protocol)
LSP goes even deeper. It understands:
- Type definitions and imports
- Go-to-definition for any symbol
- Find all references to a function
- Rename operations that update all usages
# Rustrust-analyzer ./my-rust-project
# Pythonpylsp ./my-python-project
# TypeScripttypescript-language-server --stdioLSP is what your IDE uses. It’s battle-tested and accurate. When you ask “where is process_payment called?”, LSP gives you the exact locations.
Why This Matters: Wrong Tool = Wasted Time
I see developers spend days setting up knowledge graph tools for their code. They hope it will magically help their AI understand the codebase.
It won’t.
The knowledge graph output doesn’t plug into your AI’s understanding. Your AI needs structured data about:
- What functions exist
- What types they use
- How they call each other
AST gives you #1. LSP gives you all three. Knowledge graphs give you a nice picture that looks useful but isn’t.
Common Mistakes to Avoid
Mistake 1: Using KG Because It Looks Cool
Knowledge graph visualizations are pretty. They look impressive in demos. But ask yourself: what question does this help me answer?
If you can’t answer that, don’t use the tool.
Mistake 2: Thinking KG Will Help AI Onboarding
The graphify repo says it helps “Codex onboards to a new repo in a fraction of the usual time.”
Maybe it does help a little. But not because of the knowledge graph. It probably helps because it forces you to think about your codebase structure. You could get the same benefit from reading the directory structure.
Mistake 3: Ignoring Existing Tools
You already have AST and LSP tools in your editor. Use them.
- Run
tree-sitterto dump your code structure - Use your LSP to find definitions and references
- Feed that structured data to your AI
Tool | Understands Syntax | Understands Semantics | Good for AI------------|--------------------|------------------------|-------------KG | No | No | NoAST | Yes | Partial | YesLSP | Yes | Yes | YesWhen to Actually Use Knowledge Graphs
Knowledge graphs are great for:
- Personal note-taking (Obsidian)
- Research paper connections
- Concept mapping
- Wiki-style documentation
Just not for code.
If you want to try a “real” knowledge graph that’s designed for its purpose, look at arscontexta or similar tools built for actual knowledge management.
Summary
Knowledge graphs fail for code because they don’t understand code. They treat source files like documents. But code has syntax and semantics that require specialized tools.
Use AST (tree-sitter) for syntax-level analysis. Use LSP (rust-analyzer, pylsp) for semantic understanding. These tools are built for code and will actually help your AI assistant understand your codebase.
Stop wasting time on knowledge graphs for code. Start using the right tools today.
Final Words + More Resources
My intention with this article was to help others share my knowledge and experience. If you want to contact me, you can contact by email: Email me
Here are also the most important links from this article along with some further resources that will help you in this scope:
Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!
Comments