tool-output-truncate 0.1.0

Truncate tool output (file reads, command runs, search hits) before adding to LLM message history. Char-aware head/middle/tail strategies with a configurable elision marker. Zero deps.
Documentation
  • Coverage
  • 100%
    5 out of 5 items documented1 out of 1 items with examples
  • Size
  • Source code size: 29.76 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 294.71 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 27s Average build duration of successful builds.
  • all releases: 27s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • MukundaKatta/tool-output-truncate
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • MukundaKatta

tool-output-truncate

Crates.io Documentation CI License

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

[dependencies]
tool-output-truncate = "0.1"

Use

use tool_output_truncate::truncate_middle;

let big = std::fs::read_to_string("server.log")?;
let safe_to_send = truncate_middle(&big, 4000);
// "first 2000 chars...\n\n[123456 chars truncated]\n\n...last 2000 chars"

Four strategies:

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.

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