SourceStalecollected in 17h

AST Graphs + BM25 Slash LLM Context

Read original on Reddit r/MachineLearning
#rag#ast#retrieval#codebase

Cut codebase RAG context 20x using AST graphs + BM25—benchmarks inside

30-Second TL;DR

What Changed

Parses code files with Tree-sitter into AST for node/edge graphs

Why It Matters

Enables efficient RAG on large codebases without massive contexts, outperforming naive embeddings for code queries. Useful for developers scaling LLM use on repos.

What To Do Next

Parse your codebase with Tree-sitter and index BM25 on AST nodes for RAG testing.

Who should care:Developers & AI Engineers

Key Points

  • Parses code files with Tree-sitter into AST for node/edge graphs
  • BM25 scores node metadata like names and docstrings for retrieval
  • Graph traversal includes dependencies, reducing tokens to ~5K
  • Hierarchical fallback with Mermaid diagrams for complex queries

Deep Insight

AI-generated analysis for this event — not the original article.

Enhanced Key Takeaways

  • The approach leverages Tree-sitter's incremental parsing capabilities, allowing the graph to remain synchronized with real-time IDE edits without requiring a full re-parse of the codebase.
  • By utilizing AST-based node relationships, the system effectively mitigates the 'lost in the middle' phenomenon common in long-context LLMs by pruning irrelevant code blocks before they reach the prompt window.
  • Integration of BM25 with graph traversal enables a hybrid retrieval strategy that balances semantic relevance (via node metadata) with structural context (via dependency edges), outperforming standard vector-only RAG for code-specific queries.

Competitor Analysis

Context Efficiency
AST-Graph RAG
High (Pruned)
Standard Vector RAG
Low (Full chunks)
GraphRAG (Microsoft)
Medium (Community summaries)
Code Awareness
AST-Graph RAG
High (AST-based)
Standard Vector RAG
Low (Text-based)
GraphRAG (Microsoft)
Medium (Entity-based)
Implementation
AST-Graph RAG
Complex
Standard Vector RAG
Simple
GraphRAG (Microsoft)
Moderate
Benchmarks
AST-Graph RAG
Superior for code navigation
Standard Vector RAG
Better for general QA
GraphRAG (Microsoft)
Better for global insights

Technical Deep Dive

  • Node Representation: Each node in the graph stores a unique identifier, node type (e.g., function_definition, class_declaration), start/end byte offsets, and a serialized representation of its docstring/signature.
  • Edge Weighting: Edges are categorized by relationship type (e.g., 'calls', 'inherits_from', 'defines'), with weights dynamically adjusted based on the distance from the entry point node identified by the BM25 search.
  • Retrieval Pipeline: 1) BM25 ranks initial 'seed' nodes. 2) Breadth-first search (BFS) traverses dependency edges up to a depth of N. 3) The resulting subgraph is linearized into a context-optimized prompt format.
  • Hierarchical Fallback: Employs Mermaid.js syntax to generate structural summaries of the retrieved subgraph, allowing the LLM to understand the architecture before processing specific code snippets.

Future ImplicationsAI analysis grounded in cited sources

AST-based RAG will become the standard for enterprise-grade AI coding assistants by 2027.
The significant reduction in token consumption directly translates to lower operational costs and reduced latency compared to full-context window approaches.
Hybrid retrieval systems will render pure vector-based code search obsolete for complex refactoring tasks.
Vector embeddings struggle with the precise structural dependencies that AST-based graphs naturally capture.

Timeline

2023-05
Tree-sitter gains widespread adoption in AI coding tools for language-agnostic parsing.
2024-02
Initial research papers emerge on combining Graph Neural Networks with LLMs for code understanding.
2025-09
Early implementations of AST-derived graph RAG appear in open-source developer tool repositories.

Weekly AI Recap

Read this week's curated digest of top AI events →

AI-curated news aggregator. All content rights belong to original publishers.
Original source: Reddit r/MachineLearning

This is a summary, not the original. Read the source, or get the weekly briefing.

The weekly digest

One email a week. Unsubscribe anytime.