Fit anything into an LLM context window — a tiny, zero-dependency, priority-aware token-budget packer.
Project description
contextcram
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
tiktokenor 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
798f2f1f86db92cf91ffb21b9a000f69f0051f92aa27767632b8fc0b352245cf
|
|
| MD5 |
c668f5a3fa38f92d03c97ee451775543
|
|
| BLAKE2b-256 |
a95056387d9b2effcf339e08af0502cb27e81809b211669cab918c53dd2fa662
|
Provenance
The following attestation bundles were made for contextcram-0.1.0.tar.gz:
Publisher:
publish.yml on Waelr1985/contextcram
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
contextcram-0.1.0.tar.gz -
Subject digest:
798f2f1f86db92cf91ffb21b9a000f69f0051f92aa27767632b8fc0b352245cf - Sigstore transparency entry: 1827675472
- Sigstore integration time:
-
Permalink:
Waelr1985/contextcram@dfee5243279c6918a21082e818fa62926c81bf1a -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/Waelr1985
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@dfee5243279c6918a21082e818fa62926c81bf1a -
Trigger Event:
release
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ca7d7f3533cfe83fe7b104d03a2cba615c05ea9ac4b4b3dacad2038a0e3844cc
|
|
| MD5 |
20b0ca55eb721f9f93d60fbce07e0494
|
|
| BLAKE2b-256 |
cf97509e9deec4b8a4b5ce75835f1a5d52c16b2c4b798f2d4bd17881e0d0f874
|
Provenance
The following attestation bundles were made for contextcram-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on Waelr1985/contextcram
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
contextcram-0.1.0-py3-none-any.whl -
Subject digest:
ca7d7f3533cfe83fe7b104d03a2cba615c05ea9ac4b4b3dacad2038a0e3844cc - Sigstore transparency entry: 1827675542
- Sigstore integration time:
-
Permalink:
Waelr1985/contextcram@dfee5243279c6918a21082e818fa62926c81bf1a -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/Waelr1985
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@dfee5243279c6918a21082e818fa62926c81bf1a -
Trigger Event:
release
-
Statement type: