Skip to main content

context-window-packer-py

PyPI Python License: MIT

Pack context chunks into a budget by relevance and priority. Sort by score (descending), break ties by priority, accept each chunk if it fits, otherwise drop. Zero runtime dependencies.

Python port of @mukundakatta/context-window-packer.

Install

pip install context-window-packer-py

Quick start

from context_window_packer import pack

chunks = [
    {"text": "Pluto was reclassified...", "priority": 1, "relevance": 0.9},
    {"text": "Mars is the fourth planet...", "priority": 0, "relevance": 0.4},
    {"text": "Cats are independent...", "priority": 0, "relevance": 0.1},
]

result = pack(chunks, budget_tokens=20)

result.chunks   # list of dicts that fit
result.tokens   # int -- tokens consumed by kept chunks
result.dropped  # int -- count of chunks not kept

API

pack(chunks, budget_tokens, model=None) -> PackResult

Each chunk is a mapping with at minimum text. Optional fields:

Field Default Meaning
text "" The chunk content.
tokens ceil(len(text)/4) Pre-counted tokens (skips estimator).
relevance / score 0 Higher = more relevant; primary sort key (descending).
priority 0 Tie-breaker for relevance (descending).

PackResult is a dataclass with:

  • chunks: list[dict] -- chunks that fit, in pack order.
  • tokens: int -- total tokens consumed.
  • dropped: int -- count of chunks dropped.

model is reserved for future per-family token estimators (currently uses the standard ceil(chars/4) heuristic, matching the JS sibling).

License

MIT

Metadata

Release files for context-window-packer-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-window-packer-py 0.1.0
File Size Uploaded Provenance Actions
context_window_packer_py-0.1.0.tar.gz 4.7 kB Without provenance Details for context_window_packer_py-0.1.0.tar.gz

Built distribution (wheel)

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

Total release size: 9.7 kB

Release files / context_window_packer_py-0.1.0.tar.gz

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

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

Download URL context_window_packer_py-0.1.0-py3-none-any.whl
Size 5.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4b7a0b39e69ceb73182ba86f08bc9a531fedd07af9a83ab47ed6b69f258760a2
BLAKE2b-256 checksum
How to use checksums
7d0eb0c395daf44532203f40ffce46850472c0986432f8d52ac1b90d3b15b3f0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.4

Security for context-window-packer-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