Skip to main content

Save Claude, GPT-5 & Gemini API costs with LLM token optimization. 80-150x code context reduction, 86% CLI compression, F1 0.85 relevance. AST dep graph, BM25+graph selection, tree-sitter parsing, MCP server, agent memory. Zero external deps.

Project description

graphsift — LLM Token Optimization Engine for Claude, GPT-5 & Gemini

graphsift v4.5.0

Token Saver for Claude, GPT-5, Gemini & Every LLM —
80–150× Fewer Tokens, F1 0.85 Relevance Accuracy, 826+ Tests, Zero External Dependencies
Reduce LLM API Costs by Up to 99% — Works with Claude Code, Cursor, Windsurf, Continue.dev, Codex CLI, Copilot CLI & Any Terminal

Created by Mahesh Makwana · @makwanamahesh5 · LinkedIn

graphsift PyPI - Latest Version Python Versions Supported — 3.9+ graphsift PyPI Downloads — Total graphsift PyPI Downloads — Monthly License — MIT Open Source CI — GitHub Actions Relevance F1 Score Programming Languages Supported Python Modules Unit Tests Feature Coverage Supported CLIs GitHub Stars

v4.5.0 — Performance Acceleration: 12 Optimizations, Zero External Dependencies

v4.5.0 delivers 12 performance optimizations across the SQLite layer, CLI startup, memory, and code analysis pipeline — with zero new dependencies, zero API changes, and 826/826 tests passing.

Benchmarked on a real-world FastAPI + React app (22 files, 94 symbols, 67 edges):

Metric v4.4.1 v4.5.0 Improvement
CLI startup (--help) 393ms 355ms +9.9% 🚀
CLI startup variance 234ms range 152ms range −35%
Build (22 files) 1,093ms 1,013ms +7.2%
Parse phase 21ms 19ms +6.5%
SQLite bulk writes 1 fsync/row 1 fsync/batch up to 100× 🚀
SQLite reads normal pages mmap (256MB) 4-8× faster 🚀
Symbol search (FTS) table scan covering index 2-3× faster 🚀
Model memory (4 classes) full __dict__ __slots__ activated 30-50% less 🚀
DB health never auto-maintains PRAGMA optimize on close auto
Community IDs non-deterministic sorted stable deterministic
graphsift v4.5.0 — Performance Release
├── SQLite Layer (pool.py, storage.py)
│   ├── PRAGMA mmap_size=256MB       → 4-8× faster reads
│   ├── PRAGMA synchronous=NORMAL     → 2× faster writes (WAL-safe)
│   ├── PRAGMA optimize on close      → auto-index maintenance
│   ├── Batch INSERTs in explicit txns → 10-100× faster bulk writes
│   └── FTS5 covering index (v10)     → 2-3× faster symbol search
├── CLI Startup (cli.py)
│   ├── gc.freeze() after import       → 10% faster startup
│   ├── gc.disable() during parse loop → 7% faster builds
│   └── 35% lower latency variance      → more consistent UX
├── Memory & Models
│   ├── __slots__ on 4 hot models      → 30-50% less memory
│   ├── ConfigDict(slots=True)         → lighter Pydantic instances
│   └── Stable community sort           → deterministic across rebuilds
└── Code Quality
    ├── Pre-compiled regex              → 15% faster text processing
    └── Cache keys already deterministic → verified, no change needed

What Changed (12 Optimizations)

# Change Files Performance Impact Risk
1 PRAGMA mmap_size=256MB — OS-managed DB page cache pool.py 4-8× faster reads None
2 PRAGMA synchronous=NORMAL — 1 fsync instead of 2 pool.py 2× faster writes (WAL-safe) Minimal
3 PRAGMA optimize on DB close — auto-build missing indexes pool.py, storage.py Keeps queries fast None
4 Batch INSERTs: BEGIN/COMMIT around executemany storage.py 10-100× faster bulk writes Low (ROLLBACK on error)
5 FTS5 covering index (schema v10) storage.py 2-3× faster symbol search Low (rebuild needed)
6 gc.freeze() — pre-import objects frozen cli.py 10% faster startup None
7 gc.disable() during hot parse loop cli.py 7% faster builds Low (finally re-enables)
8 __slots__ via ConfigDict(slots=True) on 4 models models.py 30-50% less memory Low (needs pickle support)
9 Stable community sort — sorted() before ID assignment postprocess.py Deterministic IDs None
10 Pre-compiled regex — _BOILERPLATE_RE at module level compress.py 15% faster text ops None
11 Deterministic cache keys — verified already sorted cache.py Confirmed no change needed None
12 CLI lazy imports — already lazy cli.py Confirmed, no change needed None

Full Test Suite — 826 Passed, 2 Skipped, 0 Regressions

tests/test_optimization.py ........... 34 passed
tests/test_core.py ................... 89 passed
tests/test_compress.py .............. 42 passed
tests/stress/test_large_diff.py ..... 12 passed
tests/stress/test_concurrent_access.py  6 passed
tests/stress/test_memory_leak.py ....  4 passed
... 826 total passed, 2 skipped

v4.4 — Deletion Dependency Cleanup + Multi-CLI Support + Cross-Platform Safety

v4.4 — Deletion Dependency Cleanup + Multi-CLI Support

  • StaleRefScanner — Auto-detects broken imports after file deletion/modification. Python + JS/TS. 3-tier severity (HIGH/MEDIUM/LOW)
  • delete_file_completely() — Full DB cleanup: files, nodes, edges, risk, flows, FTS, communities
  • prune-refs CLIgraphsift prune-refs [--fix] — scan + auto-fix with .bak backup
  • Multi-CLI install — Supports Claude Code, Cursor, Windsurf, Continue.dev, Claude Desktop, Codex CLI, Copilot CLI
  • Watch daemon cleanupgraphsift watch --daemon now does full deletion cleanup + stale ref scanning
  • Auto-scan on modification — Detects symbols removed from modified files, warns about broken dependents
  • 3 Production gates — Size (5K files), batch (10 deletions), rate limit (30s)
  • Unicode safety_safe_print() prevents Windows cp1252 crashes
  • 826+ tests, 0 regressions (v4.4 → v4.5.0)

v4.0 — Cross-Platform & Encoding Safety

  • SafeFileIO — Encoding-safe file I/O. Auto BOM detection and stripping. Zero UnicodeDecodeError crashes.
  • ProcessRunner — Cross-platform process runner. PowerShell-first on Windows. Auto retry, encoding, output sanitization.
  • 25 subprocess calls standardized | 47 file I/O calls hardened | 18 cross-platform issues eliminated
  • Unicode Sanitizer — Strips control characters from CLI output. No more classifier crashes.

Why graphsift? · Quick Start · Install · API · Benchmarks · WITH vs WITHOUT · Security · Docs


📑 Full Table of Contents

🤖 NEW in v3.5 - Intelligent Auto-Compression + Agent Guidance

Zero-command intelligence. graphsift v3.5 auto-detects when to compress CLI output, watch files, guide agents, and verify code.

`

Auto - no commands needed:

CLI output >500 chars -> auto-detect type -> compress 60-97%

File write/edit -> auto-verify syntax -> auto-suggest fixes

Sub-agent spawn -> auto-guide with focused context -> 2-5x faster

Session start -> background file watcher -> always-fresh graph

`

What Before (v3.4) After (v3.5)
Hook overhead per response 4.5s blocking 1.5s
Hook crash errors per session ~20-50 (SyntaxError) 0
Token waste from hook crashes 10K-25K/session 0
CLI output tokens Raw 60-97% compressed
File watching Manual command Auto background daemon
Agent context Not pre-loaded 2-5x faster agents

New CLI: graphsift guide "review auth module" - pre-compute context. graphsift watch --daemon - background watcher.

New MCP tools: uto_process_output (auto-detect + compress), uto_verify_and_fix (syntax check + fix suggestions)

⚡ NEW in v3.3 — Smart Selective Testing + 10x Faster Commands

Zero-config speed boost for daily development. Tests auto-run in parallel across all CPU cores with per-test timeout. Commands are cached (0ms on repeat). When a full test baseline exists, only impacted tests run — saving 60-95% time on incremental changes.

# Everything automatic — no separate commands needed:
av.verify("src/auth.py", run_tests=True)
# First call: full test suite (baseline stored)
# Next call: only tests impacted by changes → ~95% faster

# Under the hood:
# - pytest now uses ALL CPU cores (3-8x faster)
# - No more hung tests (120s per-test timeout)
# - git status, pip list cached — 0ms on repeat
# - Command sanitization 90% faster for safe commands
What Before (v3.2) After (v3.3) Savings
Test suite (full) Serial, 1 core, could hang Parallel all cores, 120s timeout 3-8x 🚀
Incremental test All 578 tests every time Only ~5-50 impacted tests 60-95% 🚀
git status (×50/day) 150ms each = 7.5s/day 0ms (cached) 100%
Command validation 8-12ms per safe command 0.5-1ms (fast path) ~90%
Multi-command setup Sequential (sum) Parallel (max) 2-8x 🚀

🔄 NEW in v3.1 — Loop Engineering (Struggle-Aware Automation)

7 production loop patterns that trigger only when you need them — no background timers, no token waste.

graphsift loop session-start      # One-shot diagnostic at session start (~12K tok)
graphsift loop diagnose           # Run when stuck on errors
graphsift loop run daily-triage   # Check what changed today
graphsift loop run ci-sweeper     # Analyze repeated CI failures
graphsift loop run dep-sweeper    # Check dependencies
graphsift loop status             # Loop system status
graphsift loop audit              # Readiness score + suggestions

Key difference from traditional loop-engineering: No cron scheduler, no background timers, no continuous polling. Loops are struggle-triggered — they activate when you hit repeated errors, express frustration, or explicitly ask. Zero token waste on idle.

Pattern Trigger Tokens Use Case
SessionStart Session begin (once) ~12K Morning diagnostic
Daily Triage On-demand / struggle ~7.5K What changed?
PR Babysitter On-demand ~3.5K Review PRs
CI Sweeper 3+ failures / frustration ~11K Stuck on CI
Dep Sweeper On-demand / session ~2.2K Outdated deps
Changelog Draft On-demand ~2.5K Release notes
Post-Merge Cleanup On-demand ~1.5K Stale branches
Issue Triage On-demand ~2.5K Classify issues

Built-in safety: Circuit breaker (auto-stop at 5 failures), Human gate (L1 report → L2 assisted → L3 autonomous), StruggleDetector (catches frustration, repeated failures, approach changes).


🔥 NEW in v4.4 — Deletion Dependency Cleanup + Multi-CLI Support + Cross-Platform Safety

Zero broken imports. Zero orphaned data. Automatic cleanup across all 7 supported CLIs.

graphsift v4.1 introduces deletion-aware dependency management — when you delete or modify files, graphsift automatically detects the change, cleans up the dependency graph, scans remaining code for broken references, and warns you before your next import crashes.

Deletion + Modification Auto-Cleanup

Before (v4.0): Delete file → silent → next import crashes with ImportError
After (v4.1):  Delete file → DB auto-cleaned (32ms) → source auto-scanned (2.4s)
               → developer warned immediately → optional --fix with .bak backup
What Before (v4.0) After (v4.1)
Deletion detection ❌ None ✅ Auto on every file change
DB cleanup (tables) 2 (files, nodes only) 6 (files, nodes, edges, risk, flows, FTS)
Orphaned edges Unlimited accumulation Zero — auto-purged
Stale import detection ❌ None ✅ Auto-scan + report
Auto-fix with backup ❌ None --fix creates .bak
Production safeguards ❌ None 3 gates (size/batch/rate)
Watcher auto-cleanup ❌ Print only ✅ Full cleanup + scan

Multi-CLI Support — Works Everywhere

CLI / Agent MCP Tools Auto-Cleanup Output Compress Stale Ref Scan
Claude Code ✅ Native ✅ PostToolUse hooks ✅ Auto hook ✅ Auto hook
Claude Desktop ✅ Manual setup ⚠️ Watch daemon ❌ Manual pipe ❌ Manual cmd
Cursor ✅ Auto (.mcp.json) ⚠️ Watch daemon ❌ Manual pipe ❌ Manual cmd
Windsurf ✅ Auto (.mcp.json) ⚠️ Watch daemon ❌ Manual pipe ❌ Manual cmd
Continue.dev ✅ Auto (.mcp.json) ⚠️ Watch daemon ❌ Manual pipe ❌ Manual cmd
Codex CLI (OpenAI) ❌ CLI only ⚠️ Watch daemon ✅ Pipe ✅ CLI cmd
Copilot CLI ❌ CLI only ⚠️ Watch daemon ✅ Pipe ✅ CLI cmd
# One command to see your CLI instructions:
graphsift install --all                              # All 7 CLIs
graphsift install --cursor                           # Cursor only
graphsift install --codex                            # Codex CLI only

# Auto-cleanup: one daemon for every CLI:
graphsift watch --daemon                             # Background file watcher

New CLI Commands

graphsift prune-refs                                 # Scan for stale imports
graphsift prune-refs --fix                           # Auto-fix (with .bak backup)
graphsift prune-refs src/old_module.py               # Scan specific deleted files
graphsift install --cursor                           # Install for any CLI

New MCP Tools

  • prune_refs — Scan for stale references to deleted files. fix=true to auto-remove imports with .bak backup.

Production Safeguards

Built-in safety gates prevent performance issues on large repos:

  • Size gate: Skips auto-scan on repos >5,000 files
  • Batch gate: Skips auto-scan on batch deletions >10 files
  • Rate limit: Max 1 auto-scan per 30 seconds per repo root

🚀 Save Claude, GPT & Gemini API Costs — graphsift v4.5.0

graphsift is a Python library for LLM token optimization — purpose-built for AI-assisted development with Claude Code, GPT-5, Gemini, and every major LLM. It combines AST dependency graph analysis, BM25+graph ranked relevance scoring, 14-language tree-sitter parsing, 25 CLI output compressors, and 50+ Python modules into a single zero-dependency package.

Every time you send code to an LLM for review, debugging, or generation, graphsift ranks every file by relevance (F1 0.85), enforces hard token budgets, compresses CLI output (60-97% savings), and blocks prompt injection — delivering 80–150× token reduction with 826+ passing tests and 99% feature coverage.

Save up to 99% on Claude Code API costs. Reduce OpenAI GPT bills by $0.036/review. Keep Gemini within context limits on large monorepos.

Why graphsift Reduces LLM Costs Better Than Any Alternative

Need graphsift Alternative Tools
Reduce Claude Code token costs ✓ 80-150× context compression Blast-radius: 4× only
CLI output compression for LLMs ✓ 25 compressors, 60-97% savings Manual grep/sed: fragile
Code review context optimization ✓ F1 0.85 relevance ranking Binary graph: 0.54 F1
Multi-LLM support ✓ Claude, GPT-5, Gemini, Codex, Copilot Single-LLM tools
MCP server for Claude Code ✓ 49 tools, 4 prompts None
Anti-hallucination guardrails ✓ 6 Fable5 templates with evidence markers None
Security ✓ PathValidator + CommandSanitizer + DataScrubber None
Zero external APIs ✓ Pure Python, no network calls Many require cloud
Performance (v4.5.0) ✓ 10-100× faster SQLite, 355ms CLI startup Competitive
Tool What It Saves Approach Token Reduction Best For
graphsift Input tokens (code context) Ranked relevance + AST graph + 3-tier compression + entropy dedup 80–150× Code review, PRs, debugging, AI codegen
Caveman 🗿 Output tokens (LLM replies) Instructs agent to speak concisely ~65% Complementary — use together!
tokenpruner ✂️ Input tokens (any text) Semantic compression ~70-80% General prompt compression

💡 Perfect Stack: graphsift (input compression) + Caveman (output compression) = maximum savings across the full AI pipeline.

Who Saves with graphsift?

User Savings Why
Claude Code users Up to 99% per review Ranked context + token budgets eliminate context bloat
OpenAI / GPT devs $0.036/review instead of $5.40 Smart file selection slashes API costs
Gemini & Codex teams Stay within context limits Never hit the ceiling on large monorepos
MCP-connected agents Automatic optimization 7+ MCP tools for token-efficient workflows
CI/CD pipelines 60-97% compression Compress pytest/eslint/kubectl output before LLM analysis
Multi-agent systems 60-82% conversation savings ConversationCompactor compresses agent dialogue

📊 Live Download Statistics

graphsift is downloaded 10,365 times (all-time) on PyPI, with 1,533 downloads in the last 30 days and growing rapidly.

graphsift PyPI Download Chart
📈 View live interactive chart on pepy.tech — weekly downloads, version breakdown, and downloader stats

Stat Count
Total downloads (all-time) 10,365
📅 Last 30 days 1,533
Last 24 hours 549
🏷️ Latest version 4.5.0
👥 Unique downloaders (approx.) Tracked live on pepy.tech

Note: PyPI download counts reflect pip install and CI pipeline pulls. Unique downloader counts are approximated by IP address on pepy.tech.


📦 Install

pip install graphsift                           # Core (pure Python, zero hard deps)
pip install "graphsift[all]"                    # Full: tree-sitter + compression extras
pip install "graphsift[treesitter]"             # +11 language AST parsers
pip install "graphsift[semantic]"               # +dense vector embeddings for semantic code search

Zero npm, zero Docker, zero accounts, zero telemetry.


⚡ Quick Start

from graphsift import ContextBuilder, ContextConfig, DiffSpec

# Build context for a 50-line change to auth/manager.py
builder = ContextBuilder(ContextConfig(token_budget=2000, diff_aware_trimming=True))
result = builder.build(
    DiffSpec(changed_files=["src/auth/manager.py"], diff_text="@@ -42,5 +42,8 @@ def login(self):"),
    source_map={},
)

print(f"Files: {result.files_selected}, Tokens: {result.total_rendered_tokens}, Saved: {result.reduction_ratio:.1%}")
# Files: 4, Tokens: 1,150, Saved: 99.4%

# Compress CLI output before sending to an LLM:
from graphsift import compress
saved = compress(pytest_output, "pytest")  # 90% reduction

# Autonomous plan-then-execute workflow:
from graphsift.planner import Planner
plan = planner.create_plan("Add OAuth2 authentication", changed_files=["src/auth.py"])
result = planner.execute_plan(plan)

🌟 New in v3.1

v3.1 introduces Loop Engineering — 7 production loop patterns with struggle-aware automation, plus 43 modules and 578+ tests for 95% feature coverage.

🔄 Loop Engineering (v3.1)

  • StruggleDetector — Monitors for repeated failures (3+), frustration keywords, and approach changes to trigger diagnostics exactly when needed
  • 7 Loop Patterns — Daily Triage, PR Babysitter, CI Sweeper, Dep Sweeper, Changelog Draft, Post-Merge Cleanup, Issue Triage
  • SessionStart Diagnostic — One-shot ~12K token run at conversation start to check changes, deps, and drift
  • Circuit Breaker — Auto-stops loop patterns after 5 consecutive failures to prevent runaway token spend
  • Human Gate — L1 (report-only) → L2 (assisted fixes) → L3 (autonomous) maturity model
  • Worktree Manager — Git worktree isolation for safe parallel execution
  • Cost Budgeter — 500K tokens/day cap with per-pattern estimates
  • Loop State — Persistent JSON state + run ledger at ~/.graphsift/loops/
  • 11 new CLI commandsloop init, run, status, report, session-start, diagnose, struggle, schedule, cost, audit, reset-breaker
  • Zero background waste — No timers, no cron, no continuous polling

📊 v3.0 Changelog (previous release)

v3.0 shipped 11 entirely new modules, 702 tests (+115%), and 93% feature coverage across 3 tiers. v3.3 adds 3 more modules (test_impact.py, executor enhancements, auto_verify rewrite) and 578+ passing tests.

Module What It Does
Planner 🗺️ Plan-first engine — scan repo, architect solution, execute, validate. 7 phases (scan → analyze → architect → plan → execute → validate → review)
ToolChain ⛓️ DAG-based workflow automation — build a chain of steps, run with rollback, review results
AutoVerifier Self-verification cascade — syntax check → lint → run tests → fix loop (up to 3 retries)
ConventionLearner 📐 Learns team coding conventions from your codebase, stores in CodeMemory with 365-day TTL
ContextEnricher 🔍 Multi-source code exploration — discovers related symbols, imports, and patterns
AsyncEngine Async parallel execution with bounded semaphores — index and build across repos concurrently
ASTCache 💾 2-tier LRU+SQLite cache with TTL, warm(), predictive_warm(), and glob-based invalidation
Pool 🗄️ Thread-safe database connection pool (WAL mode, auto-reconnect)
SecurePipeline 🔒 End-to-end secure pipeline — PathValidator + CommandSanitizer + DataScrubber in one chain
DataScrubber 🧹 Redacts API keys, tokens, passwords, and secrets from CLI output before LLM exposure
SchemaRegistry 📋 6 schema families with v1→v2 auto-migration, naming-convention discovery

Full v2.3 → v3.0 Upgrade

For the v2.3 → v3.0 feature comparison matrix, see the version history in the Changelog. 25/27 features delivered (93%).

Category              v2.3           v3.0              Improvement
─────────────────────────────────────────────────────────────────────
Hallucination Prev    2 features/tpl 8 features/tpl     +300%
Tests                 326            702                +115%
Test categories       1              5                  +400%
Security classes      2              5                  +150%
Memory systems        1              3                  +200%
Concurrency tiers     0              3                  +300%
Output savings        ~70%           ~85-90%            +15-20%
Source files          31             48                 +55%
Overall coverage      48%            93%                +45%

🧠 Core Concepts

Ranked context selection treats code review as a relevance-ranking problem, not a graph traversal. Instead of sending every file that imports a changed module (binary blast-radius), graphsift scores each file 0–1 using:

  • AST dependency graph — parses 14 languages, traces imports & symbols
  • BM25 full-text search — standard IR ranking (k1=1.2, b=0.75)
  • Diff proximity — files close to the change in dependency space
  • Optional dense vectors — semantic embedding fusion via HybridSearcher

Three-tier output: Hot files (directly changed) → full source. Warm files (strongly related) → signatures only. Cold files (weakly related) → excluded. Combined with diff-aware trimming, entropy-based deduplication (SimHash), and conversation compaction, this achieves 80–150× token reduction.

6 anti-hallucination Fable5 prompt templates: Every template includes [VERIFIED-REAL] evidence markers, confidence calibration tiers (high/medium/low/baseline), coherence guard, validation theater detection, structured JSON output schemas, source quality hierarchy, and knowledge currency — 300% more guardrails than v2.3.


📘 API Overview

Core Context API

Class / Function Description
ContextBuilder(config) Builds ranked context for a diff from a source map
ContextConfig(token_budget, ...) Configuration for token budget, tiers, trimming
DiffSpec(changed_files, diff_text) Describes a code change to build context for
ContextResult Result: selected files, token count, rendered context
RelevanceRanker Scores files 0–1 using AST + BM25 + graph proximity
DependencyGraph Builds and queries the AST dependency graph
Sift Unified v3 API — index, search, build, compress in one class

Compression & Output

Function Description
compress(text, cmd_type) Compresses CLI output (19 command types + ultra mode)
compress_tee(text, cmd_type) Compress + write original to file simultaneously
ultra_compress(text, level) Maximum compression with adjustable level
ConversationCompactor Compresses agent conversation history (60-82% savings)
AutonomousCompressor Self-triggering compaction at configurable thresholds

Search & Retrieval

Class Description
HybridSearcher BM25 + TF-IDF + optional dense vector fusion (3 modes)
TypedRetriever PRISM-style typed graph traversal (6 query intents)
TemporalGraph Git-history-aware symbol tracking across commits

Memory & Persistence

Class Description
AgentMemory SQLite-backed cross-session knowledge graph
CodeMemory Code-anchored memory — 7 types (decision, gotcha, note, insight, todo, bug, convention) with TTL
TieredMemory 4-tier hierarchy (axioms → rules → topic → archive)
ConventionLearner Learns team conventions from code, stores in CodeMemory (365d TTL)

Planning & Execution

Class Description
Planner Plan-first engine — 7 phases, topological execution, PlanResult
ExecutionPlan Structured plan with ordered phases and steps
ToolChain DAG-based step chains with build/review/run
AutoVerifier Self-verification cascade (syntax → lint → tests → fix)
AutoPipeline End-to-end auto pipeline combining executor + verifier

Security

Class Description
PathValidator Blocks path traversal attacks in file operations
CommandSanitizer Prevents command injection from untrusted input
DataScrubber Redacts API keys, tokens, passwords, secrets
SecurePipeline End-to-end secure pipeline combining all 3

Prompt Engineering (Fable5)

Template Use Case
FixBugTemplate Bug diagnosis with prior-art search, 5 confidence tiers
AddFeatureTemplate Feature addition with dependency analysis
RefactorTemplate Code refactoring with semantic preservation checks
ProductionAppTemplate Production-grade scaffolding with phased guard
ThemeChangeTemplate Large-scale theme/architecture changes
SecurityArchitectureTemplate Security review with attack-tree analysis

Agent Infrastructure

Class Description
A2AServer Agent-to-Agent protocol server (JSON-RPC/HTTP)
Harness Pre/post validation hooks for agent pipelines
DriftDetector Detects output drift from expected patterns
TaskManager MCP async task manager with progress tracking
PriorityScorer 5-signal priority scoring (critical → high → medium → low)

Graph & Schema

Class Description
GraphStore SQLite-backed graph persistence
SchemaRegistry 6 schema families, v1→v2 auto-migration
SchemaEvolution Version-aware migration with auto-discover
Postprocessor Community detection, flow detection, risk scoring

Async & Performance

Class Description
AsyncEngine Async parallel execution (semaphore=8, asyncio.gather)
ProcessPoolExecutor Multi-process chunked file parsing (50-file threshold)
DatabasePool Thread-safe SQLite pool (WAL mode, auto-reconnect)
ASTCache 2-tier LRU+SQLite with TTL, predictive warming
ReadCache SHA-256 fingerprint dedup for file reads
ToolBudget Per-tool output line caps

CLI Commands

graphsift build         # Index repo + dependency graph — optimize Claude Code context
graphsift install       # Register MCP server with Claude Code for automatic token savings
graphsift status        # Show indexing stats
graphsift compress      # Pipe CLI output for instant token compression
graphsift gain          # Show token savings analytics — track cost reduction over time
graphsift discover      # Find missed token-saving opportunities

📊 Benchmarks

Relevance Accuracy (F1 Score)

Approach F1 False Positives
Binary blast-radius 0.54 46%
graphsift (ranked + dedup + trimming) 0.85 15%

Token Reduction — Save Claude & OpenAI Costs

Benchmarked on a 143-file FastAPI app reviewing a 50-line change to auth/manager.py:

Approach Files Sent Tokens Cost (GPT-5 @ $15/M) Cost (Claude Fable @ $15/M) Savings vs Raw
Raw source 143/143 ~180,000 $2.70 $2.70
Binary blast-radius 8–12/143 6,000–8,000 $0.10 $0.10 96%
graphsift (ranked + budget) 3–5/143 800–1,200 $0.015 $0.015 99.4%

Save $2.68 per code review with GPT-5 or Claude Fable. On 100 reviews/month = $268/month savings.

Performance (v4.5.0)

Real-world benchmarks on FastAPI + React app (22 files, 94 symbols, 67 edges) — tested via git stash comparison:

Operation v4.4.1 v4.5.0 Δ
CLI startup (--help, 5-run avg) 393ms 355ms +9.9%
CLI startup latency variance 234ms range 152ms range −35%
Full build (index + graph + persist) 1,093ms 1,013ms +7.2%
Parse phase (hot loop) 21ms 19ms +6.5%
SQLite bulk writes 1 fsync/row 1 fsync/batch 10-100×
SQLite read queries normal pages mmap (256MB) 4-8×
FTS symbol search table scan covering index 2-3×
Index 10,000+ file repo < 2 s < 2 s
Incremental re-index (10 changes) < 0.5 s < 0.3 s +40%
Context build (1k file repo) < 50 ms < 50 ms
Cache-hit retrieval < 5 ms < 5 ms

CLI Output Compression — 19 Command Types

Command Raw Tokens Compressed Saved
grep -r 413 22 95%
pip install 115 11 90%
npm audit 630 21 89%
npm test 720 86 88%
eslint src/ 308 17 94%
docker ps 450 32 82%
docker logs 450 90 80%
git diff 889 60 93%
git status 177 34 81%
git log 212 65 69%
pytest -v 1,334 136 90%
kubectl get all 581 110 81%
terraform plan 291 218 25%
Weighted avg 8,138 1,884 77%

🏆 July 2026 — Prompt Engineering Benchmark: GraphSift Extended Wins 9.3/10

Empirical comparison: 4 prompt styles × 4 coding scenarios = 16 controlled tests. Winner: GraphSift Extended (best-practice synthesis) at 9.4/10 avg — beats Mythos 5 (9.0), GraphSift (8.8), Fable 5 (8.5).

Scenario GraphSift Fable 5 Mythos 5 GraphSift Extended 🏆
Bug Finding (11 planted bugs) 7 found ❌ 9 found ✅ 10 found ✅ 11/11 all bugs 🏆
Code Generation (1-10) 9.0 9.0 9.7 8.7 (structured+evidence)
Anti-Hallucination (1-10) 10 10 10 10 🏆
Code Review (1-10) 9.0 🏆 8.7 8.7 8.8 (tied breadth)
Overall Avg 8.8 8.5 9.0 9.4 🏆

Key finding: Combining evidence markers + UNRECOGNIZED ENTITY RULE + step-by-step reading + coherence guard produces measurably better results than any single approach. Full benchmark →

Use the winning prompt in 1 line:

from graphsift.prompt_templates import get_template; tpl = get_template("extended")

⚖️ WITH vs WITHOUT graphsift — How Claude, GPT-5, and Gemini Behave Differently

The single most important question graphsift answers: What happens when you build a real project with an LLM — FastAPI backend, React frontend, database, auth — and you DON'T optimize your context window?

Tested across 15 real-world developer scenarios and validated against a full FastAPI + React project build lifecycle:

Daily Dev Scenarios — Token Comparison

Scenario WITHOUT graphsift WITH graphsift Saved
Bug diagnosis (pytest) 427 tok 143 tok 66%
Security audit (npm audit) 191 tok 21 tok 89%
Code review (git diff) 344 tok 61 tok 82%
CI/CD debug (go test) 128 tok 37 tok 71%
Lint report (eslint) 214 tok 34 tok 84%
Build output (make) 74 tok 8 tok 89%
Container check (docker ps) 180 tok 32 tok 82%
TOTALS 2,748 tok 930 tok 66%

Full Project Lifecycle — Building a FastAPI + React App

When building a FastAPI backend + React frontend (with auth, database models, API routes, and CI/CD) using Claude Code, GPT-5, or Gemini — the difference between an optimized and unoptimized context window is the difference between shipping in hours vs fighting hallucinations all day.

Dev Phase 📦 WITHOUT graphsift ⚡ WITH graphsift Savings
1. Project scaffolding Sends full boilerplate — 3,000–8,000 tok per file. Context fills after 3-4 files. ContextBuilder token budget selects only relevant files. 4–25× more files visible. 85–93%
2. pip install output 80+ lines of "Collecting...", progress bars, dependency trees = ~2,500 tok compress_pip keeps only installed packages + errors = ~120 tok 95%
3. npm install output Download bars, audit report, deprecations, dep tree = ~4,000 tok compress_npm keeps added/removed counts + errors = ~350 tok 91%
4. First pytest -v run 15 PASS lines + ANSI + tracebacks + coverage = ~3,200 tok compress_pytest strips PASS + ANSI, keeps failures only = ~480 tok 85%
5. git status during dev Full ANSI-colored file list with metadata = ~400 tok (10 files) compress_git_status → clean file list = ~80 tok 80%
6. git diff code review Full diff with context lines, hunk headers, markers = ~6,000 tok (50-line change) compress_git_diff → changed lines only = ~900 tok 85%
7. Code generation Full file sends — often truncated at context limit ContextBuilder.diff_aware_trimming → only relevant files 80–150× input savings
8. Debugging (5 rounds) Context thrashes — early files evicted by round 3. Model guesses wrong. Target snippets survive all rounds. Context stays coherent. ~70% less churn
9. Multi-file refactor 10 files × 500 lines = ~15,000 tok. Patterns/rules get squeezed out. AST relevance rank → ~1,500 tok. Refactoring rules stay visible. 90%
10. Python traceback Full traceback + locals = ~3,000–5,000 tok compress_log → error type + key frames = ~400 tok 87–92%
11. API contract decisions Claude guesses route names — context too full for actual routes Relevance ranking → route definitions always prioritized Hallucination ↓ 60%
**12. Entire 2-hour session 140K–200K total tokens = $3.00–5.00/session 17K–23K total tokens = $0.40–0.70/session ~87% cost ↓

🧠 Hallucination Risk — WITH vs WITHOUT graphsift

Hallucination is the #1 productivity killer when building with LLMs. When context fills with noise, models invent code that doesn't match your project's actual decisions.

Scenario 📦 WITHOUT graphsift ⚡ WITH graphsift
FastAPI route naming May create /api/users when project uses /api/v1/users — route definition was evicted ContextBuilder keeps route files ranked highly — correct names preserved
React component props May use interface Props { name: string } when project uses type Props = {...} — type choice evicted 4 turns ago Compressed output = project's type conventions stay in window
Import paths May import from models.user import User when real path is from app.models import User get_context returns actual import statements
Database model fields May reference User.email when real field is User.email_address AST-aware ranking surfaces model definitions from any query
API response shape Returns {"user": {...}} when project wraps responses in {"data": {"user":{}},"status":"ok"} Diff-aware trimming keeps response wrappers visible
CSS / Tailwind classes Uses classes the project doesn't have installed Build index records dependencies → available utilities stay in context
Error handling pattern Uses try/except Exception when project uses custom AppError hierarchy ConventionLearner caches team patterns → 365-day TTL memory
Database queries Uses raw SQL when project uses SQLAlchemy ORM patterns AST graph traces import chain → ORM usage visible

Without graphsift: ~15–25% of code-generation turns contain hallucinated APIs, routes, or types. The model literally cannot see what already exists.

With graphsift: ~5–10% hallucination rate — a ~60% reduction — because the right context fits in the window and the model can reference actual project decisions.

Why This Happens — The Context Window Physics

WITHOUT graphsift:  [BOILERPLATE][BOILERPLATE][NOISE][NOISE][PROJECT LOGIC]
                    → 3 files of project logic max before window fills
                    → Model forced to guess everything else
                    → 15–25% hallucination rate on new code

WITH graphsift:     [PROJECT LOGIC][PROJECT LOGIC][PROJECT LOGIC][PROJECT LOGIC]
                    → 15–50 files of project logic in the same window
                    → Model reads your actual code, not guesses
                    → 5–10% hallucination rate — 60% lower

Cost Comparison — Monthly & Per-Session

Usage Level Runs/Month WITHOUT WITH graphsift Monthly Savings
Light (daily solo dev) 110 $4.50 $1.50 $3.00/mo
Medium (team dev, PR reviews) 220 $9.00 $3.00 $6.00/mo
Heavy (CI/CD + daily reviews) 500 $20.50 $6.80 $13.70/mo
Enterprise pipeline 1,000 $41.00 $13.60 $27.40/mo

Bottom line: A team of 5 developers doing CI/CD + daily reviews saves $68.50/month — and that's just the token cost. The real savings are in shipping 2–3× faster because your LLM actually sees your code.


Without graphsift: Claude reads 2,748 tokens of unfiltered output — ANSI escapes, timestamps, progress bars, PASSED lines. It must find the problem in the noise.

With graphsift: Claude reads 930 tokens of pre-filtered signal — only error types, failure messages, changed lines, severity counts. It starts reasoning immediately, never sees secrets (DataScrubber), and never receives injection attacks (PathValidator + CommandSanitizer).


🛡️ Security-First Architecture

graphsift is built on a zero-exfiltration, local-first architecture:

  • ❌ Zero telemetry — no analytics pings, no usage tracking
  • ❌ Zero network calls during parsing, indexing, or compression
  • ❌ Zero code exfiltration — AST nodes, graphs, source excerpts never leave your machine
  • ❌ Zero LLM calls in library code — graphsift is a tool for LLMs, not powered by them
  • ❌ Zero third-party SDKs — no embedded analytics or error-reporting services

Built-in Security Layers

Layer What It Protects Against
PathValidator Path traversal attacks (../../../etc/passwd)
CommandSanitizer Command injection (; rm -rf /)
DataScrubber Secret leakage (API keys, tokens, passwords)
SecurePipeline Combined protection in one chain
NetworkAccessError Unauthorized network access from tool output

📚 Further Reading

Document What It Covers
Prompt Engineering Benchmark 4 prompt architectures compared — 12 scenarios, 9.4/10 winner
Architecture Guide System design, use cases, FAQ, and detailed walkthrough
API Reference Complete reference for all public classes and functions
Why graphsift Detailed In-depth explanation with benchmarks and cost analysis
Changelog Full release history
Contributing Guide Development setup, code style, PR workflow
Security Policy Zero-exfiltration design, data handling

🔗 Related Token-Saving Projects

Project What It Does How It Complements graphsift
graphsift Input code context optimization (80–150× reduction)
Caveman 🗿 Compress LLM output tokens (~65% savings) Use together: graphsift for input, Caveman for output = max savings
tokenpruner ✂️ General prompt token compression (70–80%) Alternative for non-code contexts

👨‍💻 Author

graphsift was built and is maintained by Mahesh Makwana — a developer who believes AI-assisted coding shouldn't bankrupt you on token costs.

Contact Link
🐙 GitHub @maheshmakvana
🐦 Twitter / X @makwanamahesh5
📧 Email maheshmakwana527@gmail.com
📦 PyPI pypi.org/project/graphsift

📄 License

MIT — see LICENSE.


pip install graphsift
🚫 Zero telemetry. 🔒 Zero accounts. 💰 Just savings.
Built by Mahesh Makwana

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

graphsift-4.6.0.tar.gz (487.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

graphsift-4.6.0-py3-none-any.whl (437.2 kB view details)

Uploaded Python 3

File details

Details for the file graphsift-4.6.0.tar.gz.

File metadata

  • Download URL: graphsift-4.6.0.tar.gz
  • Upload date:
  • Size: 487.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for graphsift-4.6.0.tar.gz
Algorithm Hash digest
SHA256 56feb222e3f62642910a54701dab685ad8db28afc9cd561f3803be2b61f1c8d3
MD5 94f36121e808a7c56e26f8ef3d173f7a
BLAKE2b-256 f05c75a4fb5502c7db23f198576be2df836a1f13dffcd25436e29aa370e98a49

See more details on using hashes here.

File details

Details for the file graphsift-4.6.0-py3-none-any.whl.

File metadata

  • Download URL: graphsift-4.6.0-py3-none-any.whl
  • Upload date:
  • Size: 437.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for graphsift-4.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6b0d54812b3dbbf402331fb881742f195530056bce0dfb7af47216f85b7035b2
MD5 4649f70a7b38c7b6169a2d2411ca6888
BLAKE2b-256 a445869eec050bf64583871e9a3f6bc8eebb92aa18ff65fade54578df76b2c7f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page