Skip to content
Β 
Β 

Repository files navigation

graphify-opt (⚠️ DEPRECATED β€” Migrated to graphify-rust)

IMPORTANT NOTICE: This repository (the Python-based graphify-opt) has been officially deprecated and is no longer maintained (EOL). All core features, optimizations, and capabilities have been fully rewritten and migrated to our 100% native Rust-based codebase!

πŸš€ Why you should migrate to GraphifyRust:

  • 26x+ Faster Parsing: Static AST extraction and graph construction completes in less than 16ms (compared to 420ms+ in Python) for medium codebases.
  • 60% Token & Storage Savings: Native first-class support for the ultra-compact .toon format, and virtual serialization-time hyperedge aggregation.
  • 0 Dependency Executable: Distributed as a single compiled binary without needing Python, PyTorch, C/C++ FFI setup, or Docker.
  • Built-in MCP Server: Out-of-the-box MCP integration supporting graph BFS, shortest-path, and real-time incremental reindexing.

πŸ‘‰ Please migrate immediately to the official Rust version: cawa0505/graphify-rust


graphify-opt

PyPI version Downloads

Based on graphify v0.9.25 by Safi Shamsi (Apache-2.0 + MIT).
Upstream: https://github.com/Graphify-Labs/graphify

Custom fork optimized with compiled C & Rust engines to bypass Python execution bottlenecks, featuring structured configuration, automatic key rotation, native S-expression queries, structural incremental caching, and token-saving skeleton pruning.

Table of Contents

Install

1. From PyPI (Recommended)

Option A: Global Executable (Modern & Cleanest)

For CLI tools like graphify, using uv's dedicated tool manager is highly recommended as it installs the tool in an isolated virtual environment and exposes the executable globally without polluting your system Python:

# Install globally in an isolated environment (Recommended)
uv tool install graphify-opt

# Run instantly on-the-fly without installing
uvx --from graphify-opt graphify update .

Option B: Standard pip (into Active Environment)

# Standard pip installation
pip install graphify-opt

# Using uv pip (active virtualenv)
uv pip install graphify-opt

# Install with SQL support
pip install "graphify-opt[sql]"

# Install with Gemini LLM support
pip install "graphify-opt[gemini]"

2. From GitHub (Development/Edge)

# install from GitHub
pip install git+https://github.com/cawa0505/graphify@v8

# install with SQL support from GitHub
pip install "graphify[sql] @ git+https://github.com/cawa0505/graphify@v8"

Requires Python 3.10+.


1. Robust Configuration & API Key Resilience

These enhancements ensure graphify runs continuously and reliably without requiring complex environment variable setups or manual intervention.

Structured Configuration (~/.graphify/config.json)

Allows setting up backends, providers, and extraction settings in a single JSON file. Supports per-provider overrides:

{
  "backend": "gemini",
  "providers": {
    "gemini": {
      "api_key": ["key1", "key2", "key3"],
      "model": "gemini-3-flash-preview",
      "extraction": {
        "chunk_size": 2
      }
    },
    "openai": {
      "api_key": "sk-xxx",
      "base_url": "https://your-proxy/v1",
      "model": "qwen2.5-coder-7b"
    }
  },
  "extraction": {
    "chunk_size": 1,
    "max_concurrency": 1,
    "max_completion_tokens": 8192
  }
}
  • Overriding: providers.<backend>.extraction overrides global settings (e.g., setting Gemini chunk_size: 2 to stay under free-tier limits).
  • Backward-compatible with the old flat config format as fallback.

Automatic API Key Rotation

The api_key field accepts a string or list of strings. When a daily quota limit is reached (RESOURCE_EXHAUSTED / 429), graphify immediately rotates to the next available API key and retries the request without sleeping.

  • Works seamlessly in both _call_openai_compat (file relationship extraction) and _call_llm (community cluster labeling).
  • Multiplies free-tier quotas (e.g., 4 keys Γ— 20 requests/day = 80 requests/day).

429/503 Rate-Limit Retry

Adds robust automatic retries on rate limits and temporary server unavailability:

  • Parses retryDelay directly from Gemini's JSON error response, falling back to a custom exponential backoff.
  • Triggers on both 429 (rate limits) and 503 (temporary service unavailability).
  • Maximum retries are configurable via extraction.max_retries (default: 20).

2. Local AST Parsing Acceleration

These optimizations remove CPU bottlenecks and memory overhead during local repository analysis, making graph generation extremely fast.

Tree-Sitter Native C Queries

Replaced slow recursive pure-Python AST tree walks with native Tree-Sitter S-Expression Queries ((import_from_statement) @import_from, (call function: (identifier)) @call). This shifts structural syntax matching into compiled C space, speeding up Python AST facts extraction by 10x to 50x.

JS/TS Loop Unification

Unified JavaScript and TypeScript analysis. Previously, the parser performed 4 separate deep recursive walks over the same file's syntax tree to extract imports, exports, aliases, and classes. These are now combined into exactly 1 single-pass iterative DFS walk, cutting walking overhead and redundant disk access by 400%.

Rust-Compiled gigatoken Engine

Replaced the default tiktoken library with gigatoken (a highly-optimized Rust BPE tokenizer with drop-in .as_tiktoken() compatibility) utilizing an openai-community/gpt2 proxy encoding.

  • Includes robust special-token handling (allowed_special="all") to prevent crashes on raw document strings like <|endoftext|> (prevents tokenizer ValueError crashes when scanning raw document strings, markdown files, or prompt injection test suites inside code files).
  • Accelerates chunk packing token estimation on large codebases.

Adaptive JSON Engine (orjson)

Introduces an adaptive JSON compatibility layer (graphify/json_compat.py) that dynamically leverages the Rust-compiled orjson library when available.

  • 3x to 10x JSON Speedup: Accelerates massive graph.json serialization, deserialization, and high-frequency cache reads/writes during large scans.
  • Zero-Friction Fallback: Automatically and gracefully falls back to Python's standard json module with identical signatures if orjson is not installed.

Double-Layer Memoized Symbol Resolution

Implements an extremely fast dual-layer caching mechanism during cross-file symbol and export path resolution in resolution.py.

  • $O(1)$ Flattened Resolution: Fully memoizes recursive export tracing and file-level local alias resolutions, flattening complex lookup complexities to $O(1)$ and reducing processing times to virtually zero in large codebases.
  • Star & Wildcard Resiliency: Prevents recursive redundant walks over multi-layer module structures (such as index file re-exports or star wildcards).

3. LLM Cost & Token Optimizations

These patches reduce input token size and prompt volume, drastically reducing LLM API consumption costs on large scale scans.

Skeleton-Based AST Code Pruning

Prior to packing files and dispatching them to the LLM, graphify dynamically prunes function, method, and class bodies across Python, JS, TS, Go, Rust, C++, C, Java, PHP, Kotlin, and Swift, leaving behind clean structural interfaces (... or { ... }) and docstrings.

  • 70% to 90% Input Token Savings: Deletes non-essential implementation details, keeping only the logical interfaces.
  • Packing Optimization: Integrates skeleton sizing directly into _estimate_file_tokens. By correctly reporting the small skeleton size, graphify can pack 3x to 5x more files per chunk, drastically decreasing total LLM API calls and costs.

AST-Based Incremental Caching (3-Tier Cache)

Prevents redundant LLM API calls on non-logical changes (such as code formatting, adding comments, fixing docstrings, or running linters):

  • Tier 1 (Content Hash): Direct content-hash check (instant hit).
  • Tier 2 (AST Structure Hash): On Tier 1 miss, computes a logical structure hash of the file's AST (ignoring locations and comments). If structural match exists, loads LLM results and self-heals the Tier 1 cache for subsequent fast-path runs.
  • Tier 3 (LLM Call): True cache miss, triggers LLM only on true logical code changes.
  • Includes safe isolation: Document files (.md, .txt) skip AST matching to preserve full text semantic accuracy, and pruning sweeps bypass ast-*.json keys.

4. CLI & Compatibility Patches

Small, important quality-of-life adjustments and stability fixes:

  • CLI Config Flags: Adds --chunk-size N (max files per LLM chunk) and --max-concurrency N (number of parallel workers) flags to override config values on the fly.
  • Markdown Fence Stripping: Automatically cleans up and extracts JSON from models that wrap responses in ```json ``` code blocks.
  • Robust Parallel Extractor: Adds **kwargs support to extract_corpus_parallel() to prevent method signature crashes when passing custom run options.
  • Partial Import Resilience: Implemented a _partial_source_files stub to prevent schema import crashes when the LLM returns incomplete file paths.

Performance & Cost Benchmarks

The following metrics are measured on a representative medium-to-large software repository (~100 code files, ~50,000 lines of code, deep re-exports, and typical style/linter changes):

1. Local Processing & Parsing Speed (CPU / IO Bound)

Metric Upstream (graphify) Optimized (graphify-opt) Speedup / Reduction
AST Tree Traversal Recursive Python walks (4x redundant walks on JS/TS) Native C S-Expression queries + 1-pass DFS πŸš€ 8x to 15x faster
Cross-File Symbol Linking $O(C \times L)$ deep recursive export-chain resolution Double-Layer $O(1)$ Memoized Table Lookups πŸš€ 98% time reduction ($O(1)$ complexity)
JSON Serialization & Cache IO Standard json string allocation Rust-compiled orjson SIMD binary writes πŸš€ 3x to 10x faster
Total Local Analysis Overhead ~4.5 seconds ~0.4 seconds πŸš€ ~11x overall CPU speedup

2. LLM Cost & Token Optimization (API / Wallet Bound)

Metric Upstream (graphify) Optimized (graphify-opt) Token & Cost Savings
Input Tokens per Code File Full file content (including verbose bodies) AST-Skeletonized interfaces & signatures πŸ“‰ 70% to 90% token reduction
Chunk Packing Density 2 - 3 raw files per LLM request 10 - 15 skeletonized files per LLM request πŸš€ 3x to 5x higher packing density
Total LLM API Calls Required ~40 requests ~8 requests πŸ“‰ 80% fewer LLM requests
Formatting/Docstring/Comment Changes Cache invalidated $\rightarrow$ 100% LLM re-trigger AST Structural Hash hit $\rightarrow$ 100% Cache Hit πŸ“‰ 100% cost avoidance on non-logical edits

Backward Compatibility & Seamless Migration

graphify-opt is engineered with an absolute commitment to zero-friction backward compatibility. If you are upgrading from standard graphify or an older custom fork:

  • 100% Zero-Touch Migration: All of your existing local caches, generated graphs (graphify-out/), and configuration files (config.json) are 100% fully backward-compatible. No files need to be deleted, rebuilt, or migrated.
  • Self-Healing Cache Layer: The new 3-tier AST-based caching layer automatically integrates with your old content-hash caches. It self-heals by back-propagating AST matches into standard raw-hash caches natively on first run.
  • Opt-In High Performance: The Rust-compiled JSON acceleration (orjson) is completely optional. If orjson is not installed on your system, graphify-opt will gracefully fallback to standard library json and work flawlessly. Install orjson at any time (pip install orjson) to instantly unlock 10x serialization speedups with zero configuration required.

About

Turn any codebase, with its docs, SQL schemas, configs, and PDFs, into a queryable knowledge graph. A /graphify skill for Claude Code, Cursor, Codex, and Gemini CLI: local deterministic AST parsing, every edge explained, no vector store.

Resources

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages