Skip to main content

Aggregate codebase files into a single context file for LLM consumption.

Project description

Context Engine CLI

Bridge the gap between massive local codebases and Large Language Models (LLMs). Context Engine CLI turns a repository into a clean, single-file context bundle that is easier to inspect, share, and paste into AI tools.

✨ Key Features

  • Fast directory traversal with os.scandir, avoiding slower recursive path walking.
  • Memory-safe output streaming that writes directly to disk instead of building one huge string in RAM.
  • Concurrent file reading with ThreadPoolExecutor for faster aggregation on large repositories.
  • Safe synchronized writes with threading.Lock so multithreaded output stays clean and readable.
  • .gitignore and .contextignore support for project-specific filtering.
  • Precompiled ignore rules using fnmatch.translate and re.Pattern for faster repeated matching.
  • Default binary and build-artifact exclusions for files such as .png, .exe, .pdf, .pyc, node_modules, __pycache__, and .git.
  • Token estimation using the practical rule of thumb 1 token ≈ 4 characters.
  • Token budget control with --max-tokens to stop before the output exceeds an LLM context window.
  • Clean terminal UX with scan feedback, warnings for skipped files, and a final processing summary.

📦 Installation

Clone the repository, then install it locally in editable mode:

git clone https://github.com/zain333ux/context-engine-cli.git
cd context-engine-cli
python -m pip install -e .

After installation, the context-engine command is available from your terminal:

context-engine --help

On Windows, if the command is not recognized after installation, make sure your Python Scripts directory is on PATH.

🚀 Usage

Basic scan

Generate a codebase_context.txt file from a target project:

context-engine ./my-project

Choose an output file

context-engine ./my-project --output project_context.txt

Short form:

context-engine ./my-project -o project_context.txt

Ignore additional file extensions

Pass a comma-separated list of extensions to exclude:

context-engine ./my-project --ignore .csv,.sqlite,.bak

Extensions can be provided with or without the leading dot:

context-engine ./my-project -i csv,sqlite,bak

Limit estimated LLM tokens

Stop appending files once the estimated output would exceed a token budget:

context-engine ./my-project --max-tokens 100000

Combine options:

context-engine ./my-project \
  --output llm_context.txt \
  --ignore .png,.jpg,.sqlite \
  --max-tokens 100000

🧠 Why This Exists

LLMs work best when they receive structured, relevant context. Real repositories, however, are messy: they include dependency folders, caches, binaries, build output, generated files, and enough source code to overwhelm both memory and context windows.

Context Engine CLI creates a clean, LLM-friendly snapshot of a codebase by:

  • rendering the directory tree,
  • appending readable source files with clear file headers,
  • skipping binary or unreadable files gracefully,
  • respecting ignore rules,
  • estimating final token usage,
  • and streaming output safely to disk.

The result is a single text file that an LLM can parse without requiring you to manually copy files, prune noise, or guess whether the final context will fit.

🧾 Output Format

Each generated file starts with a directory tree, followed by clearly separated file sections:

CODEBASE CONTEXT
================================================================================
Directory Tree
================================================================================
my-project
|-- context_engine
|   `-- main.py
`-- pyproject.toml

================================================================================
File: context_engine/main.py
================================================================================
[FILE CONTENT HERE]

🙈 Ignore Rules

Context Engine CLI reads ignore patterns once at startup from:

  • built-in defaults,
  • .gitignore,
  • .contextignore,
  • and the optional --ignore argument.

Use .contextignore for LLM-specific exclusions without changing your Git behavior:

docs/archive/
*.snapshot
large-fixtures/

Negated ignore rules such as !important.py are currently skipped.

⚙️ CLI Reference

context-engine TARGET_DIR [-o OUTPUT] [-i EXTENSIONS] [--max-tokens N]

Arguments:

  • TARGET_DIR: Directory to scan.
  • -o, --output: Output file name. Defaults to codebase_context.txt.
  • -i, --ignore: Additional comma-separated file extensions to ignore.
  • --max-tokens: Maximum estimated tokens to write before stopping.

🛠 Development

Install locally in editable mode:

python -m pip install -e .

Run the CLI from source:

python -m context_engine.main ./my-project

Run a syntax check:

python -m compileall context_engine

Generate the local stress-test fixture:

python test_generator.py
context-engine test_env -o test_env_context.txt

📄 Package Metadata

  • Package name: context-engine-zain333
  • Python package: context_engine
  • Entry point: context-engine = context_engine.main:main
  • Python version: >=3.10
  • Runtime dependencies: none beyond the Python standard library

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

context_engine_zain333-0.1.0.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

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

context_engine_zain333-0.1.0-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file context_engine_zain333-0.1.0.tar.gz.

File metadata

  • Download URL: context_engine_zain333-0.1.0.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.14

File hashes

Hashes for context_engine_zain333-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9d027dbd5871051331428f42cf065bfc5e25304d9eb1e88c3ca2833d18829a52
MD5 35587f52aa8fba13fa81e22413bcf7d6
BLAKE2b-256 07a7d22ef9c20e76b92100681b0dceb5dafdb9b8bbfc768b4a3c463b6e8c6c0b

See more details on using hashes here.

File details

Details for the file context_engine_zain333-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for context_engine_zain333-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4419c78b473355a26db48cc8c4953dc7d505f89ffd54d635cda273f6f02975c8
MD5 d94c5e6866d82f26fb387d2a4ec53f98
BLAKE2b-256 1a42a5bdb560960839aba9be6692f3ea2b88c3339260d49575c63d50ac9f841a

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