← All articles

    GraphRAG for Codebases: When do we actually need AI?

    Refactoring code with LLMs isn’t new. But standard prompting quickly runs into some fundamental problems when you move beyond small, self-contained codebases:

    The AI Architecture Series

    GraphRAG for Codebases: When do we actually need AI?

    Using knowledge graphs to give LLMs the right context, enforce architecture and deliver accurate, reliable results.

    1. Large codebase

      Files, functions, classes, APIs, data, services

    2. Knowledge graphs

      Deterministic graphs from code structure and data schemas

    3. Relevant context

      Graph queries select exactly what the LLM needs to see

    4. LLM

      Explains, refactors or generates code with the right context

    5. Validation

      Check against target architecture, patterns and coding standards

    Not about more AI. About better context, deterministic foundations and reliable outcomes.

    • Manage context

      Query the graph to get the right context, every time.

    • Validate structure

      Autonomously check code against architecture and standards.

    • Focus on syntax

      LLMs handle the code. Graphs ensure the design is right.

    • Scale with confidence

      Works across large, complex and distributed codebases.

    Refactoring code with LLMs isn’t new. But standard prompting quickly runs into some fundamental problems when you move beyond small, self-contained codebases:

    1. Context limits — LLMs have finite context windows. What happens when you’re dealing with a large ERP system or thousands of enterprise-wide integrations? How do you summarise/compact the right memory?

    2. Finding the right context — Even if you could fit the entire codebase into the context window, how do you ensure the LLM focuses its attention on the right files, functions, dependencies and relationships?

    3. Hallucinations — How do you know the LLM’s recommendations are actually correct? A claim of 95% accuracy is only meaningful if someone has reviewed the entire codebase to establish what’s correct in the first place — which is precisely the problem we’re trying to solve.

    4. Architecture and coding standards — How do you enforce architectural constraints and coding standards with Markdown instructions alone? And, more importantly, can you reliably trust an LLM to follow them across a large and interconnected codebase?

    These aren’t necessarily problems that a better model, a bigger context window, or a better prompt can solve.

    This is where GraphRAG starts to become interesting.

    The approach

    I see two distinct stages in a GraphRAG workflow for codebases.

    1. Build the knowledge graphs deterministically — without LLMs

    I would create two complementary knowledge graphs.

    Data KG — Created by ingesting storage-layer metadata into a graph. The relationships are derived from the underlying database schemas and metadata rather than inferred by an LLM.

    flowchart TD
    	subgraph Data KG
        A(Database)-->|Extract|I(Schema)
        I-->|Metadata|J(Graph model)
        J-->K(Data KG) 
    end
    

    Application KG — Created using language-specific parsers to traverse the Abstract Syntax Trees (ASTs) of the files in the repository. Combined with lexical matching, this can map relationships between functions, parameters, classes, services and other code elements across files and repositories.

    flowchart TD
        subgraph Application KG
            B(Codebase repo)
        	B-->|Parse syntax|L(Abstract Syntax Tree)
        	B-->|Full-text search|M(Lexical search)
        	O(Graph model)
          	N(Application KG)
        	L-->O
        	M-->O
          	O-->N
        end
    

    The important point is that there is no AI involved in creating this structural representation.

    The graph is a deterministic description of the code and data relationships that can be derived from the underlying artefacts — much closer to what the compiler or interpreter actually sees than to an LLM’s interpretation of the code.

    2. Use the graph to control how AI interacts with the codebase

    Once the data and application codebases have been mapped into a graph, we can use that graph to:

    • Select the relevant context — Rather than passing an entire codebase to an LLM, use graph queries to identify the specific files, functions, dependencies and data relationships relevant to the task.
    • Ask the LLM to explain or refactor code — The LLM handles the language-level work while the graph provides the relevant structural context.
    • Validate the result — Define a target architecture, patterns and coding standards as a target knowledge graph, then analyse the LLM’s proposed changes against those constraints.
        flowchart TD
            subgraph Deterministic
            A(Data KG)
            B(Application KG)
            end
            A-->|Graph query|C
            B-->|Graph query|C
            subgraph Probablistic
            C(Prompt Interface)
            D(LLM)
          	end
          	C-->D
            D-->|Parse syntax & compare|Deterministic
    

    Conceptually, this starts to look a little like Test Driven Development (TDD): define what the result should look like, generate a solution, and then test the solution against those predefined expectations.

    So why put codebase metadata into a graph?

    There are several interesting possibilities.

    1. Context management

    If you can query the graph down to the function and parameter level, you have a mechanism for selecting the context that an LLM actually needs — potentially making both context length and summarisation much more manageable.

    2. Autonomous structural validation

    Instead of asking an LLM whether its proposed changes comply with the architecture, you can validate structural relationships against predefined architectural constraints.

    3. Separating reasoning from syntax

    If the target architecture is already represented in a knowledge graph, the LLM doesn’t necessarily need to invent the architecture. It can focus on translating the desired change into code while the surrounding system checks whether the result conforms to the target structure.

    And this isn’t just a theoretical direction. Commercial developer tools such as Sourcegraph and Glean are also exploring the use of code and organisational knowledge graphs alongside AI.

    For me, the interesting question isn’t:

    “How do we get an LLM to understand an entire codebase?”

    It’s:

    “How do we give an LLM exactly the context it needs — while keeping the things that need to be deterministic outside the LLM?”

    That’s where I think GraphRAG gets particularly interesting.

    This article shows the thinking behind our GraphRAG enablement engagement. Grounded AI-assisted refactoring.

    Discussion

    Get started

    Want this applied to your codebase?

    Request a discovery call. We’ll talk through your system, where it hurts, and whether a Codebase Knowledge Graph is the right fit.