Skip to main content

Fit anything into an LLM context window — a tiny, zero-dependency, priority-aware token-budget packer.

Project description

contextcram

PyPI version Python versions CI License: MIT

Fit anything into an LLM context window. A tiny, zero-dependency, priority-aware token-budget packer for RAG pipelines and agents.

Every RAG or agent app has the same problem: you have too much stuff — a system prompt, chat history, retrieved documents, tool output — and a fixed token budget. contextcram packs it all in by priority, truncating, trimming, or dropping the least important pieces so the important ones always make it.

from contextcram import Packer

packer = Packer(budget=8000)  # token budget

packer.add(system_prompt, priority="required")                 # never dropped
packer.add(chat_history, priority="high", strategy="trim")     # drop oldest turns
packer.add(retrieved_docs, priority="medium", strategy="drop") # all-or-nothing
packer.add(tool_output, priority="low", strategy="truncate")   # cut to fit

result = packer.fit()
print(result.text)            # assembled, in-budget context
print(result.used_tokens)     # e.g. 7840
print(result.dropped_names)   # what didn't make the cut

Why

  • Zero dependencies. Pure Python. Works out of the box with a fast characters-per-token heuristic; plug in tiktoken or any tokenizer when you need exact counts.
  • Framework-agnostic. Use it with LangChain, LlamaIndex, the raw provider SDKs, or nothing at all.
  • Priority-aware. You decide what survives a tight budget, not a blind truncate at the end.
  • Observable. Every result tells you what was kept, truncated, and dropped.

Installation

pip install contextcram
# optional: exact token counts via tiktoken
pip install "contextcram[tiktoken]"

Strategies

When an optional item doesn't fully fit, its strategy decides what happens:

Strategy Behavior
drop Include the item whole, or not at all
truncate Cut from the end, keeping the head (default)
truncate_head Cut from the start, keeping the tail
trim For list content: drop oldest segments first

required items are always kept; if they alone exceed the budget, a BudgetExceeded error is raised.

Exact token counts

from contextcram import Packer, tiktoken_tokenizer

packer = Packer(budget=8000, tokenizer=tiktoken_tokenizer("gpt-4o"))

Or wrap any tokenizer with CallableTokenizer(lambda s: len(my_encode(s))).

Priorities

Use the named levels "required", "high", "medium", "low", or pass any integer (higher is kept first):

packer.add(text, priority=42, strategy="truncate")

Development

git clone https://github.com/Waelr1985/contextcram.git
cd contextcram
uv sync
uv run pytest
uv run ruff check .
uv run mypy

License

MIT

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

contextcram-0.1.0.tar.gz (9.7 kB view details)

Uploaded Source

Built Distribution

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

contextcram-0.1.0-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: contextcram-0.1.0.tar.gz
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for contextcram-0.1.0.tar.gz
Algorithm Hash digest
SHA256 798f2f1f86db92cf91ffb21b9a000f69f0051f92aa27767632b8fc0b352245cf
MD5 c668f5a3fa38f92d03c97ee451775543
BLAKE2b-256 a95056387d9b2effcf339e08af0502cb27e81809b211669cab918c53dd2fa662

See more details on using hashes here.

Provenance

The following attestation bundles were made for contextcram-0.1.0.tar.gz:

Publisher: publish.yml on Waelr1985/contextcram

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

File details

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

File metadata

  • Download URL: contextcram-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for contextcram-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ca7d7f3533cfe83fe7b104d03a2cba615c05ea9ac4b4b3dacad2038a0e3844cc
MD5 20b0ca55eb721f9f93d60fbce07e0494
BLAKE2b-256 cf97509e9deec4b8a4b5ce75835f1a5d52c16b2c4b798f2d4bd17881e0d0f874

See more details on using hashes here.

Provenance

The following attestation bundles were made for contextcram-0.1.0-py3-none-any.whl:

Publisher: publish.yml on Waelr1985/contextcram

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 Pingdom Monitoring Sentry Error logging StatusPage Status page