Skip to main content

context-forge-py

PyPI Python License: MIT

Context engineering toolkit for RAG and agent prompts. Chunk documents, score relevance (BM25), apply diversity-aware packing (MMR), flag prompt-injection risk, and emit citation-ready context blocks that fit a token budget. Zero runtime dependencies.

Python port of @mukundakatta/context-forge.

Install

pip install context-forge-py

Quick start

from context_forge import forge

result = forge(
    chunks=[
        {"id": "doc1#0", "text": "Pluto was reclassified as a dwarf planet in 2006.", "source": "wiki/pluto"},
        {"id": "doc2#0", "text": "Mars is the fourth planet from the Sun.", "source": "wiki/mars"},
    ],
    query="What happened to Pluto?",
    budget=200,
)

result.blocks      # list of kept chunks (ranked, diversified, packed)
result.used_tokens # int -- tokens used by kept blocks
result.dropped     # list of {id, reason}
result.risks       # injection findings (e.g. ignore_instructions, exfil_curl)
result.citations   # {block_id: {source, span: [start, end]}}

You can also use the lower-level pipeline and the document-level entry:

from context_forge import (
    pack_context,    # full pipeline: chunk -> score -> diversify -> risk-scan -> pack
    chunk_document,  # split a document into token-bounded chunks
    score_chunks,    # BM25 score against a query
    diversify,       # MMR re-rank for diversity
    pack_to_budget,  # greedy budget packer
    scan_injection,  # prompt-injection risk scan
    estimate_tokens, # ceil(len/4) heuristic
)

API

forge(chunks, query, budget=1200, *, lambda_=0.7, per_chunk_min=20)

Compose ranking + diversification + injection-scanning + budget-packing on a list of pre-chunked context. Returns a ForgedContext dataclass with blocks, used_tokens, dropped, risks, and citations.

pack_context(query, documents, budget_tokens=1200, max_tokens=200, overlap_tokens=20, lambda_=0.7, per_chunk_min=20)

Full pipeline starting from raw documents ({"id": ..., "text": ..., "source": ...}). Chunks each document, then runs the same score/diversify/scan/pack steps as forge.

Risk classes

scan_injection(text) returns findings with kind, severity, snippet, and index. Detected kinds include ignore_instructions, system_prefix, you_are_now, role_tag, exfil_curl, exfil_wget, exfil_base64, zero_width_char, and suspicious_url.

License

MIT

Metadata

Release files for context-forge-py 0.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 context-forge-py 0.1.0
File Size Uploaded Provenance Actions
context_forge_py-0.1.0.tar.gz 11.9 kB Without provenance Details for context_forge_py-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for context-forge-py 0.1.0
File Interpreter ABI Platform Actions
context_forge_py-0.1.0-py3-none-any.whl Python 3 none any Details for context_forge_py-0.1.0-py3-none-any.whl

Total release size: 24.7 kB

Release files / context_forge_py-0.1.0.tar.gz

Download URL context_forge_py-0.1.0.tar.gz
Size 11.9 kB
Tags Source
SHA-256 checksum
How to use checksums
2b981ea71148b2818d198b9c16e07616e7ab22dfc5de8d6398dd6f4929d37451
BLAKE2b-256 checksum
How to use checksums
b9b6e840d367832190fe876bdec24d59d9c4338e2493390021fb23988f1fe41b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.4

Release files / context_forge_py-0.1.0-py3-none-any.whl

Download URL context_forge_py-0.1.0-py3-none-any.whl
Size 12.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dad20dbb348e7ec97a130d48730de27e49b917d4ae85cd61e1155b2729378330
BLAKE2b-256 checksum
How to use checksums
46c994e93a614b9a7374ab78a34cc40c7ca85599675231ab1cd46fe7c2440c4d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.4

Security for context-forge-py 0.1.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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

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