tool-output-truncate
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 keep head + tail and replace the
middle with an elision marker. This is that, char-aware (UTF-8 safe) and
line-aware, zero deps.
Install
[]
= "0.1"
Use
use truncate_middle;
let big = read_to_string?;
let safe_to_send = truncate_middle;
// "first 2000 chars...\n\n[123456 chars truncated]\n\n...last 2000 chars"
Four strategies:
truncate_head // keep prefix
truncate_tail // keep suffix
truncate_middle // keep both ends (default for logs)
truncate_middle_lines // line-aware version of middle
All four are no-ops when the input already fits.
What it does NOT do
- No tokenization. Pass a char cap. As a rough Anthropic/OpenAI proxy,
treat
chars * 4 ≈ tokens(so 4000 chars ≈ 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 crate
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 doesn't show half a line. This is the four-function lib you grab instead.
License
MIT OR Apache-2.0