Skip to main content

Trimwise

PyPI version

Need to fit a long document into a small AI prompt budget? Trimwise picks excerpts from across it that fit the space you set aside for source text, instead of just cutting off the end. Add a question to steer the choice; the selected passages keep their original wording and order.

Documentation · PyPI · Forever free public API

Try it

You can use Trimwise without installing it via the forever free public API - https://trimwise.aatbit.com

Install Trimwise with Python 3.10–3.14:

python -m pip install trimwise

Here's the input and the question:

from trimwise import Trimmer

report = """# Incident report

The site went down at 09:14. Initial checks focused on the network.

## Root cause

An expired credential blocked the service.

## Prevention

Credentials will now rotate every 30 days.
"""

result = Trimmer().trim(
    report,
    limit=24,
    query="What caused the outage and how will it be prevented?",
)
print(result.text)
print(f"{result.output_count}/{result.limit} tokens")

Output from running that code:

## Root cause

An expired credential blocked the service.

## Prevention

Credentials will now rotate every 30 days.

23/24 tokens

The limit is a ceiling, not a target to fill. It can be measured in tokens (the default), words, or characters. result.spans also tells you where each kept piece came from in the original Python string.

Have more than one source?

Use trim_context() when several sources must share one limit. This input has an initial report and a follow-up:

from trimwise import ContextSource, Trimmer

sources = [
    ContextSource(
        text="Initial checks focused on the network. The network was healthy.",
        prefix="Incident: ",
    ),
    ContextSource(
        text="A retry loop ignored backoff settings. Service recovered at 14:32 UTC.",
        prefix="Follow-up: ",
    ),
]

result = Trimmer().trim_context(
    sources,
    limit=20,
    query="Which retry loop caused the outage, and when did service recover?",
)
print(result.text)
print(f"{result.output_count}/{result.limit} tokens")

Output from running that code:

Follow-up: A retry loop ignored backoff settings. Service recovered at 14:32 UTC.
20/20 tokens

Only the follow-up contributes to this result. You still get one result row per input source, including an empty row for the incident report. Source labels count toward this limit; your surrounding prompt instructions and answer space do not. See Many Sources, One Shared Limit for the full behavior.

Which strategy should you use?

Start with the default, auto. With a question, it matches relevant words (lexical). Without one, it makes a broader document excerpt (structural). Neither downloads a model.

If the answer may use different wording from your question, try semantic. hybrid combines word matching and semantic matching. Those two need your own embedding callback or the optional semantic model. The strategy guide has examples and tradeoffs.

What Trimwise does—and doesn't

Trimwise works on text you already have. It does not search for documents or rewrite them. Unlike token-pruning compressors, it keeps readable source pieces; see the research comparison for the tradeoff.

Observe trimming in your application

Trimwise creates OpenTelemetry spans for its public operations when your application configures an OpenTelemetry SDK. Without an SDK provider, the API stays a no-op and sends nothing. Your application keeps control of exporters, collector endpoints, credentials, resources, sampling, batching, and shutdown; none of those settings are added to TrimConfig.

See Observability for OTLP/gRPC and OTLP/HTTP setup, span names and attributes, trace parenting, and privacy guarantees.

A tight limit can leave out evidence needed to answer a question. The limit applies to Trimwise's returned text, not your entire prompt, so leave room for instructions and the model's answer. Read the guarantees and limitations before relying on an excerpt for a high-stakes task.

How it did in one test

In a frozen 160-case test using the published 0.2.0 release, Trimwise Hybrid kept every annotated source passage in 66.9% of cases at a 512-token limit. This measures passage survival on that test—not answer quality or performance on every document. The benchmark report explains the setup, comparisons, and limits.

Complete annotated source-passage survival by output-token limit on 160 position-controlled cases, comparing Trimwise with evaluated adapters.

In a separate 93-case short-answer check at 256 tokens, GPT-5.4 Nano matched 43 reference answers with Hybrid's output, versus 41 with the full source (46.2% vs. 44.1%). GPT-5.4 Mini scored 46.2% vs. 45.2%; GPT-5.6 Luna tied at 45.2%. Each context received one model response. This automatic text match is a useful diagnostic, not a human correctness judgment or proof that trimming generally improves answers.

Answer-match rate on 93 cases across context limits for GPT-5.4 Mini, GPT-5.4 Nano, and GPT-5.6 Luna. At 256 tokens, Nano scored 46.2% with Trimwise Hybrid versus 44.1% with the full source.

Open the full-size answer-match graph · How the answer check was scored

At 512 tokens, Hybrid's median warm trim time was 42.8 ms on the benchmark machine (Lexical: 6.4 ms). Those timings exclude cold model loading and are not portable speed guarantees.

For more detail, see Getting Started, Configuration and API, or the API Reference.

Trimwise is available under the MIT License and maintained by AATBIT Labs.

Metadata

Release files for trimwise 0.7.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 trimwise 0.7.0
File Size Uploaded
trimwise-0.7.0.tar.gz 37.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for trimwise 0.7.0
File Interpreter ABI Platform
trimwise-0.7.0-py3-none-any.whl Python 3 none any Details

Total release size: 76.1 kB

Release files / trimwise-0.7.0.tar.gz

Download URL trimwise-0.7.0.tar.gz
Size 37.2 kB
Tags Source
SHA-256 checksum
How to use checksums
38674bd6a6ebe9db95aa7acaa396740145e15d5d388d3d70431e1882164ea61e
BLAKE2b-256 checksum
How to use checksums
04d972cfc26afc7c82f3a2de0efbb4cd4db57e5b2ca89693715886f0ff11934a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Sep 21, 2026.

Transparency log

Release files / trimwise-0.7.0-py3-none-any.whl

Download URL trimwise-0.7.0-py3-none-any.whl
Size 38.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
80541dbb0a7ef4ba70e0d0bf0edc3e4965877574980ea62d2c56ad0288be19f7
BLAKE2b-256 checksum
How to use checksums
523d60a6f2cdb2e08179f202a6008a2ae6c7322b2347182cc044882228586b5b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Sep 21, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.7.0 This release

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

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