What is a code knowledge graph?
The short answer
A code knowledge graph represents entities in a software repository and the relationships between them. Nodes may represent files, functions, classes or modules, while relationships may represent calls, imports, inheritance, containment or dependencies.
Turn it and see
A code knowledge graph is easier to trust once you have looked at one. The nodes here are the labels the extractor emits and the edges are the relationship types it writes, so this is the structure being described rather than an illustration of it.
Five related but distinct terms
AST, code graph, knowledge graph, dependency graph and call graph are often used loosely as if they meant the same thing. They don't: each represents a repository at a different level of detail, for a different purpose.
| Term | What it represents | Typical use |
|---|---|---|
| AST (abstract syntax tree) | The parsed grammatical structure of a single file (statements, expressions, functions) as a tree | Parsing, syntax highlighting, linting, code transformation |
| Code graph | Code entities (files, functions, classes) and the structural relationships between them across a repository | General-purpose structural navigation of a codebase |
| Knowledge graph | Entities and relationships modeled explicitly as nodes and edges, often with typed relationships | Querying how entities relate, beyond simple hierarchy |
| Dependency graph | Which modules, packages or files depend on which others | Understanding build order, blast radius of a change, package management |
| Call graph | Which functions call which other functions | Tracing execution paths, finding callers of a given function |
A code knowledge graph can be thought of as a knowledge graph whose entities and relationships happen to be drawn from code; it can encompass what a call graph and a dependency graph each represent, as specific relationship types within a broader graph.
A simple example
A small slice of a code knowledge graph for a checkout flow might represent one controller calling a service, which in turn uses a gateway and returns a result:
CheckoutController
|
| CALLS
v
PaymentService
|
| USES / RETURNS
v
Gateway / ResultCheckoutController and PaymentService are nodes; CALLS and USES / RETURNS are typed edges between them. A query against this graph can answer "what does CheckoutController depend on to complete checkout?" directly, by walking the edges, instead of inferring the answer from reading source text.
See a code knowledge graph in action
CodeMesh builds a structural graph of your repository and uses it to retrieve targeted context for coding agents.