AST Graphs + BM25 Slash LLM Context
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.
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
- AST-Graph RAG
- High (Pruned)
- Standard Vector RAG
- Low (Full chunks)
- GraphRAG (Microsoft)
- Medium (Community summaries)
- AST-Graph RAG
- High (AST-based)
- Standard Vector RAG
- Low (Text-based)
- GraphRAG (Microsoft)
- Medium (Entity-based)
- AST-Graph RAG
- Complex
- Standard Vector RAG
- Simple
- GraphRAG (Microsoft)
- Moderate
- AST-Graph RAG
- Superior for code navigation
- Standard Vector RAG
- Better for general QA
- GraphRAG (Microsoft)
- Better for global insights
| Feature | AST-Graph RAG | Standard Vector RAG | GraphRAG (Microsoft) |
|---|---|---|---|
| Context Efficiency | High (Pruned) | Low (Full chunks) | Medium (Community summaries) |
| Code Awareness | High (AST-based) | Low (Text-based) | Medium (Entity-based) |
| Implementation | Complex | Simple | Moderate |
| Benchmarks | Superior for code navigation | Better for general QA | 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
Timeline
- 2023-05Tree-sitter gains widespread adoption in AI coding tools for language-agnostic parsing.
- 2024-02Initial research papers emerge on combining Graph Neural Networks with LLMs for code understanding.
- 2025-09Early 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.