Skip to main content

Framework-agnostic, provider-agnostic context-window packer for LLM chat history.

Project description

convopack

PyPI Python CI codecov Typed uv License

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_result pairs 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

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

convopack-0.3.1.tar.gz (43.0 kB view details)

Uploaded Source

Built Distribution

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

convopack-0.3.1-py3-none-any.whl (42.4 kB view details)

Uploaded Python 3

File details

Details for the file convopack-0.3.1.tar.gz.

File metadata

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

File hashes

Hashes for convopack-0.3.1.tar.gz
Algorithm Hash digest
SHA256 5f0f1bbf4dd33cd21627fba642e69bead4fb8de9aa5073da1203822e17428919
MD5 91dbd517af589a0b823d6c3dbe2031e1
BLAKE2b-256 d3f3d9377eea11401ce73038d4ed5d085e4d86d1781e393651790bea62c32f0c

See more details on using hashes here.

Provenance

The following attestation bundles were made for convopack-0.3.1.tar.gz:

Publisher: release.yml on Mrrobi/convopack

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

File details

Details for the file convopack-0.3.1-py3-none-any.whl.

File metadata

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

File hashes

Hashes for convopack-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 96296a6c19b3c0598aee0300cf983cc55bbb8e35add8e5bf67e2de4d7a9a2692
MD5 1144392a6a2ddf3cc1ae4426978ec437
BLAKE2b-256 20c90ca822e22491f3e1c784d890f7a9508bafe0e7c7cabf92f6dfc1e248d78d

See more details on using hashes here.

Provenance

The following attestation bundles were made for convopack-0.3.1-py3-none-any.whl:

Publisher: release.yml on Mrrobi/convopack

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