Providing Custom Reference Documentation for AI Code Optimization with code-graph-rag

The code-graph-rag repository implements a Retrieval-Augmented Generation (RAG) system that builds a semantic graph of your codebase, enabling LLMs to query precise, context-aware reference documentation for AI-powered code optimization.

This open-source toolkit transforms static source files into a queryable knowledge base. By combining abstract syntax tree (AST) caching, graph-based storage, and semantic embeddings, it delivers accurate code intelligence that outperforms simple text-based RAG approaches. Developers can integrate custom reference documentation into AI workflows for refactoring, debugging, and performance optimization tasks.

How code-graph-rag Structures Custom Reference Documentation

The system organizes code knowledge across three architectural layers. Each layer contributes specific capabilities for building and querying custom reference documentation.

Source Extraction and AST Caching

The codebase_rag/ast_cache.py module handles the foundation layer. It parses source files using Tree-Sitter and stores language-specific ASTs for fast reuse across multiple queries.

This caching strategy eliminates redundant parsing overhead. The AST representation preserves structural relationships—function definitions, class hierarchies, import statements—that raw text tokenization would lose. For AI code optimization, this structural context enables precise reasoning about code behavior and dependencies.

Graph Construction and Incremental Updates

Two modules collaborate to build the navigable knowledge graph:

  • codebase_rag/graph_loader.py – Transforms cached ASTs into a Neo4j-compatible graph structure with nodes for functions, classes, variables, and edges for relationships like "calls," "inherits," or "imports"
  • codebase_rag/graph_updater.py – Incrementally enriches the graph with runtime information including call-sites, type hints, and execution frequency data

This graph structure enables traversal-based queries that text embeddings alone cannot satisfy. For example, finding all functions that transitively modify a global variable requires graph navigation, not similarity search.

Semantic Query Engine

The codebase_rag/embedder.py module generates vector embeddings for both code snippets and natural language queries. It supports multiple LLM backends:

  • OpenAI embeddings
  • Cohere embed models
  • Custom embedding providers via configurable interface

In codebase_rag/main.py, the orchestration layer combines semantic similarity with graph traversal. Queries execute in two phases: initial vector retrieval identifies candidate nodes, then graph constraints refine results based on structural relationships.

Setting Up Custom Reference Documentation

Indexing a Repository

The CLI interface in codebase_rag/cli.py provides the entry point for building your documentation base:


# Install and index your target repository

pip install code-graph-rag
cgr index /path/to/project

# This creates a local graph database at ./graph.db

The indexing process executes the full pipeline: AST parsing, graph construction, and embedding generation. Progress displays through cli_help.py status messages.

Querying Your Documentation

Once indexed, reference documentation becomes queryable through natural language:


# Ask questions about implementation details

cgr ask "How does the caching layer invalidate entries?"

# The system retrieves relevant code context and feeds it to the configured LLM

Programmatic Integration for AI Optimization Workflows

For custom AI code optimization pipelines, instantiate CodeGraphRAG directly:

from codebase_rag.main import CodeGraphRAG

# Initialize with existing graph database

cgr = CodeGraphRAG(db_path="./graph.db")

# Execute semantic search with structural awareness

results = cgr.query(
    "Find all functions that modify the global variable `CACHE`"
)

for node in results:
    print(f"{node['name']} in {node['file']}")
    print(node["snippet"])
    print("---")

The returned nodes include file paths, line numbers, and code snippets—sufficient context for LLM-based optimization suggestions.

Exporting to External Graph Databases

For teams with existing Neo4j infrastructure:

from codebase_rag.graph_loader import GraphLoader

loader = GraphLoader(db_path="./graph.db")
loader.export_to_neo4j(
    uri="bolt://localhost:7687",
    user="neo4j",
    password="*****"
)

This enables integration with visualization tools and custom Cypher-based analytics.

Specialized Components for Advanced Documentation

Cross-Language Function Resolution

The codebase_rag/function_registry.py maintains a unified registry of known library functions and their signatures across languages. This resolves imports and external dependencies that AST parsing alone cannot trace.

When optimizing code that calls third-party libraries, this registry provides accurate type information and behavior contracts.

Dead Code Detection

The codebase_rag/dead_code.py module analyzes call graphs to identify unreachable definitions. For AI code optimization, this feeds critical context:

  • Eliminating dead code before optimization prevents wasted effort
  • Understanding true dependencies improves refactoring safety
  • Reducing graph size accelerates query performance

Dead code insights integrate directly into the RAG pipeline via graph_updater.py.

Language-Specific Parser Extensions

Language support extends through codebase_rag/constants/:

File Purpose
codebase_rag/constants/ast_python.py Python AST node definitions and visitor patterns
codebase_rag/constants/ast_java.py Java language-specific parsing rules

Adding new languages requires implementing the constant definitions and registering the Tree-Sitter grammar. The modular architecture isolates these changes without modifying core orchestration logic.

Optimizing AI-Generated Code Suggestions

The semantic graph enables specific optimization workflows unavailable to standard RAG:

  1. Impact Analysis – Query all call sites before modifying a function signature
  2. Type-Aware Refactoring – Traverse inheritance hierarchies to ensure polymorphic safety
  3. Performance Hotspot Identification – Correlate static call graphs with runtime profiling data ingested via graph_updater.py

Each workflow leverages the structured representation that code-graph-rag maintains, delivering higher-precision context than filesystem-based or simple vector retrieval systems.

Summary

  • code-graph-rag builds custom reference documentation through AST parsing, graph construction, and semantic embedding across three architectural layers
  • The ast_cache.py module enables fast, repeated querying through persistent AST storage
  • graph_loader.py and graph_updater.py maintain navigable code relationships in Neo4j-compatible format
  • embedder.py supports multiple LLM backends for flexible semantic search
  • CLI tools (cli.py, cli_help.py) and Python API (main.py) serve both interactive and programmatic use cases
  • Specialized components handle cross-language resolution (function_registry.py), dead code elimination (dead_code.py), and incremental graph enrichment

Frequently Asked Questions

Does code-graph-rag support languages beyond Python and Java?

Yes. The architecture is language-agnostic. New languages require adding parser constants in codebase_rag/constants/ and registering the corresponding Tree-Sitter grammar. The core graph and embedding layers operate on generic AST structures without language-specific assumptions.

Vector similarity finds textually or semantically related code but misses structural relationships. The graph representation in code-graph-rag captures call hierarchies, inheritance chains, and data flow dependencies. Queries like "functions that modify X and are called by Y" require graph traversal that pure embedding search cannot execute accurately.

Can I integrate custom embedding models for specialized domains?

Yes. The embedder.py module implements a configurable backend interface. Pass custom embedding functions through the initialization parameters in CodeGraphRAG, or modify embedder.py to support proprietary or fine-tuned models for domain-specific codebases.

What storage requirements does the graph database have?

The local graph database (default ./graph.db) scales with codebase size and AST complexity. Typical ratios are 10-50MB per 1000 source files for interpreted languages, higher for deeply nested static languages. The graph_loader.py export functions enable migration to managed Neo4j instances when local storage becomes limiting.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →