Skip to main content

diffctx — smart diff context for LLM code review

CI PyPI crates.io npm License

diffctx selects the minimum code an LLM needs to review a git diff. Instead of pasting whole files, it walks the dependency graph outward from the changed lines and stops once more context stops paying for itself.

Coming from treemapper? That name is deprecated. Every command, flag, and API call works unchanged: treemapperdiffctx, treemapper-mcpdiffctx-mcp.

Why not just use tree or repomix?

tree repomix Claude Code Review diffctx
Primary use case directory listing full repo export automated PR review diff context for code review
Smart diff context
Works with any LLM Claude only
Free / local / offline $15–25/review
GitHub required
Multiple output formats limited YAML/JSON/MD/txt
Python API
MCP server

Fuller positioning — measured results, and when a whole-repo packer or a persistent code-graph server fits better: COMPARISON.md.

Install

uvx diffctx . --diff HEAD~1             # zero-install, run once via uv
pipx install diffctx                    # recommended: isolated CLI, no venv needed
pip install diffctx                     # or: into an active environment
pipx install 'diffctx[mcp]'             # + MCP server for AI assistants

Without Python:

cargo install diffctx                   # native CLI from crates.io
npx diffctx . --diff HEAD~1             # npm wrapper over the native binary
docker run --rm -v "$PWD:/repo" ghcr.io/nikolay-e/diffctx . --diff HEAD~1

On Windows, via Scoop (this repository is the bucket):

scoop bucket add diffctx https://github.com/nikolay-e/diffctx
scoop install diffctx/diffctx

The image runs as a non-root user (uid 10001) and writes to stdout — the native binary has no -o flag, so redirect to capture: ... --diff HEAD~1 > context.yaml.

Prebuilt binaries for linux (x86_64/aarch64), macOS (arm64) and Windows (x64) are attached to every release. The native binary covers diff mode with YAML/JSON output; tree mode, Markdown output, the graph subcommand and the MCP server live in the Python package. cargo add diffctx embeds the pipeline in a Rust project — the library is imported as _diffctx (use _diffctx::pipeline::build_diff_context), since the crate doubles as the Python extension module (docs.rs).

Quick start

diffctx . --diff HEAD~1       # smart context for last commit → paste into Claude/ChatGPT
diffctx . -f md -c            # full codebase export → clipboard in Markdown

diffctx demo

diffctx . --diff HEAD~1 selects only the fragments an LLM needs to review the last commit, instead of dumping every changed file in full.

Diff context mode

Finds the minimal set of fragments needed to understand a change — imports, callers, type definitions, config dependencies — across 50+ file types. It builds a code graph (imports, co-changes, type refs), propagates relevance outward from the changed lines, and stops when relevance drops below --tau or the --budget token cap is hit.

Flag Default Description
--scoring ego ego = bounded expansion around changed nodes (fast, predictable radius); ppr = Personalized PageRank (global, smoother decay, slower); bm25 = lexical retrieval against the diff hunks (baseline for sparse graphs)
--budget auto Hard cap in o200k_base tokens (see Token counting): N enforces a fixed cap, -1 disables it, 0 is a strict-zero floor (empty selection; use --full for changed files only)
--alpha 0.60 PPR damping; higher = context clusters tighter around changes (--scoring ppr only)
--tau 0.12 Relevance threshold for full fragment content; lower-scoring fragments are stubbed or dropped (lower = more context)
--full false Only the changed files, every fragment, no related-code context
--timeout 300 Wall-clock deadline in seconds; on expiry diffctx exits 124 instead of hanging
--with-raw-diff false Also embed git's raw unified diff ahead of the selected fragments — additive (selection unchanged), not charged to --budget, lock/ignored/secret-like sections omitted. Python CLI only

Calibration of --alpha, --tau, and the edge-weight priors: docs/engineering/parameter-strategy.md. Theory: diffctx: Budgeted Typed-Graph Retrieval for Diff-Aware Code Context Selection (Zenodo, 2026).

graph subcommand

Explore the underlying dependency graph directly, without a diff:

diffctx graph .                                  # Mermaid graph of directory deps (default)
diffctx graph . --summary                        # cycles, hotspots, coupling metrics
diffctx graph . --level fragment -f json         # fragment-level graph as JSON
diffctx graph . --level file -f graphml -o g.xml # file-level graph as GraphML

Usage

# full codebase export:
diffctx .                                 # Markdown to stdout + token count
diffctx . -f md -c                        # Markdown → clipboard
diffctx . -f json -o tree.json            # JSON → file
diffctx . --no-content                    # structure only, no file contents
diffctx . --max-depth 3                   # limit depth
diffctx . -i custom.ignore                # custom ignore patterns

# diff context mode (requires git repo):
diffctx . --diff                          # uncommitted changes (working tree vs HEAD)
diffctx . --diff HEAD~1                   # context for last commit
diffctx . --diff main..feature            # context for feature branch
diffctx . --diff HEAD~1 --budget 30000    # limit to ~30k tokens
diffctx . --diff HEAD~1 -c                # diff context to clipboard
diffctx . --diff HEAD~1 --with-raw-diff   # raw patch + selected context

Every run reports token count and size on stderr — 12,847 tokens (o200k_base), 52.3 KB (tiktoken, the GPT-4o tokenizer; ~-prefixed above 1 MB). -c/--copy copies output via pbcopy (macOS), clip (Windows), or wl-copy/xclip/xsel (Linux). Unreadable files become placeholders like <binary file: N bytes>, <file too large: N bytes>, or <unreadable content: not utf-8>.

Counts come from tiktoken's o200k_base encoder and are exact only for the GPT-4o family; Claude, Gemini and others tokenize differently, so treat --budget as an upper bound in o200k tokens and leave headroom. Details: docs/product/token-budget.md.

Python API

from pathlib import Path
from diffctx import build_diff_context, map_directory, to_json, to_markdown, to_text, to_yaml

ctx = build_diff_context(
    Path("."),
    "HEAD~1..HEAD",
    budget_tokens=None,       # None = auto; 0 = strict-zero floor (empty); -1 = uncapped; N = hard cap
    alpha=0.6,
    tau=0.12,
    full=False,
    scoring_mode="ego",
    timeout=300,
    with_raw_diff=False,      # True also embeds the raw unified diff (not charged to budget)
)
print(to_markdown(ctx))

tree = map_directory(
    ".",
    max_depth=None,
    no_content=False,
    max_file_bytes=None,
    ignore_file=None,
    no_default_ignores=False,
    whitelist_file=None,
)
print(to_yaml(tree))

MCP server

diffctx includes an MCP server that lets AI assistants (Claude Code, Cursor, Windsurf, etc.) call diff context analysis automatically during code review. Install with pip install 'diffctx[mcp]', then register it — for Claude Code:

claude mcp add diffctx -- diffctx-mcp

The server exposes three tools — get_diff_context, get_tree_map, and get_file_context — that assistants call when reviewing PRs, explaining changes, or investigating broken tests. Tool reference and configs for Cursor, Continue, Windsurf, and Zed: src/diffctx/mcp/README.md.

Ignore patterns

Respects .gitignore and .diffctx/ignore automatically — hierarchically at every directory level, with full gitignore semantics (negation !important.log, anchored /root_only.txt). .diffctx/whitelist acts as an include-only filter, and the output file is always auto-ignored. --no-default-ignores disables the built-in patterns; --no-ignores disables all ignore rules (tree mode only).

An excluded path never appears in the output in any role: in diff mode it is dropped both from changed_files and from the candidate universe, so it cannot come back as a related-context fragment either (including under --full). The same guarantee covers secret-like paths (id_rsa, *.pem, *.key, ...), which are filtered even without an ignore entry.

Token cache

Diff mode caches per-blob tokenization under ~/Library/Caches/diffctx/token-cache (macOS), $XDG_CACHE_HOME/diffctx/token-cache (Linux) or %LOCALAPPDATA%\diffctx\token-cache (Windows). It is a pure speedup: deleting it only costs one cold run.

Variable Effect
DIFFCTX_TOKEN_CACHE_DIR Relocate the cache
DIFFCTX_TOKEN_CACHE_MAX_BYTES Size cap, default 536870912 (512 MB); 0 disables eviction

Eviction is amortized: each run trims one of the cache's 256 shards back under its share of the cap, oldest entries first.

Exit codes

Code Meaning
0 Success — output contains content
1 Runtime error (bad path, permission denied, etc.)
2 Usage error (invalid flags/arguments)
3 Environment error (--diff outside a git repo, git not installed, no commits yet)
4 --diff produced no semantic context (clean tree, binary-only, everything filtered); output is still emitted. Deletion/rename/lockfile-only diffs list deleted_files/renamed_files/lockfile_changes and exit 0
124 --diff exceeded the --timeout wall-clock deadline
130 Interrupted (Ctrl-C)
141 Broken pipe (e.g. piping into head)

Repository layout

Rust product code lives in crates/diffctx-native/; the Python CLI and MCP package live in src/diffctx/. Evaluation code, immutable datasets, and paper artifacts are intentionally separated under eval/, datasets/, and paper/. See repository ownership boundaries for the lifecycle and entry point of each area.

License

Apache 2.0


  • Documentation site — the pipeline end to end: diff → fragments → graph → relevance → selection
  • GitHub Action — diff context as a CI step for LLM review
  • Token counting — which encoder, and what --budget means for non-GPT models
  • Comparison — measured results, and when a whole-repo packer or a persistent code-graph server fits better
  • Changelog
  • Security policy — threat model and vulnerability reporting
  • Parameter strategy — how --alpha, --tau, and edge weights are calibrated

Download files

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

Source Distribution

diffctx-1.12.3.tar.gz (297.1 kB view details)

Uploaded Source

Built Distributions

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

diffctx-1.12.3-cp310-abi3-win_amd64.whl (8.7 MB view details)

Uploaded CPython 3.10+Windows x86-64

diffctx-1.12.3-cp310-abi3-manylinux_2_28_x86_64.whl (8.9 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.28+ x86-64

diffctx-1.12.3-cp310-abi3-manylinux_2_28_aarch64.whl (8.7 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.28+ ARM64

diffctx-1.12.3-cp310-abi3-macosx_11_0_arm64.whl (8.9 MB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

File details

Details for the file diffctx-1.12.3.tar.gz.

File metadata

  • Download URL: diffctx-1.12.3.tar.gz
  • Upload date:
  • Size: 297.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for diffctx-1.12.3.tar.gz
Algorithm Hash digest
SHA256 97ac358a474f447ae2a04075426ac48d9b4b2a6637b60d79f017686de5498018
MD5 cf634eb7a4f40feca32811a77cddf017
BLAKE2b-256 8ac55d83cc0baa079a13c3bbb978cd6d93c31429004e5d45b8132fb429c83dde

See more details on using hashes here.

Provenance

The following attestation bundles were made for diffctx-1.12.3.tar.gz:

Publisher: cd.yml on nikolay-e/diffctx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file diffctx-1.12.3-cp310-abi3-win_amd64.whl.

File metadata

  • Download URL: diffctx-1.12.3-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 8.7 MB
  • Tags: CPython 3.10+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for diffctx-1.12.3-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 55fb466dd39a21540956ab7f88d8826683fb549cb9d1530cb053a7e2e45887e2
MD5 4ee2b9e8e1ebc55f807feec5a874f514
BLAKE2b-256 dd8ceaa792897ec8e5f5fa27e228a066653c7703e3251e0c5d31c201f4e888ca

See more details on using hashes here.

Provenance

The following attestation bundles were made for diffctx-1.12.3-cp310-abi3-win_amd64.whl:

Publisher: cd.yml on nikolay-e/diffctx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file diffctx-1.12.3-cp310-abi3-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for diffctx-1.12.3-cp310-abi3-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 0314e2a6501cd4b66ef55c59f942f1dd4f33e8da56d63fd85da496180b76e9ea
MD5 577115814a61bad8821b509000f49d72
BLAKE2b-256 611d2d0ec6df7d23c5615ddd99b1761cd2529fd2afee19e76826fd279d064d5d

See more details on using hashes here.

Provenance

The following attestation bundles were made for diffctx-1.12.3-cp310-abi3-manylinux_2_28_x86_64.whl:

Publisher: cd.yml on nikolay-e/diffctx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file diffctx-1.12.3-cp310-abi3-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for diffctx-1.12.3-cp310-abi3-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 1e10f49cf970606388e641e77e9dc73fe713f8d45c6ca000b655564ddb83b2f2
MD5 f8c865e4e761baa0e488d73aa3b693d7
BLAKE2b-256 aabbe5a7c8245acdd286b68107171d4ca2d7b4eeb8134600aeb3aee5da762651

See more details on using hashes here.

Provenance

The following attestation bundles were made for diffctx-1.12.3-cp310-abi3-manylinux_2_28_aarch64.whl:

Publisher: cd.yml on nikolay-e/diffctx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file diffctx-1.12.3-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for diffctx-1.12.3-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6fde1cdc21f5fe82a1e813c0e87674bb5dd5409785869d9b4ae0cfacf745ca26
MD5 b13cdfdcec2800a2502a79d1514adaf3
BLAKE2b-256 b7500b279533543395190056865cba80b3718fd0a981f1015f248ca699fcc8e5

See more details on using hashes here.

Provenance

The following attestation bundles were made for diffctx-1.12.3-cp310-abi3-macosx_11_0_arm64.whl:

Publisher: cd.yml on nikolay-e/diffctx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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