Skip to main content

ContextCraft

AST-Aware, Dependency-Graph-Ranked, Token-Budgeted Semantic Codebase Compressor for LLMs

CI Python 3.10+ PyPI Typed: mypy strict Code style: ruff License: MIT


Why ContextCraft?

Existing repository packers (repomix, gitingest, code2prompt) blindly concatenate every file verbatim. On real-world repositories, this blows past LLM context budgets, buries critical architecture inside thousands of lines of boilerplate (Lost-in-the-Middle degradation), and wastes API tokens.

ContextCraft is the AST-aware "MarkItDown for Codebases." Instead of naive concatenation or arbitrary character truncation, ContextCraft parses your codebase with Tree-sitter, ranks every file's architectural importance via Personalized PageRank on the cross-file dependency graph, and solves a Multiple-Choice Knapsack Problem (MCKP) across 4 AST fidelity tiers (FULL, SKEL, SIGS, TREE) to pack maximum architectural signal into your exact token budget $B$.


10-Second Quickstart

1. Installation (PyPI & uvx)

# Run instantly without installing via uvx:
uvx contextcraft-ai . --budget 16k --focus "AST elision and knapsack optimizer" -o context.md

# Or install globally / in your virtual environment from PyPI:
pip install contextcraft-ai
contextcraft . --budget 32k --format xml --copy

2. Command-Line Interface (CLI)

# Compress a repository into a strict 4,000-token budget biased toward your task
uv run contextcraft . --budget 4k --focus "AST elision and knapsack optimizer" -o context.md

# Emit Anthropic Claude-optimized <repository_context> XML and copy to clipboard
uv run contextcraft . --budget 32k --format xml --copy

3. Python SDK (Modeled after microsoft/markitdown)

from contextcraft import ContextCraft

cc = ContextCraft(budget_tokens="16k")
result = cc.compile("./my-repo", focus_query="authentication middleware")

print(result.markdown)
print(
    f"Compression: {result.stats.compression_ratio}x ({result.stats.compressed_tokens} / {result.stats.budget_tokens} tokens)"
)

4. Interactive Web Playground & "URL Swap Trick"

uv run uvicorn web.app:app --reload --port 8000

Open http://127.0.0.1:8000 to interactively tune token budgets, inspect PageRank scores, or use the URL Swap Trick: visit http://127.0.0.1:8000/<owner>/<repo>?budget=32k&focus=auth to automatically clone and compress any public GitHub repository!


Architecture Pipeline

┌────────────────────────────────────────────────────────────────────────────┐
│ 1. RepositoryScanner & SecretSanitizer                                     │
│    • Hierarchical .gitignore / .contextignore & binary filtering           │
│    • Pre-AST API key, PEM & high-entropy secret scrubbing (line-preserving)│
└────────────────────────────────────┬───────────────────────────────────────┘
                                     ▼
┌────────────────────────────────────────────────────────────────────────────┐
│ 2. UniversalASTParser (Tree-sitter + Deterministic Fallback)               │
│    • Extracts classes, functions, methods, signatures, and docstrings      │
│    • Computes exact post-docstring `body_range` spans for surgical elision │
└────────────────────────────────────┬───────────────────────────────────────┘
                                     ▼
┌────────────────────────────────────────────────────────────────────────────┐
│ 3. DependencyGraphScorer (Cross-File MultiDiGraph + Personalized PageRank) │
│    • Weighted edges from module imports & IDF-scaled symbol references     │
│    • BM25 query personalization (--focus) + 1-hop neighbor diffusion       │
└────────────────────────────────────┬───────────────────────────────────────┘
                                     ▼
┌────────────────────────────────────────────────────────────────────────────┐
│ 4. MultiResolutionElider (4 Discrete AST Fidelity Tiers per File)          │
│    • Tier 3 [FULL] (u=1.00): Complete sanitized source code                │
│    • Tier 2 [SKEL] (u=0.82): Signatures + types + docstrings (body elided) │
│    • Tier 1 [SIGS] (u=0.45): Imports & public declarations only            │
│    • Tier 0 [TREE] (u=0.00): Annotated directory tree entry only           │
└────────────────────────────────────┬───────────────────────────────────────┘
                                     ▼
┌────────────────────────────────────────────────────────────────────────────┐
│ 5. TokenBudgetOptimizer (Multiple-Choice Knapsack Solver) & Formatter      │
│    • Maximizes ∑ PageRank(i) · u(l_i) subject to TotalTokens ≤ Budget B    │
│    • Emits Markdown, Claude XML, or JSON with strict budget verification   │
└────────────────────────────────────────────────────────────────────────────┘

Quantitative Benchmark (ContextCraft Repository)

Evaluated using benchmarks/run_benchmark.py (--focus "AST elision knapsack optimizer pagerank"):

  • Raw Uncompressed Repository (repomix / verbatim concatenation baseline): 43,151 tokens (1.00x)
Method Target Budget ($B$) Emitted Tokens Compression Ratio Public API Signature Coverage Budget Compliant
Naive Prefix Truncation 4,000 4,000 10.79x 18.4% YES (Loses 81% of repo)
ContextCraft (Ours) 4,000 3,950 10.92x 70.9% YES (98.8% utilization)
Naive Prefix Truncation 6,000 6,000 7.19x 31.6% YES (Loses 68% of repo)
ContextCraft (Ours) 6,000 5,940 7.26x 94.3% YES (99.0% utilization)

Development & Verification

# Install all dependencies (core, web, and dev)
uv sync --locked --all-extras --dev

# Run lint, strict type checking, and test coverage
uv run ruff check src tests web benchmarks
uv run mypy src/contextcraft
uv run pytest --cov=contextcraft --cov-report=term-missing

License

Released under the MIT License.

Metadata

Release files for contextcraft-ai 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for contextcraft-ai 0.2.0
File Size Uploaded Provenance Actions
contextcraft_ai-0.2.0.tar.gz 236.4 kB With provenance Details for contextcraft_ai-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for contextcraft-ai 0.2.0
File Interpreter ABI Platform Actions
contextcraft_ai-0.2.0-py3-none-any.whl Python 3 none any Details for contextcraft_ai-0.2.0-py3-none-any.whl

Total release size: 297.0 kB

Release files / contextcraft_ai-0.2.0.tar.gz

Download URL contextcraft_ai-0.2.0.tar.gz
Size 236.4 kB
Tags Source
SHA-256 checksum
How to use checksums
4d5b83d0b76e079153f05a3d8f2c128eb06e5952af9c6b13390ec58e0d064cfe
BLAKE2b-256 checksum
How to use checksums
dfc68ed2edf76447a80d10786085806c563e8608686582732c909ea176508c28
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 22, 2026.

Transparency log

Release files / contextcraft_ai-0.2.0-py3-none-any.whl

Download URL contextcraft_ai-0.2.0-py3-none-any.whl
Size 60.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7d94fe2e1e961ec872bfb0d76b106c3ac0abfa522c1769ca6f0aa00615a19b60
BLAKE2b-256 checksum
How to use checksums
c360a6bd122b14878bf50fe4253ea7d33c2bf410a1283e03b99615fe74428247
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 22, 2026.

Transparency log

Security for contextcraft-ai 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for contextcraft_ai-0.2.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 22, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page