convopack
Framework-agnostic, provider-agnostic context-window packer for LLM chat history.
Why
LLM apps accumulate messages until they overflow the model's context window. Existing fixes are framework-locked (LangChain, LangGraph), provider-specific (Anthropic context-management beta), or designed for long-term semantic memory rather than turn-by-turn packing (mem0).
convopack is a small, focused library that takes a conversation history and a token budget and returns the largest tail that fits, while:
- preserving
tool_use/tool_resultpairs atomically, - normalising message shapes across OpenAI, Anthropic, and Gemini,
- letting you plug in any tokenizer or summariser,
- staying async-friendly and zero-dependency at the core.
Install
pip install convopack # core only
pip install "convopack[tiktoken]" # + OpenAI tokenizer
pip install "convopack[anthropic]" # + Anthropic tokenizer
pip install "convopack[all]" # everything
# or with uv
uv add convopack # core
uv add "convopack[all]" # everything
The library ships a py.typed marker; mypy --strict and pyright both
recognise its public types without further configuration.
Quickstart
from convopack import Packer, Recency
packer = Packer(
budget=8000,
tokenizer="tiktoken:gpt-4o",
strategy=Recency(),
pin=["system", "first_user"],
)
packed = packer.pack(messages) # list[dict] in, list[dict] out
Strategies
| Strategy | When to use |
|---|---|
Recency |
Keep the tail that fits. Cheapest, no LLM call. |
FirstFit |
Keep the oldest chunks that fit. Good when system + few-shots dominate. |
SummaryEvict |
Summarise evicted turns into a single system message. |
Importance |
Score each turn yourself; drop the lowest until it fits. |
SemanticDedup |
Remove near-duplicate turns by embedding cosine, then fall back. |
Comparison
| Feature | convopack | LangChain trim_messages |
mem0 | Anthropic native |
|---|---|---|---|---|
| Framework-free | yes | no | no | yes |
| Multi-provider message shapes | yes | partial | n/a | no |
| Tool-call pair safety | yes | no | n/a | yes (one type) |
| Pluggable strategy | yes | no | n/a | no |
| Async summariser | yes | no | n/a | n/a |
| Scope | per-turn | per-turn | cross-session | per-turn |
Example notebooks
Self-contained Jupyter notebooks. Every one runs end-to-end without an API
key (they use the zero-dependency approx tokenizer and deterministic
fakes). All four are executed in CI to stay in sync with the library.
| Notebook | What it covers |
|---|---|
01_quickstart.ipynb |
Build a Packer, pack a history, round-trip through OpenAI shape, scan budgets. |
02_tool_pair_atomicity.ipynb |
The killer feature: tool_use / tool_result pairs stay together for every strategy. Includes a 200-iteration property check. |
03_strategies.ipynb |
Same history through all five strategies side-by-side. |
04_prompt_caching.ipynb |
Anthropic cache_control markers, OpenAI prefix-stability signature, cost back-of-envelope. |
Rendered with outputs at https://mrrobi.github.io/convopack/notebooks/01_quickstart/.
License
MIT
Metadata
Release files for convopack 0.3.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| convopack-0.3.2.tar.gz | 43.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| convopack-0.3.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 85.5 kB
Release files / convopack-0.3.2.tar.gz
| Download URL | convopack-0.3.2.tar.gz |
|---|---|
| Size | 43.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c0ec5ce10e1b8e4cc0df82e1dc1813ffcd8d552c8cb16a153857b303bbebf22f
|
|
BLAKE2b-256 checksum How to use checksums |
e371a3625d2caae7c686ab30c2666868a4aa9887cc4089bb8de40512f7579f7f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 25, 2026.
Transparency logRelease files / convopack-0.3.2-py3-none-any.whl
| Download URL | convopack-0.3.2-py3-none-any.whl |
|---|---|
| Size | 42.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
fd1659205097fd25786d26e94c4a937a236678e5feaaee1227423b1dab6f2b1d
|
|
BLAKE2b-256 checksum How to use checksums |
c6aef9cae158edd80e5d3c4798c12541e65a22367d70a9c5e18ad5f90c2dcfa4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 25, 2026.
Transparency log