delbina
Keep long LLM chats under a token budget. One function call, no server, no lock-in.
Why
Chat models are stateless. Every turn, your app re-sends the entire conversation, so a 50-turn chat pays for turn 1 fifty times. Total cost grows roughly with the square of chat length, and eventually the chat hits the context limit.
delbina sits between your chat history and the model call. It trims the history to a token budget you choose, keeps what matters (system prompt, recent turns), and can replace older turns with a running summary from a local model.
your messages ──► delbina.compress() ──► model API
(full history) (fits budget)
Install
pip install delbina
Python 3.10+. The only dependency is tiktoken (for counting tokens). On first use it downloads a tokenizer file, so the first run needs network access.
Quick start
from delbina import compress
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "..."},
{"role": "assistant", "content": "..."},
# ... hundreds more turns
]
small = compress(messages, max_tokens=2000)
# send `small` to your model instead of `messages`
What compress guarantees:
- leading system messages are always kept
- the last
keep_lastmessages (default 6) are always kept - older messages are dropped, oldest first, until the chat fits
max_tokens - the kept part starts on a user message, so tool calls and replies are never split
- your input list is never modified
In a chat loop (recommended)
Use one Compressor per conversation. With a summarizer, dropped turns become a summary, and the summary is updated incrementally: each turn only the newly dropped messages are summarized, not the whole history again.
from delbina import Compressor, ollama_summarizer
comp = Compressor(
max_tokens=3000,
keep_last=6,
summarize=ollama_summarizer("qwen2.5:1.5b"), # any local Ollama model
)
messages = [{"role": "system", "content": "You are a helpful assistant."}]
while True:
messages.append({"role": "user", "content": input("> ")})
reply = call_your_model(comp.compress(messages)) # full history stays in `messages`
messages.append({"role": "assistant", "content": reply})
print(reply)
print(comp.last_stats) # tokens_before, tokens_after, dropped, summarized
The summarizer is any function list[str] -> str, so you can plug in any model or API.
Ready-to-use examples for OpenAI, Anthropic and Ollama: docs/integrations.md and examples/.
CLI
delbina compress chat.json --max-tokens 2000 -o small.json # compress a saved chat
delbina bench chat.json # compare strategies' token cost
delbina --version
chat.json is {"messages": [{"role": ..., "content": ...}, ...]} or {"turns": ["...", ...]}.
On Windows, if delbina.exe is blocked by Smart App Control, use python -m delbina ... instead.
Measuring
from delbina import cost, full, last_n, rolling_summary
turns = ["...", "...", "..."]
cost(turns, full) # tokens spent if the full history is re-sent every turn
cost(turns, last_n(4)) # tokens spent if only the last 4 turns are sent
See docs/guide.md for every option and strategy.
Status
Alpha (0.x). The API may change between minor versions.
What works: token budgeting, system/tail preservation, safe cut points, incremental summaries, measurement tools.
Not built yet: answer-quality evaluation. Token savings alone don't prove the model still answers well; a benchmark for that is the next milestone. Token counts use OpenAI's cl100k_base encoding, which is an approximation for other models; pass count_tokens= for exact counts.
Contributing
Issues and pull requests are welcome. See CONTRIBUTING.md. Security issues: SECURITY.md.
License
Metadata
Release files for delbina 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| delbina-0.2.0.tar.gz | 16.6 kB | With provenance | Details for delbina-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| delbina-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for delbina-0.2.0-py3-none-any.whl |
Total release size: 31.2 kB
Release files / delbina-0.2.0.tar.gz
| Download URL | delbina-0.2.0.tar.gz |
|---|---|
| Size | 16.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
eaf4c1958e8d2158a43d503919a1372c609a6d0506b0fbdaac4c972fb5fc7f14
|
|
BLAKE2b-256 checksum How to use checksums |
01f28235aaab853758f9730bacb5e9df453b619e6eaa90743829cfecb824f6e6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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 Nov 23, 2026.
Transparency logRelease files / delbina-0.2.0-py3-none-any.whl
| Download URL | delbina-0.2.0-py3-none-any.whl |
|---|---|
| Size | 14.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a775428b722ccd274366e0ef7e544457561601f31f4170ecbc36d971356112f0
|
|
BLAKE2b-256 checksum How to use checksums |
5bc55566deacc2a7d312bfb33dd1e16eba9d3693e5e951e7cd5b9e6d786f43b9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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 Nov 23, 2026.
Transparency logSecurity for delbina 0.2.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.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for delbina-0.2.0.tar.gz
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 Nov 23, 2026.
Transparency logA 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.