Skip to main content

AI Context Core

CI PyPI Version GitHub Release Python Versions Downloads License Linting: Ruff

Compile token-efficient, verifiable context for AI-assisted coding.

ai-context-core (CLI ai-ctx) turns a Python project into context artifacts an AI agent can consume: a structured AI_CONTEXT.md, a machine-readable project_context.json, a token context_manifest.json and a symbol index. Everything is versioned, provenance-tagged and optionally token-budgeted.

Context-only contract (v5.0.0). ai-context-core is a context compiler (source → transform → render → verify): it consumes analysis instead of owning it. QGIS compliance, i18n, security scanning and quality gates live in qgis-plugin-analyzer; scaffolding in agentic-forge. Commands such as audit, qgis, security, patterns, inspect, full-scan, fix, scaffold, doctor, interactive and serve were removed (hidden redirect stubs remain for one release). See ADR-0008, the v5.0.0 plan and the CHANGELOG.

Features

Context compilation

  • Pluggable sources: --source auto|external|builtin. external consumes qgis-plugin-analyzer output (analysis_results/project_context.json); builtin runs the internal AST engine. auto prefers external when present.
  • Hybrid by design: structure tree, git evolution and manual notes are always computed by ai-context-core; the analysis fields come from the selected source.
  • Provenance: every artifact carries a _meta block (source, schema, tool_version, git_sha, content_hash).

Token budget & manifest

  • ai-ctx context --max-tokens N caps AI_CONTEXT.md with deterministic truncation; per-section caps via [context.budget].
  • context_manifest.json records exact per-section token deltas, the budget and the total (total == header + sum(sections)).

Verification & index

  • ai-ctx verify exits 1 when the tracked sources changed since the last render (content hash); ai-ctx context --check gates CI.
  • ai-ctx symbols [--grep] writes a versioned symbols.json (definitions and references with file:line).
  • ai-ctx health reports freshness, token usage, symbol coverage and provenance — no quality gate; it fails only on staleness.

Context analysis (builtin source)

  • Structure: project tree, slide-of-code and file statistics.
  • Entry points: __main__-guard detection.
  • Dependencies: import graph, circular dependencies, unused imports, coupling (CBO), graph density/DAG, Mermaid diagram.
  • Git evolution: hotspots and code churn.
  • Metrics: cyclomatic complexity, Maintenance Index, type-hint coverage.

Performance & robustness

  • Incremental cache: hybrid mtime + SHA-256 file cache (--no-cache to bypass).
  • Smart parallelism: sequential below a threshold, batched ProcessPoolExecutor above it.
  • Fast ignore: compiled-regex file filtering via .analyzerignore.
  • Staleness-safe hashing: verification bypasses the in-process read cache.

Installation

# As a global tool
uv tool install ai-context-core

# Or inside a virtual environment
uv venv && source .venv/bin/activate
uv pip install ai-context-core

Using pip

pip install ai-context-core

It is always recommended to use a virtual environment.

Quick start

# 1. Initialize the .ai-context structure (optional)
ai-ctx init

# 2. Compile context artifacts
ai-ctx context                       # AI_CONTEXT.md + project_context.json + context_manifest.json
ai-ctx context --source external     # consume qgis-plugin-analyzer output
ai-ctx context --max-tokens 8000     # enforce a token budget
ai-ctx context --check               # generate and assert freshness (CI)

# 3. Verify and inspect
ai-ctx verify                        # exit 1 if sources changed since render
ai-ctx symbols --grep my_function    # file:line index lookup
ai-ctx health                        # freshness + tokens + symbols + provenance

Generated artifacts

Artifact Description
AI_CONTEXT.md Structured, token-budgeted context for LLMs.
project_context.json Machine-readable analysis payload + _meta provenance.
context_manifest.json Per-section token counts, budget and total.
symbols.json Versioned symbol index (definitions + references).
PROJECT_SUMMARY.md Human-readable summary (emitted by analyze).

Configuration

Full reference: docs/CONFIGURATION.md. Defaults live in src/ai_context_core/config/defaults.toml.

context_docs = []                      # extra markdown to embed as architecture notes

[context]
sections = ["structure", "metrics", "dependencies", "git"]

[context.budget]
max_tokens = 8000                      # override per run with --max-tokens N
# [context.budget.sections]
# structure = 1500

[sources]
source = "auto"                        # auto | external | builtin
# external_path = "analysis_results/project_context.json"

Analysis scope (.analyzerignore)

ai-ctx reads .analyzerignore at the project root to exclude paths from the analysis scope (modules, dependency graph, code metrics).

Commands reference

Compile & verify

Command Description
ai-ctx context Compile AI_CONTEXT.md, project_context.json, context_manifest.json (primary path). Options: --source, --max-tokens, --check, --include-md, --no-cache, --workers.
ai-ctx analyze Run the pipeline and emit AI_CONTEXT.md, PROJECT_SUMMARY.md, project_context.json. Options: --source, --format markdown|json, --include-md, --no-cache, --workers.
ai-ctx verify Exit 1 when artifacts are stale relative to the tracked sources.
ai-ctx symbols Build symbols.json; --grep <text> prints matching file:line entries.
ai-ctx health Context freshness, token usage, symbol coverage, provenance.

Inspect

Command Description
ai-ctx stats Quick project statistics (SLOC, modules, complexity, maintenance index).
ai-ctx deps Dependency analysis: --unused, --cycles, --metrics (CBO, density, DAG).
ai-ctx git Git evolution: hotspots and churn (--days N).
ai-ctx graph Export the dependency model as a Mermaid diagram (--output ARC.mmd).
ai-ctx roadmap Refactor priority (complexity × churn).
ai-ctx compare <a> <b> Diff two analysis JSON reports by metric.
ai-ctx help-me Context-health recommendations.

Project setup

Command Description
ai-ctx init Create .ai-context config and workflow templates (--profile <name>).
ai-ctx profiles List available profiles (python-generic).
ai-ctx clean Remove cache and generated artifacts (--dry-run to preview).

Metrics

Metrics are heuristics computed from static analysis and are not directly comparable to the canonical metrics of other tools (e.g. qgis-analyzer).

Metric Definition Notes
ai-ctx Quality Score → context health Aggregated 0–100 heuristic: base 100 minus penalties for average/outlier complexity, low maintainability and missing tests, plus test bonuses. Includes an explicit score breakdown. Tool-specific; not a gate. Being reframed as context health (see ADR-0008).
Avg Cyclomatic Complexity Arithmetic mean of per-module cyclomatic complexity. An average; qgis-analyzer applies a per-function gate (CC ≤ 10).
Avg Maintenance Index (MI) SEI Maintenance Index normalized to 0–100, averaged across modules. Exclusive to ai-context-core.
Max Complexity Highest single-module cyclomatic complexity. Complements the average to surface outliers.

API stability and deprecations

The canonical public API lives under ai_context_core.analyzer (engine, providers, visitors, builders), ai_context_core.sources, ai_context_core.context, ai_context_core.model and ai_context_core.cli.commands. Anything else is internal.

The v5.0.0 command removals keep hidden redirect stubs (e.g. ai-ctx audit, qgis, security, patterns) that point to their new owner for one release.

No import-level compatibility aliases remain: the ContextAggregator alias and the deprecations module were removed in v5.0.1.

Docker support

Reproducible development, testing and CI via Docker:

make docker-build    # Build all images
make docker-test     # Run the test suite with coverage
make docker-shell    # Interactive development shell
make docker-lint     # Run the linter

License

This project is licensed under the GNU General Public License v3 (GPLv3). See the LICENSE file for the full license text.

Metadata

Release files for ai-context-core 5.1.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 ai-context-core 5.1.0
File Size Uploaded
ai_context_core-5.1.0.tar.gz 115.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ai-context-core 5.1.0
File Interpreter ABI Platform
ai_context_core-5.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 237.5 kB

Release files / ai_context_core-5.1.0.tar.gz

Download URL ai_context_core-5.1.0.tar.gz
Size 115.0 kB
Tags Source
SHA-256 checksum
How to use checksums
a77ac877f85c3766171f943a2773141e9fe8c7898ba2197e86d5042a9e2000e9
BLAKE2b-256 checksum
How to use checksums
9e74b0f7e73bb2c2c5803b36449e3df6fd8575866498162383e81e246476bd51
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.10 {"installer":{"name":"uv","version":"0.12.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Manjaro Linux","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / ai_context_core-5.1.0-py3-none-any.whl

Download URL ai_context_core-5.1.0-py3-none-any.whl
Size 122.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
297ea2875dcd36a6c84f31b6b4a001682453a4056eddb93dc957c9d414ecc631
BLAKE2b-256 checksum
How to use checksums
8c4f0f4d4dc54a38172ec859a17fa51100bd55159c082d1475d2635a4e6175a6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.10 {"installer":{"name":"uv","version":"0.12.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Manjaro Linux","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release history Release notifications | RSS feed

This release

5.1.0 This release

2 release files

5.0.1

2 release files

5.0.0

2 release files

4.1.1

2 release files

4.1.0

2 release files

4.0.0

2 release files

3.5.0

2 release files

3.4.0

2 release files

3.3.0

2 release files

3.2.1

2 release files

3.2.0

2 release files

3.1.2

2 release files

3.1.1

2 release files

3.1.0

2 release files

3.0.3

2 release files

3.0.2

2 release files

3.0.1

2 release files

3.0.0

2 release files

2.5.2

2 release files

2.5.1

2 release files

2.5.0

2 release files

2.1.1

2 release files

2.1.0

2 release files

2.0.0

2 release files

1.0.1

2 release files

1.0.0

2 release files

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