Skip to main content

tool-output-truncate-py

PyPI Python License: MIT

Truncate tool output before adding it to LLM message history.

When an agent runs cat file.log, ripgrep, or a database query, the result can be megabytes. Naively appending it to the conversation blows the context window. The standard fix is to keep the head and the tail and replace the middle with an elision marker. This is that, char-aware (UTF-8 safe) and line-aware, with zero runtime dependencies.

Sibling to the Rust crate tool-output-truncate.

Install

pip install tool-output-truncate-py

Use

from tool_output_truncate import truncate_middle

big = open("server.log").read()
safe_to_send = truncate_middle(big, 4000)
# "first 2000 chars\n\n[123456 chars truncated]\n\n...last 2000 chars"

Four strategies:

from tool_output_truncate import (
    truncate_head,
    truncate_tail,
    truncate_middle,
    truncate_middle_lines,
)

truncate_head(text, max_chars)         # keep prefix
truncate_tail(text, max_chars)         # keep suffix
truncate_middle(text, max_chars)       # keep both ends (default for logs)
truncate_middle_lines(text, max_lines) # line-aware version of middle

All four are no-ops when the input already fits.

Examples

Keep the prefix (use when the tail is noise, e.g. long lists where order matters):

truncate_head("abcdefghij", 4)
# 'abcd\n\n[6 chars truncated]\n\n'

Keep the suffix (use when the head is preamble, e.g. command output with banner lines):

truncate_tail("abcdefghij", 4)
# '\n\n[6 chars truncated]\n\nghij'

Keep both ends (best default for arbitrary text where head and tail both carry signal):

truncate_middle("0123456789", 4)
# '01\n\n[6 chars truncated]\n\n89'

Line-aware middle (splits at line boundaries so you do not see half a line of JSON or a partial stack-trace frame):

text = "\n".join(f"line {i}" for i in range(100))
truncate_middle_lines(text, 4)
# 'line 0\nline 1\n[96 lines / ... chars truncated]\nline 98\nline 99'

UTF-8 is handled correctly. Python str is a sequence of codepoints, so slicing never splits a multi-byte character. Emoji, accented Latin, and CJK all count as one character each:

truncate_head("crab" + "\U0001f980" * 8, 6)
# keeps 6 codepoints from the start, marker reports remaining count

What it does NOT do

  • No tokenization. Pass a char cap. As a rough Anthropic and OpenAI proxy, treat chars * 4 == tokens (so 4000 chars is about 1k tokens).
  • No structured truncation (JSON, YAML, XML). For JSON specifically, parse first and decide which fields to keep.
  • No summarization. This is character arithmetic only.

Why a focused library

Most agent frameworks ship a custom truncator inline. They reinvent the edge cases each time: UTF-8 boundaries, odd-sized budgets, line-aware splitting that does not show half a line. This is the four-function library you grab instead.

License

MIT

Metadata

Release files for tool-output-truncate-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 tool-output-truncate-py 0.1.0
File Size Uploaded Provenance
tool_output_truncate_py-0.1.0.tar.gz 6.3 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for tool-output-truncate-py 0.1.0
File Interpreter ABI Platform
tool_output_truncate_py-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 12.2 kB

Release files / tool_output_truncate_py-0.1.0.tar.gz

Download URL tool_output_truncate_py-0.1.0.tar.gz
Size 6.3 kB
Tags Source
SHA-256 checksum
How to use checksums
a4fe8269f32a805d15abd6d439a0060b9ba1acfb405b04d34f74a23f3d1563dd
BLAKE2b-256 checksum
How to use checksums
d8f56c8b00ac36f146bc52ccfc5cad9b77f8e0c48d6a8d48aabca6b1e714c0d0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.5

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

Download URL tool_output_truncate_py-0.1.0-py3-none-any.whl
Size 5.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
06201630c2f3f4c39bf1283aa1dbb04f8099e6ad4b3db4be781e6977218bd660
BLAKE2b-256 checksum
How to use checksums
5ffbfa0f59a85f73128fe4f225766ba820a3ba0735d6ac76c8ef1c3a6cfe8444
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.5

Security for tool-output-truncate-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