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.

TermWhat it representsTypical use
AST (abstract syntax tree)The parsed grammatical structure of a single file (statements, expressions, functions) as a treeParsing, syntax highlighting, linting, code transformation
Code graphCode entities (files, functions, classes) and the structural relationships between them across a repositoryGeneral-purpose structural navigation of a codebase
Knowledge graphEntities and relationships modeled explicitly as nodes and edges, often with typed relationshipsQuerying how entities relate, beyond simple hierarchy
Dependency graphWhich modules, packages or files depend on which othersUnderstanding build order, blast radius of a change, package management
Call graphWhich functions call which other functionsTracing 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:

relationship graph
CheckoutController
        |
        | CALLS
        v
  PaymentService
        |
        | USES / RETURNS
        v
 Gateway / Result

CheckoutController 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.

Start Saving TokensSee the Benchmark