Skip to main content

agentfit-py

PyPI Python License: MIT

Fit your messages into the LLM context window. Token-aware truncation with multiple strategies, pluggable tokenizers. Zero runtime dependencies.

Python port of @mukundakatta/agentfit. The JS sibling has the full design notes; this README sticks to the Python API.

Install

pip install agentfit-py

Usage

from agentfit import count, fit, OverBudgetError

messages = [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user",   "content": "Hello there!"},
    {"role": "assistant", "content": "Hi! How can I help?"},
    {"role": "user",   "content": "Tell me a long story..."},
]

# Estimate tokens (heuristic; pass `tokenizer=...` to plug in tiktoken etc.)
count(messages, model="claude-sonnet-4-6")   # -> int

# Drop messages until under budget. System message + recent N are preserved.
result = fit(
    messages,
    max_tokens=8000,
    model="claude-sonnet-4-6",
    strategy="drop-oldest",      # or "drop-middle" / "priority"
    preserve_system=True,
    preserve_last_n=2,
)

result.messages   # list[dict]  -- survived
result.dropped    # list[dict]  -- removed
result.tokens     # {"before": int, "after": int, "budget": int}
result.fit        # True iff under budget

If the budget can't be reached even after dropping all non-protected messages, fit() raises OverBudgetError (carries the partial result). Use on_over_budget="return-partial" to return the over-budget result instead with fit=False.

Strategies

Strategy Behavior
drop-oldest (default) Drop earliest non-protected message first.
drop-middle Drop messages closest to the center; preserves start + recent tail.
priority Drop messages with the lowest priority field first (default 0).

Custom tokenizer

Pass any Callable[[str], int]. Example with tiktoken:

import tiktoken
enc = tiktoken.get_encoding("cl100k_base")
fit(messages, max_tokens=8000, tokenizer=lambda s: len(enc.encode(s)))

API differences from the JS sibling

  • count() and fit() use Python keyword args (max_tokens=, preserve_system=, etc.) instead of the JS options object.
  • fit() returns a FitResult dataclass instead of a plain object.
  • No wrapFetch / monkey-patching equivalents -- not needed in Python.

See the JS sibling's README for the full design notes and broader algorithmic reasoning.

Metadata

Release files for agentfit-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)

Source distribution for agentfit-py 0.1.0
File Size Uploaded Provenance
agentfit_py-0.1.0.tar.gz 9.1 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for agentfit-py 0.1.0
File Interpreter ABI Platform
agentfit_py-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 18.1 kB

Release files / agentfit_py-0.1.0.tar.gz

Download URL agentfit_py-0.1.0.tar.gz
Size 9.1 kB
Tags Source
SHA-256 checksum
How to use checksums
c010d14b17b0216061cf1e1e4be53ef108c9211d9464a4655bfd9e2738b5d805
BLAKE2b-256 checksum
How to use checksums
70db680042f05c8fa4d1e51602f07a67dd8c01c5738af226998b54ec74347823
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.4

Release files / agentfit_py-0.1.0-py3-none-any.whl

Download URL agentfit_py-0.1.0-py3-none-any.whl
Size 8.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0ebe09d3d769443c8deb98c2d12c1a851ba84e14dbe58a38406eeb49bb38d202
BLAKE2b-256 checksum
How to use checksums
17a60dd161fc39e2a823e371ddff9ff0096ba2a03042e830d1d0b3fd9e9cdcac
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.4

Security for agentfit-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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page