Skip to content
alberto bermudez
← Home
M.Sc. Thesis · TU BerlinSiemens Mobility · 20261.3 · sehr gut

Knowledge Graphs + RAG for safety-critical code.

A two-stage retrieval system that teaches LLMs the structural shape of a codebase — files, classes, functions, imports, and calls — before they answer a question about it. Built at Siemens Mobility for safety-critical codebases.

~30%
Onboarding ↓
1.3
Final grade · sehr gut
30k+
Files indexed
§01Motivation

LLMs answer code questions confidently and frequently wrongly. They've seen a lot of code on the open web, but they have no idea what's in your repository: which functions call which, which classes inherit from where, which file is the entry point. Vanilla RAG returns adjacent text chunks; it does not return structure.

At Siemens Mobility, that gap is especially expensive: safety-critical codebases are large, deeply structured, and onboarded slowly. New developers spend weeks tracing call graphs by hand before they can contribute. The thesis asked a focused question: can a structure-aware RAG system cut that ramp-up time materially?

The answer was yes — measured at roughly a 30% reduction in onboarding time on the codebases I evaluated.

§02System
  • Parser

    Tree-sitter parses Python, C, and C++ into a structured graph of files, classes, and functions.

  • Graph store

    Neo4j persists nodes (File, Class, Function, Concept) and typed edges (CONTAINS, IMPORTS, CALLS, EXTENDS, EXTRACTED_FROM, RELATES_TO).

  • Enrichment

    Every node gets an LLM-generated summary. The enrichment phase is checkpoint-resumable so it survives API rate limits and interruptions — important on 30k+ file repositories.

  • Embedding & retrieval

    FAISS indexes the node embeddings. At query time, vector recall finds candidate nodes; a graph traversal expands the context window using the typed edges.

  • Generation

    The structured context window — text plus graph relationships — grounds an LLM response with citations back to specific graph nodes. Engineers can verify the answer by inspecting the source it came from.

§03Why two stages

Vector search alone retrieves chunks that are textually similar. Graph traversal alone is precise but doesn't know what the question is about. Combining them gets you the candidates that matter and the structural neighborhood around them — which is what a senior engineer actually uses to answer a code question.

§04Result

On safety-critical Siemens Mobility codebases, the assistant reduced new-developer onboarding time by approximately 30% in user studies, with the strongest gains on tasks that required tracing cross-file behaviour.

Tested on repositories with 30,000+ files; indexing is resumable; the graph store handles the cardinality; retrieval stays interactive. The companion visualizer makes every retrieval step inspectable, which turned out to matter as much as the answer quality.

§05What's next

Productizing this into a tool anyone can point at a public repo: a GitHub URL goes in, a chat interface comes out, the companion visualizer overlays the retrieved graph so every answer is inspectable down to the node.

Read the code

The thesis repo includes the full pipeline, the Streamlit interface, and Neo4j schema. The companion visualizer is a separate React app for inspecting the graph interactively.