prompt-token-trim-py
Trim prompt messages to fit a token budget while preserving priority. Sort messages by priority (descending), accept each if it fits, then re-emit them in original order. Zero runtime dependencies.
Python port of @mukundakatta/prompt-token-trim.
Note: see also agentfit-py. They look similar; this one is message-level priority trimming (drop a single message if it doesn't fit). agentfit-py is whole-history strategy fitting (drop-oldest, drop-middle, priority, with system-message preservation and partial-result modes).
Install
pip install prompt-token-trim-py
Quick start
from prompt_token_trim import trim
messages = [
{"role": "system", "content": "You are a helpful assistant.", "priority": 10},
{"role": "user", "content": "Tell me about Pluto.", "priority": 5},
{"role": "assistant", "content": "Pluto is a dwarf planet.", "priority": 5},
{"role": "user", "content": "And Mars?", "priority": 1},
]
result = trim(messages, budget=20, preserve_system=True)
result.messages # list[dict] in original order, kept under budget
result.tokens # int -- tokens consumed
result.dropped # int -- count of messages dropped
API
trim(messages, budget, *, preserve_system=True) -> TrimResult
messages: list of dicts withrole,content, optionalpriority(defaults to0).budget: token budget (ceil(len(content) / 4)heuristic per message).preserve_system: keep allrole == "system"messages even if they don't fit (they consume budget anyway). Defaults toTrue.
TrimResult is a dataclass with:
messages: kept messages, in their original order.tokens: total tokens consumed.dropped: count of messages that did not survive.
The JS sibling exposes trimMessages({maxTokens}) -- the trim_messages(messages, *, max_tokens=...) alias is provided for parity.
License
MIT
Metadata
Release files for prompt-token-trim-py 0.1.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 |
|---|---|---|---|---|
| prompt_token_trim_py-0.1.0.tar.gz | 5.1 kB | Without provenance | Details for prompt_token_trim_py-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| prompt_token_trim_py-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for prompt_token_trim_py-0.1.0-py3-none-any.whl |
Total release size: 10.5 kB
Release files / prompt_token_trim_py-0.1.0.tar.gz
| Download URL | prompt_token_trim_py-0.1.0.tar.gz |
|---|---|
| Size | 5.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e8da815eee87385725f438fad3bd3cd7b738f85118ea32e06bcc7053bec1e70f
|
|
BLAKE2b-256 checksum How to use checksums |
53562733c48cf2b92f3524459acb36a5a0fe410d891d9cb48ee4ff4ff9798ff9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.4
|
Release files / prompt_token_trim_py-0.1.0-py3-none-any.whl
| Download URL | prompt_token_trim_py-0.1.0-py3-none-any.whl |
|---|---|
| Size | 5.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
23d0070bc1b9a1c958f6deb2b3a481832c35d5be711364524bbd2b48f3efa4fc
|
|
BLAKE2b-256 checksum How to use checksums |
e1b6faa859c2645a6459ece4a978762a0070d1bccc6b7c9be3ad18dcf948975c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.4
|
Security for prompt-token-trim-py 0.1.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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A 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.